00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "bfd.h"
00029 #include "sysdep.h"
00030 #include "objalloc.h"
00031 #include "libbfd.h"
00032 #include "libiberty.h"
00033
00034 #ifndef S_IXUSR
00035 #define S_IXUSR 0100
00036 #endif
00037 #ifndef S_IXGRP
00038 #define S_IXGRP 0010
00039 #endif
00040 #ifndef S_IXOTH
00041 #define S_IXOTH 0001
00042 #endif
00043
00044
00045
00046 static unsigned int _bfd_id_counter = 0;
00047
00048
00049
00050
00051
00052
00053 bfd *
00054 _bfd_new_bfd (void)
00055 {
00056 bfd *nbfd;
00057
00058 nbfd = bfd_zmalloc (sizeof (bfd));
00059 if (nbfd == NULL)
00060 return NULL;
00061
00062 nbfd->id = _bfd_id_counter++;
00063
00064 nbfd->memory = objalloc_create ();
00065 if (nbfd->memory == NULL)
00066 {
00067 bfd_set_error (bfd_error_no_memory);
00068 free (nbfd);
00069 return NULL;
00070 }
00071
00072 nbfd->arch_info = &bfd_default_arch_struct;
00073
00074 nbfd->direction = no_direction;
00075 nbfd->iostream = NULL;
00076 nbfd->where = 0;
00077 if (!bfd_hash_table_init_n (& nbfd->section_htab, bfd_section_hash_newfunc,
00078 251))
00079 {
00080 free (nbfd);
00081 return NULL;
00082 }
00083 nbfd->sections = NULL;
00084 nbfd->section_tail = &nbfd->sections;
00085 nbfd->format = bfd_unknown;
00086 nbfd->my_archive = NULL;
00087 nbfd->origin = 0;
00088 nbfd->opened_once = FALSE;
00089 nbfd->output_has_begun = FALSE;
00090 nbfd->section_count = 0;
00091 nbfd->usrdata = NULL;
00092 nbfd->cacheable = FALSE;
00093 nbfd->flags = BFD_NO_FLAGS;
00094 nbfd->mtime_set = FALSE;
00095
00096 return nbfd;
00097 }
00098
00099
00100
00101 bfd *
00102 _bfd_new_bfd_contained_in (bfd *obfd)
00103 {
00104 bfd *nbfd;
00105
00106 nbfd = _bfd_new_bfd ();
00107 if (nbfd == NULL)
00108 return NULL;
00109 nbfd->xvec = obfd->xvec;
00110 nbfd->iovec = obfd->iovec;
00111 nbfd->my_archive = obfd;
00112 nbfd->direction = read_direction;
00113 nbfd->target_defaulted = obfd->target_defaulted;
00114 return nbfd;
00115 }
00116
00117
00118
00119 void
00120 _bfd_delete_bfd (bfd *abfd)
00121 {
00122 bfd_hash_table_free (&abfd->section_htab);
00123 objalloc_free ((struct objalloc *) abfd->memory);
00124 free (abfd);
00125 }
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144
00145
00146
00147
00148
00149
00150
00151
00152 bfd *
00153 bfd_openr (const char *filename, const char *target)
00154 {
00155 bfd *nbfd;
00156 const bfd_target *target_vec;
00157
00158 nbfd = _bfd_new_bfd ();
00159 if (nbfd == NULL)
00160 return NULL;
00161
00162 target_vec = bfd_find_target (target, nbfd);
00163 if (target_vec == NULL)
00164 {
00165 _bfd_delete_bfd (nbfd);
00166 return NULL;
00167 }
00168
00169 nbfd->filename = filename;
00170 nbfd->direction = read_direction;
00171
00172 if (bfd_open_file (nbfd) == NULL)
00173 {
00174
00175 bfd_set_error (bfd_error_system_call);
00176 _bfd_delete_bfd (nbfd);
00177 return NULL;
00178 }
00179
00180 return nbfd;
00181 }
00182
00183
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215
00216 bfd *
00217 bfd_fdopenr (const char *filename, const char *target, int fd)
00218 {
00219 bfd *nbfd;
00220 const bfd_target *target_vec;
00221 int fdflags;
00222
00223 bfd_set_error (bfd_error_system_call);
00224 #if ! defined(HAVE_FCNTL) || ! defined(F_GETFL)
00225 fdflags = O_RDWR;
00226 #else
00227 fdflags = fcntl (fd, F_GETFL, NULL);
00228 #endif
00229 if (fdflags == -1)
00230 return NULL;
00231
00232 nbfd = _bfd_new_bfd ();
00233 if (nbfd == NULL)
00234 return NULL;
00235
00236 target_vec = bfd_find_target (target, nbfd);
00237 if (target_vec == NULL)
00238 {
00239 _bfd_delete_bfd (nbfd);
00240 return NULL;
00241 }
00242
00243 #ifndef HAVE_FDOPEN
00244 nbfd->iostream = fopen (filename, FOPEN_RB);
00245 #else
00246
00247 switch (fdflags & (O_ACCMODE))
00248 {
00249 case O_RDONLY: nbfd->iostream = fdopen (fd, FOPEN_RB); break;
00250 case O_WRONLY: nbfd->iostream = fdopen (fd, FOPEN_RUB); break;
00251 case O_RDWR: nbfd->iostream = fdopen (fd, FOPEN_RUB); break;
00252 default: abort ();
00253 }
00254 #endif
00255
00256 if (nbfd->iostream == NULL)
00257 {
00258 _bfd_delete_bfd (nbfd);
00259 return NULL;
00260 }
00261
00262
00263 nbfd->filename = filename;
00264
00265
00266
00267
00268
00269 switch (fdflags & (O_ACCMODE))
00270 {
00271 case O_RDONLY: nbfd->direction = read_direction; break;
00272 case O_WRONLY: nbfd->direction = write_direction; break;
00273 case O_RDWR: nbfd->direction = both_direction; break;
00274 default: abort ();
00275 }
00276
00277 if (! bfd_cache_init (nbfd))
00278 {
00279 _bfd_delete_bfd (nbfd);
00280 return NULL;
00281 }
00282 nbfd->opened_once = TRUE;
00283
00284 return nbfd;
00285 }
00286
00287
00288
00289
00290
00291
00292
00293
00294
00295
00296
00297
00298
00299
00300 bfd *
00301 bfd_openstreamr (const char *filename, const char *target, void *streamarg)
00302 {
00303 FILE *stream = streamarg;
00304 bfd *nbfd;
00305 const bfd_target *target_vec;
00306
00307 nbfd = _bfd_new_bfd ();
00308 if (nbfd == NULL)
00309 return NULL;
00310
00311 target_vec = bfd_find_target (target, nbfd);
00312 if (target_vec == NULL)
00313 {
00314 _bfd_delete_bfd (nbfd);
00315 return NULL;
00316 }
00317
00318 nbfd->iostream = stream;
00319 nbfd->filename = filename;
00320 nbfd->direction = read_direction;
00321
00322 if (! bfd_cache_init (nbfd))
00323 {
00324 _bfd_delete_bfd (nbfd);
00325 return NULL;
00326 }
00327
00328 return nbfd;
00329 }
00330
00331
00332
00333
00334
00335
00336
00337
00338
00339
00340
00341
00342
00343
00344
00345
00346
00347
00348
00349
00350
00351
00352
00353
00354
00355
00356
00357
00358
00359
00360
00361
00362
00363
00364
00365
00366
00367
00368
00369
00370
00371
00372
00373
00374
00375
00376
00377
00378
00379 struct opncls
00380 {
00381 void *stream;
00382 file_ptr (*pread) (struct bfd *abfd, void *stream, void *buf,
00383 file_ptr nbytes, file_ptr offset);
00384 int (*close) (struct bfd *abfd, void *stream);
00385 file_ptr where;
00386 };
00387
00388 static file_ptr
00389 opncls_btell (struct bfd *abfd)
00390 {
00391 struct opncls *vec = abfd->iostream;
00392 return vec->where;
00393 }
00394
00395 static int
00396 opncls_bseek (struct bfd *abfd, file_ptr offset, int whence)
00397 {
00398 struct opncls *vec = abfd->iostream;
00399 switch (whence)
00400 {
00401 case SEEK_SET: vec->where = offset; break;
00402 case SEEK_CUR: vec->where += offset; break;
00403 case SEEK_END: return -1;
00404 }
00405 return 0;
00406 }
00407
00408 static file_ptr
00409 opncls_bread (struct bfd *abfd, void *buf, file_ptr nbytes)
00410 {
00411 struct opncls *vec = abfd->iostream;
00412 file_ptr nread = (vec->pread) (abfd, vec->stream, buf, nbytes, vec->where);
00413 if (nread < 0)
00414 return nread;
00415 vec->where += nread;
00416 return nread;
00417 }
00418
00419 static file_ptr
00420 opncls_bwrite (struct bfd *abfd ATTRIBUTE_UNUSED,
00421 const void *where ATTRIBUTE_UNUSED,
00422 file_ptr nbytes ATTRIBUTE_UNUSED)
00423 {
00424 return -1;
00425 }
00426
00427 static int
00428 opncls_bclose (struct bfd *abfd)
00429 {
00430 struct opncls *vec = abfd->iostream;
00431
00432
00433 int status = 0;
00434 if (vec->close != NULL)
00435 status = (vec->close) (abfd, vec->stream);
00436 abfd->iostream = NULL;
00437 return status;
00438 }
00439
00440 static int
00441 opncls_bflush (struct bfd *abfd ATTRIBUTE_UNUSED)
00442 {
00443 return 0;
00444 }
00445
00446 static int
00447 opncls_bstat (struct bfd *abfd ATTRIBUTE_UNUSED, struct stat *sb)
00448 {
00449 memset (sb, 0, sizeof (*sb));
00450 return 0;
00451 }
00452
00453 static const struct bfd_iovec opncls_iovec = {
00454 &opncls_bread, &opncls_bwrite, &opncls_btell, &opncls_bseek,
00455 &opncls_bclose, &opncls_bflush, &opncls_bstat
00456 };
00457
00458 bfd *
00459 bfd_openr_iovec (const char *filename, const char *target,
00460 void *(*open) (struct bfd *nbfd,
00461 void *open_closure),
00462 void *open_closure,
00463 file_ptr (*pread) (struct bfd *abfd,
00464 void *stream,
00465 void *buf,
00466 file_ptr nbytes,
00467 file_ptr offset),
00468 int (*close) (struct bfd *nbfd,
00469 void *stream))
00470 {
00471 bfd *nbfd;
00472 const bfd_target *target_vec;
00473 struct opncls *vec;
00474 void *stream;
00475
00476 nbfd = _bfd_new_bfd ();
00477 if (nbfd == NULL)
00478 return NULL;
00479
00480 target_vec = bfd_find_target (target, nbfd);
00481 if (target_vec == NULL)
00482 {
00483 _bfd_delete_bfd (nbfd);
00484 return NULL;
00485 }
00486
00487 nbfd->filename = filename;
00488 nbfd->direction = read_direction;
00489
00490 stream = open (nbfd, open_closure);
00491 if (stream == NULL)
00492 {
00493 _bfd_delete_bfd (nbfd);
00494 return NULL;
00495 }
00496
00497 vec = bfd_zalloc (nbfd, sizeof (struct opncls));
00498 vec->stream = stream;
00499 vec->pread = pread;
00500 vec->close = close;
00501
00502 nbfd->iovec = &opncls_iovec;
00503 nbfd->iostream = vec;
00504
00505 return nbfd;
00506 }
00507
00508
00509
00510
00511
00512
00513
00514
00515
00516
00517
00518
00519
00520
00521
00522
00523
00524
00525
00526
00527
00528 bfd *
00529 bfd_openw (const char *filename, const char *target)
00530 {
00531 bfd *nbfd;
00532 const bfd_target *target_vec;
00533
00534
00535
00536 nbfd = _bfd_new_bfd ();
00537 if (nbfd == NULL)
00538 return NULL;
00539
00540 target_vec = bfd_find_target (target, nbfd);
00541 if (target_vec == NULL)
00542 {
00543 _bfd_delete_bfd (nbfd);
00544 return NULL;
00545 }
00546
00547 nbfd->filename = filename;
00548 nbfd->direction = write_direction;
00549
00550 if (bfd_open_file (nbfd) == NULL)
00551 {
00552
00553 bfd_set_error (bfd_error_system_call);
00554 _bfd_delete_bfd (nbfd);
00555 return NULL;
00556 }
00557
00558 return nbfd;
00559 }
00560
00561
00562
00563
00564
00565
00566
00567
00568
00569
00570
00571
00572
00573
00574
00575
00576
00577
00578
00579
00580
00581
00582
00583
00584
00585
00586 bfd_boolean
00587 bfd_close (bfd *abfd)
00588 {
00589 bfd_boolean ret;
00590
00591 if (bfd_write_p (abfd))
00592 {
00593 if (! BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)))
00594 return FALSE;
00595 }
00596
00597 if (! BFD_SEND (abfd, _close_and_cleanup, (abfd)))
00598 return FALSE;
00599
00600
00601
00602 if (!(abfd->flags & BFD_IN_MEMORY))
00603 ret = abfd->iovec->bclose (abfd);
00604 else
00605 ret = TRUE;
00606
00607
00608
00609 if (ret
00610 && abfd->direction == write_direction
00611 && abfd->flags & EXEC_P)
00612 {
00613 struct stat buf;
00614
00615 if (stat (abfd->filename, &buf) == 0)
00616 {
00617 unsigned int mask = umask (0);
00618
00619 umask (mask);
00620 chmod (abfd->filename,
00621 (0777
00622 & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
00623 }
00624 }
00625
00626 _bfd_delete_bfd (abfd);
00627
00628 return ret;
00629 }
00630
00631
00632
00633
00634
00635
00636
00637
00638
00639
00640
00641
00642
00643
00644
00645
00646
00647
00648
00649
00650
00651
00652
00653 bfd_boolean
00654 bfd_close_all_done (bfd *abfd)
00655 {
00656 bfd_boolean ret;
00657
00658 ret = bfd_cache_close (abfd);
00659
00660
00661
00662 if (ret
00663 && abfd->direction == write_direction
00664 && abfd->flags & EXEC_P)
00665 {
00666 struct stat buf;
00667
00668 if (stat (abfd->filename, &buf) == 0)
00669 {
00670 unsigned int mask = umask (0);
00671
00672 umask (mask);
00673 chmod (abfd->filename,
00674 (0777
00675 & (buf.st_mode | ((S_IXUSR | S_IXGRP | S_IXOTH) &~ mask))));
00676 }
00677 }
00678
00679 _bfd_delete_bfd (abfd);
00680
00681 return ret;
00682 }
00683
00684
00685
00686
00687
00688
00689
00690
00691
00692
00693
00694
00695
00696
00697 bfd *
00698 bfd_create (const char *filename, bfd *templ)
00699 {
00700 bfd *nbfd;
00701
00702 nbfd = _bfd_new_bfd ();
00703 if (nbfd == NULL)
00704 return NULL;
00705 nbfd->filename = filename;
00706 if (templ)
00707 nbfd->xvec = templ->xvec;
00708 nbfd->direction = no_direction;
00709 bfd_set_format (nbfd, bfd_object);
00710
00711 return nbfd;
00712 }
00713
00714
00715
00716
00717
00718
00719
00720
00721
00722
00723
00724
00725
00726
00727
00728
00729
00730
00731 bfd_boolean
00732 bfd_make_writable (bfd *abfd)
00733 {
00734 struct bfd_in_memory *bim;
00735
00736 if (abfd->direction != no_direction)
00737 {
00738 bfd_set_error (bfd_error_invalid_operation);
00739 return FALSE;
00740 }
00741
00742 bim = bfd_malloc (sizeof (struct bfd_in_memory));
00743 abfd->iostream = bim;
00744
00745 bim->size = 0;
00746 bim->buffer = 0;
00747
00748 abfd->flags |= BFD_IN_MEMORY;
00749 abfd->direction = write_direction;
00750 abfd->where = 0;
00751
00752 return TRUE;
00753 }
00754
00755
00756
00757
00758
00759
00760
00761
00762
00763
00764
00765
00766
00767
00768
00769
00770
00771
00772 bfd_boolean
00773 bfd_make_readable (bfd *abfd)
00774 {
00775 if (abfd->direction != write_direction || !(abfd->flags & BFD_IN_MEMORY))
00776 {
00777 bfd_set_error (bfd_error_invalid_operation);
00778 return FALSE;
00779 }
00780
00781 if (! BFD_SEND_FMT (abfd, _bfd_write_contents, (abfd)))
00782 return FALSE;
00783
00784 if (! BFD_SEND (abfd, _close_and_cleanup, (abfd)))
00785 return FALSE;
00786
00787
00788 abfd->arch_info = &bfd_default_arch_struct;
00789
00790 abfd->where = 0;
00791 abfd->format = bfd_unknown;
00792 abfd->my_archive = NULL;
00793 abfd->origin = 0;
00794 abfd->opened_once = FALSE;
00795 abfd->output_has_begun = FALSE;
00796 abfd->section_count = 0;
00797 abfd->usrdata = NULL;
00798 abfd->cacheable = FALSE;
00799 abfd->flags = BFD_IN_MEMORY;
00800 abfd->mtime_set = FALSE;
00801
00802 abfd->target_defaulted = TRUE;
00803 abfd->direction = read_direction;
00804 abfd->sections = 0;
00805 abfd->symcount = 0;
00806 abfd->outsymbols = 0;
00807 abfd->tdata.any = 0;
00808
00809 bfd_section_list_clear (abfd);
00810 bfd_check_format (abfd, bfd_object);
00811
00812 return TRUE;
00813 }
00814
00815
00816
00817
00818
00819
00820
00821
00822
00823
00824
00825
00826
00827 void *
00828 bfd_alloc (bfd *abfd, bfd_size_type size)
00829 {
00830 void *ret;
00831
00832 if (size != (unsigned long) size)
00833 {
00834 bfd_set_error (bfd_error_no_memory);
00835 return NULL;
00836 }
00837
00838 ret = objalloc_alloc (abfd->memory, (unsigned long) size);
00839 if (ret == NULL)
00840 bfd_set_error (bfd_error_no_memory);
00841 return ret;
00842 }
00843
00844
00845
00846
00847
00848
00849
00850
00851
00852
00853
00854
00855
00856 void *
00857 bfd_zalloc (bfd *abfd, bfd_size_type size)
00858 {
00859 void *res;
00860
00861 res = bfd_alloc (abfd, size);
00862 if (res)
00863 memset (res, 0, (size_t) size);
00864 return res;
00865 }
00866
00867
00868
00869
00870 void
00871 bfd_release (bfd *abfd, void *block)
00872 {
00873 objalloc_free_block ((struct objalloc *) abfd->memory, block);
00874 }
00875
00876
00877
00878
00879
00880
00881
00882
00883
00884
00885
00886
00887
00888
00889
00890
00891 #define GNU_DEBUGLINK ".gnu_debuglink"
00892
00893
00894
00895
00896
00897
00898
00899
00900
00901
00902
00903
00904
00905
00906
00907
00908
00909 unsigned long
00910 bfd_calc_gnu_debuglink_crc32 (unsigned long crc,
00911 const unsigned char *buf,
00912 bfd_size_type len)
00913 {
00914 static const unsigned long crc32_table[256] =
00915 {
00916 0x00000000, 0x77073096, 0xee0e612c, 0x990951ba, 0x076dc419,
00917 0x706af48f, 0xe963a535, 0x9e6495a3, 0x0edb8832, 0x79dcb8a4,
00918 0xe0d5e91e, 0x97d2d988, 0x09b64c2b, 0x7eb17cbd, 0xe7b82d07,
00919 0x90bf1d91, 0x1db71064, 0x6ab020f2, 0xf3b97148, 0x84be41de,
00920 0x1adad47d, 0x6ddde4eb, 0xf4d4b551, 0x83d385c7, 0x136c9856,
00921 0x646ba8c0, 0xfd62f97a, 0x8a65c9ec, 0x14015c4f, 0x63066cd9,
00922 0xfa0f3d63, 0x8d080df5, 0x3b6e20c8, 0x4c69105e, 0xd56041e4,
00923 0xa2677172, 0x3c03e4d1, 0x4b04d447, 0xd20d85fd, 0xa50ab56b,
00924 0x35b5a8fa, 0x42b2986c, 0xdbbbc9d6, 0xacbcf940, 0x32d86ce3,
00925 0x45df5c75, 0xdcd60dcf, 0xabd13d59, 0x26d930ac, 0x51de003a,
00926 0xc8d75180, 0xbfd06116, 0x21b4f4b5, 0x56b3c423, 0xcfba9599,
00927 0xb8bda50f, 0x2802b89e, 0x5f058808, 0xc60cd9b2, 0xb10be924,
00928 0x2f6f7c87, 0x58684c11, 0xc1611dab, 0xb6662d3d, 0x76dc4190,
00929 0x01db7106, 0x98d220bc, 0xefd5102a, 0x71b18589, 0x06b6b51f,
00930 0x9fbfe4a5, 0xe8b8d433, 0x7807c9a2, 0x0f00f934, 0x9609a88e,
00931 0xe10e9818, 0x7f6a0dbb, 0x086d3d2d, 0x91646c97, 0xe6635c01,
00932 0x6b6b51f4, 0x1c6c6162, 0x856530d8, 0xf262004e, 0x6c0695ed,
00933 0x1b01a57b, 0x8208f4c1, 0xf50fc457, 0x65b0d9c6, 0x12b7e950,
00934 0x8bbeb8ea, 0xfcb9887c, 0x62dd1ddf, 0x15da2d49, 0x8cd37cf3,
00935 0xfbd44c65, 0x4db26158, 0x3ab551ce, 0xa3bc0074, 0xd4bb30e2,
00936 0x4adfa541, 0x3dd895d7, 0xa4d1c46d, 0xd3d6f4fb, 0x4369e96a,
00937 0x346ed9fc, 0xad678846, 0xda60b8d0, 0x44042d73, 0x33031de5,
00938 0xaa0a4c5f, 0xdd0d7cc9, 0x5005713c, 0x270241aa, 0xbe0b1010,
00939 0xc90c2086, 0x5768b525, 0x206f85b3, 0xb966d409, 0xce61e49f,
00940 0x5edef90e, 0x29d9c998, 0xb0d09822, 0xc7d7a8b4, 0x59b33d17,
00941 0x2eb40d81, 0xb7bd5c3b, 0xc0ba6cad, 0xedb88320, 0x9abfb3b6,
00942 0x03b6e20c, 0x74b1d29a, 0xead54739, 0x9dd277af, 0x04db2615,
00943 0x73dc1683, 0xe3630b12, 0x94643b84, 0x0d6d6a3e, 0x7a6a5aa8,
00944 0xe40ecf0b, 0x9309ff9d, 0x0a00ae27, 0x7d079eb1, 0xf00f9344,
00945 0x8708a3d2, 0x1e01f268, 0x6906c2fe, 0xf762575d, 0x806567cb,
00946 0x196c3671, 0x6e6b06e7, 0xfed41b76, 0x89d32be0, 0x10da7a5a,
00947 0x67dd4acc, 0xf9b9df6f, 0x8ebeeff9, 0x17b7be43, 0x60b08ed5,
00948 0xd6d6a3e8, 0xa1d1937e, 0x38d8c2c4, 0x4fdff252, 0xd1bb67f1,
00949 0xa6bc5767, 0x3fb506dd, 0x48b2364b, 0xd80d2bda, 0xaf0a1b4c,
00950 0x36034af6, 0x41047a60, 0xdf60efc3, 0xa867df55, 0x316e8eef,
00951 0x4669be79, 0xcb61b38c, 0xbc66831a, 0x256fd2a0, 0x5268e236,
00952 0xcc0c7795, 0xbb0b4703, 0x220216b9, 0x5505262f, 0xc5ba3bbe,
00953 0xb2bd0b28, 0x2bb45a92, 0x5cb36a04, 0xc2d7ffa7, 0xb5d0cf31,
00954 0x2cd99e8b, 0x5bdeae1d, 0x9b64c2b0, 0xec63f226, 0x756aa39c,
00955 0x026d930a, 0x9c0906a9, 0xeb0e363f, 0x72076785, 0x05005713,
00956 0x95bf4a82, 0xe2b87a14, 0x7bb12bae, 0x0cb61b38, 0x92d28e9b,
00957 0xe5d5be0d, 0x7cdcefb7, 0x0bdbdf21, 0x86d3d2d4, 0xf1d4e242,
00958 0x68ddb3f8, 0x1fda836e, 0x81be16cd, 0xf6b9265b, 0x6fb077e1,
00959 0x18b74777, 0x88085ae6, 0xff0f6a70, 0x66063bca, 0x11010b5c,
00960 0x8f659eff, 0xf862ae69, 0x616bffd3, 0x166ccf45, 0xa00ae278,
00961 0xd70dd2ee, 0x4e048354, 0x3903b3c2, 0xa7672661, 0xd06016f7,
00962 0x4969474d, 0x3e6e77db, 0xaed16a4a, 0xd9d65adc, 0x40df0b66,
00963 0x37d83bf0, 0xa9bcae53, 0xdebb9ec5, 0x47b2cf7f, 0x30b5ffe9,
00964 0xbdbdf21c, 0xcabac28a, 0x53b39330, 0x24b4a3a6, 0xbad03605,
00965 0xcdd70693, 0x54de5729, 0x23d967bf, 0xb3667a2e, 0xc4614ab8,
00966 0x5d681b02, 0x2a6f2b94, 0xb40bbe37, 0xc30c8ea1, 0x5a05df1b,
00967 0x2d02ef8d
00968 };
00969 const unsigned char *end;
00970
00971 crc = ~crc & 0xffffffff;
00972 for (end = buf + len; buf < end; ++ buf)
00973 crc = crc32_table[(crc ^ *buf) & 0xff] ^ (crc >> 8);
00974 return ~crc & 0xffffffff;;
00975 }
00976
00977
00978
00979
00980
00981
00982
00983
00984
00985
00986
00987
00988
00989
00990
00991 static char *
00992 get_debug_link_info (bfd *abfd, unsigned long *crc32_out)
00993 {
00994 asection *sect;
00995 unsigned long crc32;
00996 bfd_byte *contents;
00997 int crc_offset;
00998 char *name;
00999
01000 BFD_ASSERT (abfd);
01001 BFD_ASSERT (crc32_out);
01002
01003 sect = bfd_get_section_by_name (abfd, GNU_DEBUGLINK);
01004
01005 if (sect == NULL)
01006 return NULL;
01007
01008 if (!bfd_malloc_and_get_section (abfd, sect, &contents))
01009 {
01010 if (contents != NULL)
01011 free (contents);
01012 return NULL;
01013 }
01014
01015
01016 name = (char *) contents;
01017 crc_offset = strlen (name) + 1;
01018 crc_offset = (crc_offset + 3) & ~3;
01019
01020 crc32 = bfd_get_32 (abfd, contents + crc_offset);
01021
01022 *crc32_out = crc32;
01023 return name;
01024 }
01025
01026
01027
01028
01029
01030
01031
01032
01033
01034
01035
01036
01037
01038
01039 static bfd_boolean
01040 separate_debug_file_exists (const char *name, const unsigned long crc)
01041 {
01042 static unsigned char buffer [8 * 1024];
01043 unsigned long file_crc = 0;
01044 int fd;
01045 bfd_size_type count;
01046
01047 BFD_ASSERT (name);
01048
01049 fd = open (name, O_RDONLY);
01050 if (fd < 0)
01051 return FALSE;
01052
01053 while ((count = read (fd, buffer, sizeof (buffer))) > 0)
01054 file_crc = bfd_calc_gnu_debuglink_crc32 (file_crc, buffer, count);
01055
01056 close (fd);
01057
01058 return crc == file_crc;
01059 }
01060
01061
01062
01063
01064
01065
01066
01067
01068
01069
01070
01071
01072
01073
01074
01075
01076
01077
01078 static char *
01079 find_separate_debug_file (bfd *abfd, const char *debug_file_directory)
01080 {
01081 char *basename;
01082 char *dir;
01083 char *debugfile;
01084 unsigned long crc32;
01085 int i;
01086
01087 BFD_ASSERT (abfd);
01088 if (debug_file_directory == NULL)
01089 debug_file_directory = ".";
01090
01091
01092 if (! abfd->filename)
01093 return NULL;
01094
01095 basename = get_debug_link_info (abfd, & crc32);
01096 if (basename == NULL)
01097 return NULL;
01098
01099 if (strlen (basename) < 1)
01100 {
01101 free (basename);
01102 return NULL;
01103 }
01104
01105 dir = strdup (abfd->filename);
01106 if (dir == NULL)
01107 {
01108 free (basename);
01109 return NULL;
01110 }
01111 BFD_ASSERT (strlen (dir) != 0);
01112
01113
01114 for (i = strlen (dir) - 1; i >= 0; i--)
01115 if (IS_DIR_SEPARATOR (dir[i]))
01116 break;
01117
01118 dir[i + 1] = '\0';
01119 BFD_ASSERT (dir[i] == '/' || dir[0] == '\0');
01120
01121 debugfile = malloc (strlen (debug_file_directory) + 1
01122 + strlen (dir)
01123 + strlen (".debug/")
01124 + strlen (basename)
01125 + 1);
01126 if (debugfile == NULL)
01127 {
01128 free (basename);
01129 free (dir);
01130 return NULL;
01131 }
01132
01133
01134 strcpy (debugfile, dir);
01135 strcat (debugfile, basename);
01136
01137 if (separate_debug_file_exists (debugfile, crc32))
01138 {
01139 free (basename);
01140 free (dir);
01141 return debugfile;
01142 }
01143
01144
01145 strcpy (debugfile, dir);
01146 strcat (debugfile, ".debug/");
01147 strcat (debugfile, basename);
01148
01149 if (separate_debug_file_exists (debugfile, crc32))
01150 {
01151 free (basename);
01152 free (dir);
01153 return debugfile;
01154 }
01155
01156
01157 strcpy (debugfile, debug_file_directory);
01158 i = strlen (debug_file_directory) - 1;
01159 if (i > 0
01160 && debug_file_directory[i] != '/'
01161 && dir[0] != '/')
01162 strcat (debugfile, "/");
01163 strcat (debugfile, dir);
01164 strcat (debugfile, basename);
01165
01166 if (separate_debug_file_exists (debugfile, crc32))
01167 {
01168 free (basename);
01169 free (dir);
01170 return debugfile;
01171 }
01172
01173 free (debugfile);
01174 free (basename);
01175 free (dir);
01176 return NULL;
01177 }
01178
01179
01180
01181
01182
01183
01184
01185
01186
01187
01188
01189
01190
01191
01192
01193
01194
01195
01196
01197
01198
01199
01200
01201
01202
01203
01204
01205
01206 char *
01207 bfd_follow_gnu_debuglink (bfd *abfd, const char *dir)
01208 {
01209 return find_separate_debug_file (abfd, dir);
01210 }
01211
01212
01213
01214
01215
01216
01217
01218
01219
01220
01221
01222
01223
01224
01225
01226
01227
01228
01229
01230 asection *
01231 bfd_create_gnu_debuglink_section (bfd *abfd, const char *filename)
01232 {
01233 asection *sect;
01234 bfd_size_type debuglink_size;
01235
01236 if (abfd == NULL || filename == NULL)
01237 {
01238 bfd_set_error (bfd_error_invalid_operation);
01239 return NULL;
01240 }
01241
01242
01243 filename = lbasename (filename);
01244
01245 sect = bfd_get_section_by_name (abfd, GNU_DEBUGLINK);
01246 if (sect)
01247 {
01248
01249 bfd_set_error (bfd_error_invalid_operation);
01250 return NULL;
01251 }
01252
01253 sect = bfd_make_section (abfd, GNU_DEBUGLINK);
01254 if (sect == NULL)
01255 return NULL;
01256
01257 if (! bfd_set_section_flags (abfd, sect,
01258 SEC_HAS_CONTENTS | SEC_READONLY | SEC_DEBUGGING))
01259
01260 return NULL;
01261
01262
01263 debuglink_size = strlen (filename) + 1;
01264 debuglink_size += 3;
01265 debuglink_size &= ~3;
01266 debuglink_size += 4;
01267
01268 if (! bfd_set_section_size (abfd, sect, debuglink_size))
01269
01270 return NULL;
01271
01272 return sect;
01273 }
01274
01275
01276
01277
01278
01279
01280
01281
01282
01283
01284
01285
01286
01287
01288
01289
01290
01291
01292
01293
01294
01295
01296 bfd_boolean
01297 bfd_fill_in_gnu_debuglink_section (bfd *abfd,
01298 struct bfd_section *sect,
01299 const char *filename)
01300 {
01301 bfd_size_type debuglink_size;
01302 unsigned long crc32;
01303 char * contents;
01304 bfd_size_type crc_offset;
01305 FILE * handle;
01306 static unsigned char buffer[8 * 1024];
01307 size_t count;
01308
01309 if (abfd == NULL || sect == NULL || filename == NULL)
01310 {
01311 bfd_set_error (bfd_error_invalid_operation);
01312 return FALSE;
01313 }
01314
01315
01316
01317
01318
01319
01320
01321 handle = fopen (filename, FOPEN_RB);
01322 if (handle == NULL)
01323 {
01324 bfd_set_error (bfd_error_system_call);
01325 return FALSE;
01326 }
01327
01328 crc32 = 0;
01329 while ((count = fread (buffer, 1, sizeof buffer, handle)) > 0)
01330 crc32 = bfd_calc_gnu_debuglink_crc32 (crc32, buffer, count);
01331 fclose (handle);
01332
01333
01334
01335 filename = lbasename (filename);
01336
01337 debuglink_size = strlen (filename) + 1;
01338 debuglink_size += 3;
01339 debuglink_size &= ~3;
01340 debuglink_size += 4;
01341
01342 contents = malloc (debuglink_size);
01343 if (contents == NULL)
01344 {
01345
01346 bfd_set_error (bfd_error_no_memory);
01347 return FALSE;
01348 }
01349
01350 strcpy (contents, filename);
01351 crc_offset = debuglink_size - 4;
01352
01353 bfd_put_32 (abfd, crc32, contents + crc_offset);
01354
01355 if (! bfd_set_section_contents (abfd, sect, contents, 0, debuglink_size))
01356 {
01357
01358 free (contents);
01359 return FALSE;
01360 }
01361
01362 return TRUE;
01363 }