coffgen.c 86 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057
  1. /* Support for the generic parts of COFF, for BFD.
  2. Copyright (C) 1990-2015 Free Software Foundation, Inc.
  3. Written by Cygnus Support.
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /* Most of this hacked by Steve Chamberlain, sac@cygnus.com.
  18. Split out of coffcode.h by Ian Taylor, ian@cygnus.com. */
  19. /* This file contains COFF code that is not dependent on any
  20. particular COFF target. There is only one version of this file in
  21. libbfd.a, so no target specific code may be put in here. Or, to
  22. put it another way,
  23. ********** DO NOT PUT TARGET SPECIFIC CODE IN THIS FILE **********
  24. If you need to add some target specific behaviour, add a new hook
  25. function to bfd_coff_backend_data.
  26. Some of these functions are also called by the ECOFF routines.
  27. Those functions may not use any COFF specific information, such as
  28. coff_data (abfd). */
  29. #include "sysdep.h"
  30. #include "bfd.h"
  31. #include "libbfd.h"
  32. #include "coff/internal.h"
  33. #include "libcoff.h"
  34. /* Take a section header read from a coff file (in HOST byte order),
  35. and make a BFD "section" out of it. This is used by ECOFF. */
  36. static bfd_boolean
  37. make_a_section_from_file (bfd *abfd,
  38. struct internal_scnhdr *hdr,
  39. unsigned int target_index)
  40. {
  41. asection *return_section;
  42. char *name;
  43. bfd_boolean result = TRUE;
  44. flagword flags;
  45. name = NULL;
  46. /* Handle long section names as in PE. On reading, we want to
  47. accept long names if the format permits them at all, regardless
  48. of the current state of the flag that dictates if we would generate
  49. them in outputs; this construct checks if that is the case by
  50. attempting to set the flag, without changing its state; the call
  51. will fail for formats that do not support long names at all. */
  52. if (bfd_coff_set_long_section_names (abfd, bfd_coff_long_section_names (abfd))
  53. && hdr->s_name[0] == '/')
  54. {
  55. char buf[SCNNMLEN];
  56. long strindex;
  57. char *p;
  58. const char *strings;
  59. /* Flag that this BFD uses long names, even though the format might
  60. expect them to be off by default. This won't directly affect the
  61. format of any output BFD created from this one, but the information
  62. can be used to decide what to do. */
  63. bfd_coff_set_long_section_names (abfd, TRUE);
  64. memcpy (buf, hdr->s_name + 1, SCNNMLEN - 1);
  65. buf[SCNNMLEN - 1] = '\0';
  66. strindex = strtol (buf, &p, 10);
  67. if (*p == '\0' && strindex >= 0)
  68. {
  69. strings = _bfd_coff_read_string_table (abfd);
  70. if (strings == NULL)
  71. return FALSE;
  72. if ((bfd_size_type)(strindex + 2) >= obj_coff_strings_len (abfd))
  73. return FALSE;
  74. strings += strindex;
  75. name = (char *) bfd_alloc (abfd,
  76. (bfd_size_type) strlen (strings) + 1 + 1);
  77. if (name == NULL)
  78. return FALSE;
  79. strcpy (name, strings);
  80. }
  81. }
  82. if (name == NULL)
  83. {
  84. /* Assorted wastage to null-terminate the name, thanks AT&T! */
  85. name = (char *) bfd_alloc (abfd,
  86. (bfd_size_type) sizeof (hdr->s_name) + 1 + 1);
  87. if (name == NULL)
  88. return FALSE;
  89. strncpy (name, (char *) &hdr->s_name[0], sizeof (hdr->s_name));
  90. name[sizeof (hdr->s_name)] = 0;
  91. }
  92. return_section = bfd_make_section_anyway (abfd, name);
  93. if (return_section == NULL)
  94. return FALSE;
  95. return_section->vma = hdr->s_vaddr;
  96. return_section->lma = hdr->s_paddr;
  97. return_section->size = hdr->s_size;
  98. return_section->filepos = hdr->s_scnptr;
  99. return_section->rel_filepos = hdr->s_relptr;
  100. return_section->reloc_count = hdr->s_nreloc;
  101. bfd_coff_set_alignment_hook (abfd, return_section, hdr);
  102. return_section->line_filepos = hdr->s_lnnoptr;
  103. return_section->lineno_count = hdr->s_nlnno;
  104. return_section->userdata = NULL;
  105. return_section->next = NULL;
  106. return_section->target_index = target_index;
  107. if (! bfd_coff_styp_to_sec_flags_hook (abfd, hdr, name, return_section,
  108. & flags))
  109. result = FALSE;
  110. return_section->flags = flags;
  111. /* At least on i386-coff, the line number count for a shared library
  112. section must be ignored. */
  113. if ((return_section->flags & SEC_COFF_SHARED_LIBRARY) != 0)
  114. return_section->lineno_count = 0;
  115. if (hdr->s_nreloc != 0)
  116. return_section->flags |= SEC_RELOC;
  117. /* FIXME: should this check 'hdr->s_size > 0'. */
  118. if (hdr->s_scnptr != 0)
  119. return_section->flags |= SEC_HAS_CONTENTS;
  120. /* Compress/decompress DWARF debug sections with names: .debug_* and
  121. .zdebug_*, after the section flags is set. */
  122. if ((flags & SEC_DEBUGGING)
  123. && strlen (name) > 7
  124. && ((name[1] == 'd' && name[6] == '_')
  125. || (strlen (name) > 8 && name[1] == 'z' && name[7] == '_')))
  126. {
  127. enum { nothing, compress, decompress } action = nothing;
  128. char *new_name = NULL;
  129. if (bfd_is_section_compressed (abfd, return_section))
  130. {
  131. /* Compressed section. Check if we should decompress. */
  132. if ((abfd->flags & BFD_DECOMPRESS))
  133. action = decompress;
  134. }
  135. else if (!bfd_is_section_compressed (abfd, return_section))
  136. {
  137. /* Normal section. Check if we should compress. */
  138. if ((abfd->flags & BFD_COMPRESS) && return_section->size != 0)
  139. action = compress;
  140. }
  141. switch (action)
  142. {
  143. case nothing:
  144. break;
  145. case compress:
  146. if (!bfd_init_section_compress_status (abfd, return_section))
  147. {
  148. (*_bfd_error_handler)
  149. (_("%B: unable to initialize compress status for section %s"),
  150. abfd, name);
  151. return FALSE;
  152. }
  153. if (return_section->compress_status == COMPRESS_SECTION_DONE)
  154. {
  155. if (name[1] != 'z')
  156. {
  157. unsigned int len = strlen (name);
  158. new_name = bfd_alloc (abfd, len + 2);
  159. if (new_name == NULL)
  160. return FALSE;
  161. new_name[0] = '.';
  162. new_name[1] = 'z';
  163. memcpy (new_name + 2, name + 1, len);
  164. }
  165. }
  166. break;
  167. case decompress:
  168. if (!bfd_init_section_decompress_status (abfd, return_section))
  169. {
  170. (*_bfd_error_handler)
  171. (_("%B: unable to initialize decompress status for section %s"),
  172. abfd, name);
  173. return FALSE;
  174. }
  175. if (name[1] == 'z')
  176. {
  177. unsigned int len = strlen (name);
  178. new_name = bfd_alloc (abfd, len);
  179. if (new_name == NULL)
  180. return FALSE;
  181. new_name[0] = '.';
  182. memcpy (new_name + 1, name + 2, len - 1);
  183. }
  184. break;
  185. }
  186. if (new_name != NULL)
  187. bfd_rename_section (abfd, return_section, new_name);
  188. }
  189. return result;
  190. }
  191. /* Read in a COFF object and make it into a BFD. This is used by
  192. ECOFF as well. */
  193. const bfd_target *
  194. coff_real_object_p (bfd *,
  195. unsigned,
  196. struct internal_filehdr *,
  197. struct internal_aouthdr *);
  198. const bfd_target *
  199. coff_real_object_p (bfd *abfd,
  200. unsigned nscns,
  201. struct internal_filehdr *internal_f,
  202. struct internal_aouthdr *internal_a)
  203. {
  204. flagword oflags = abfd->flags;
  205. bfd_vma ostart = bfd_get_start_address (abfd);
  206. void * tdata;
  207. void * tdata_save;
  208. bfd_size_type readsize; /* Length of file_info. */
  209. unsigned int scnhsz;
  210. char *external_sections;
  211. if (!(internal_f->f_flags & F_RELFLG))
  212. abfd->flags |= HAS_RELOC;
  213. if ((internal_f->f_flags & F_EXEC))
  214. abfd->flags |= EXEC_P;
  215. if (!(internal_f->f_flags & F_LNNO))
  216. abfd->flags |= HAS_LINENO;
  217. if (!(internal_f->f_flags & F_LSYMS))
  218. abfd->flags |= HAS_LOCALS;
  219. /* FIXME: How can we set D_PAGED correctly? */
  220. if ((internal_f->f_flags & F_EXEC) != 0)
  221. abfd->flags |= D_PAGED;
  222. bfd_get_symcount (abfd) = internal_f->f_nsyms;
  223. if (internal_f->f_nsyms)
  224. abfd->flags |= HAS_SYMS;
  225. if (internal_a != (struct internal_aouthdr *) NULL)
  226. bfd_get_start_address (abfd) = internal_a->entry;
  227. else
  228. bfd_get_start_address (abfd) = 0;
  229. /* Set up the tdata area. ECOFF uses its own routine, and overrides
  230. abfd->flags. */
  231. tdata_save = abfd->tdata.any;
  232. tdata = bfd_coff_mkobject_hook (abfd, (void *) internal_f, (void *) internal_a);
  233. if (tdata == NULL)
  234. goto fail2;
  235. scnhsz = bfd_coff_scnhsz (abfd);
  236. readsize = (bfd_size_type) nscns * scnhsz;
  237. external_sections = (char *) bfd_alloc (abfd, readsize);
  238. if (!external_sections)
  239. goto fail;
  240. if (bfd_bread ((void *) external_sections, readsize, abfd) != readsize)
  241. goto fail;
  242. /* Set the arch/mach *before* swapping in sections; section header swapping
  243. may depend on arch/mach info. */
  244. if (! bfd_coff_set_arch_mach_hook (abfd, (void *) internal_f))
  245. goto fail;
  246. /* Now copy data as required; construct all asections etc. */
  247. if (nscns != 0)
  248. {
  249. unsigned int i;
  250. for (i = 0; i < nscns; i++)
  251. {
  252. struct internal_scnhdr tmp;
  253. bfd_coff_swap_scnhdr_in (abfd,
  254. (void *) (external_sections + i * scnhsz),
  255. (void *) & tmp);
  256. if (! make_a_section_from_file (abfd, &tmp, i + 1))
  257. goto fail;
  258. }
  259. }
  260. return abfd->xvec;
  261. fail:
  262. bfd_release (abfd, tdata);
  263. fail2:
  264. abfd->tdata.any = tdata_save;
  265. abfd->flags = oflags;
  266. bfd_get_start_address (abfd) = ostart;
  267. return (const bfd_target *) NULL;
  268. }
  269. /* Turn a COFF file into a BFD, but fail with bfd_error_wrong_format if it is
  270. not a COFF file. This is also used by ECOFF. */
  271. const bfd_target *
  272. coff_object_p (bfd *abfd)
  273. {
  274. bfd_size_type filhsz;
  275. bfd_size_type aoutsz;
  276. unsigned int nscns;
  277. void * filehdr;
  278. struct internal_filehdr internal_f;
  279. struct internal_aouthdr internal_a;
  280. /* Figure out how much to read. */
  281. filhsz = bfd_coff_filhsz (abfd);
  282. aoutsz = bfd_coff_aoutsz (abfd);
  283. filehdr = bfd_alloc (abfd, filhsz);
  284. if (filehdr == NULL)
  285. return NULL;
  286. if (bfd_bread (filehdr, filhsz, abfd) != filhsz)
  287. {
  288. if (bfd_get_error () != bfd_error_system_call)
  289. bfd_set_error (bfd_error_wrong_format);
  290. bfd_release (abfd, filehdr);
  291. return NULL;
  292. }
  293. bfd_coff_swap_filehdr_in (abfd, filehdr, &internal_f);
  294. bfd_release (abfd, filehdr);
  295. /* The XCOFF format has two sizes for the f_opthdr. SMALL_AOUTSZ
  296. (less than aoutsz) used in object files and AOUTSZ (equal to
  297. aoutsz) in executables. The bfd_coff_swap_aouthdr_in function
  298. expects this header to be aoutsz bytes in length, so we use that
  299. value in the call to bfd_alloc below. But we must be careful to
  300. only read in f_opthdr bytes in the call to bfd_bread. We should
  301. also attempt to catch corrupt or non-COFF binaries with a strange
  302. value for f_opthdr. */
  303. if (! bfd_coff_bad_format_hook (abfd, &internal_f)
  304. || internal_f.f_opthdr > aoutsz)
  305. {
  306. bfd_set_error (bfd_error_wrong_format);
  307. return NULL;
  308. }
  309. nscns = internal_f.f_nscns;
  310. if (internal_f.f_opthdr)
  311. {
  312. void * opthdr;
  313. opthdr = bfd_alloc (abfd, aoutsz);
  314. if (opthdr == NULL)
  315. return NULL;
  316. if (bfd_bread (opthdr, (bfd_size_type) internal_f.f_opthdr, abfd)
  317. != internal_f.f_opthdr)
  318. {
  319. bfd_release (abfd, opthdr);
  320. return NULL;
  321. }
  322. /* PR 17512: file: 11056-1136-0.004. */
  323. if (internal_f.f_opthdr < aoutsz)
  324. memset (((char *) opthdr) + internal_f.f_opthdr, 0, aoutsz - internal_f.f_opthdr);
  325. bfd_coff_swap_aouthdr_in (abfd, opthdr, (void *) &internal_a);
  326. bfd_release (abfd, opthdr);
  327. }
  328. return coff_real_object_p (abfd, nscns, &internal_f,
  329. (internal_f.f_opthdr != 0
  330. ? &internal_a
  331. : (struct internal_aouthdr *) NULL));
  332. }
  333. /* Get the BFD section from a COFF symbol section number. */
  334. asection *
  335. coff_section_from_bfd_index (bfd *abfd, int section_index)
  336. {
  337. struct bfd_section *answer = abfd->sections;
  338. if (section_index == N_ABS)
  339. return bfd_abs_section_ptr;
  340. if (section_index == N_UNDEF)
  341. return bfd_und_section_ptr;
  342. if (section_index == N_DEBUG)
  343. return bfd_abs_section_ptr;
  344. while (answer)
  345. {
  346. if (answer->target_index == section_index)
  347. return answer;
  348. answer = answer->next;
  349. }
  350. /* We should not reach this point, but the SCO 3.2v4 /lib/libc_s.a
  351. has a bad symbol table in biglitpow.o. */
  352. return bfd_und_section_ptr;
  353. }
  354. /* Get the upper bound of a COFF symbol table. */
  355. long
  356. coff_get_symtab_upper_bound (bfd *abfd)
  357. {
  358. if (!bfd_coff_slurp_symbol_table (abfd))
  359. return -1;
  360. return (bfd_get_symcount (abfd) + 1) * (sizeof (coff_symbol_type *));
  361. }
  362. /* Canonicalize a COFF symbol table. */
  363. long
  364. coff_canonicalize_symtab (bfd *abfd, asymbol **alocation)
  365. {
  366. unsigned int counter;
  367. coff_symbol_type *symbase;
  368. coff_symbol_type **location = (coff_symbol_type **) alocation;
  369. if (!bfd_coff_slurp_symbol_table (abfd))
  370. return -1;
  371. symbase = obj_symbols (abfd);
  372. counter = bfd_get_symcount (abfd);
  373. while (counter-- > 0)
  374. *location++ = symbase++;
  375. *location = NULL;
  376. return bfd_get_symcount (abfd);
  377. }
  378. /* Get the name of a symbol. The caller must pass in a buffer of size
  379. >= SYMNMLEN + 1. */
  380. const char *
  381. _bfd_coff_internal_syment_name (bfd *abfd,
  382. const struct internal_syment *sym,
  383. char *buf)
  384. {
  385. /* FIXME: It's not clear this will work correctly if sizeof
  386. (_n_zeroes) != 4. */
  387. if (sym->_n._n_n._n_zeroes != 0
  388. || sym->_n._n_n._n_offset == 0)
  389. {
  390. memcpy (buf, sym->_n._n_name, SYMNMLEN);
  391. buf[SYMNMLEN] = '\0';
  392. return buf;
  393. }
  394. else
  395. {
  396. const char *strings;
  397. BFD_ASSERT (sym->_n._n_n._n_offset >= STRING_SIZE_SIZE);
  398. strings = obj_coff_strings (abfd);
  399. if (strings == NULL)
  400. {
  401. strings = _bfd_coff_read_string_table (abfd);
  402. if (strings == NULL)
  403. return NULL;
  404. }
  405. /* PR 17910: Only check for string overflow if the length has been set.
  406. Some DLLs, eg those produced by Visual Studio, may not set the length field. */
  407. if (obj_coff_strings_len (abfd) > 0
  408. && sym->_n._n_n._n_offset >= obj_coff_strings_len (abfd))
  409. return NULL;
  410. return strings + sym->_n._n_n._n_offset;
  411. }
  412. }
  413. /* Read in and swap the relocs. This returns a buffer holding the
  414. relocs for section SEC in file ABFD. If CACHE is TRUE and
  415. INTERNAL_RELOCS is NULL, the relocs read in will be saved in case
  416. the function is called again. If EXTERNAL_RELOCS is not NULL, it
  417. is a buffer large enough to hold the unswapped relocs. If
  418. INTERNAL_RELOCS is not NULL, it is a buffer large enough to hold
  419. the swapped relocs. If REQUIRE_INTERNAL is TRUE, then the return
  420. value must be INTERNAL_RELOCS. The function returns NULL on error. */
  421. struct internal_reloc *
  422. _bfd_coff_read_internal_relocs (bfd *abfd,
  423. asection *sec,
  424. bfd_boolean cache,
  425. bfd_byte *external_relocs,
  426. bfd_boolean require_internal,
  427. struct internal_reloc *internal_relocs)
  428. {
  429. bfd_size_type relsz;
  430. bfd_byte *free_external = NULL;
  431. struct internal_reloc *free_internal = NULL;
  432. bfd_byte *erel;
  433. bfd_byte *erel_end;
  434. struct internal_reloc *irel;
  435. bfd_size_type amt;
  436. if (sec->reloc_count == 0)
  437. return internal_relocs; /* Nothing to do. */
  438. if (coff_section_data (abfd, sec) != NULL
  439. && coff_section_data (abfd, sec)->relocs != NULL)
  440. {
  441. if (! require_internal)
  442. return coff_section_data (abfd, sec)->relocs;
  443. memcpy (internal_relocs, coff_section_data (abfd, sec)->relocs,
  444. sec->reloc_count * sizeof (struct internal_reloc));
  445. return internal_relocs;
  446. }
  447. relsz = bfd_coff_relsz (abfd);
  448. amt = sec->reloc_count * relsz;
  449. if (external_relocs == NULL)
  450. {
  451. free_external = (bfd_byte *) bfd_malloc (amt);
  452. if (free_external == NULL)
  453. goto error_return;
  454. external_relocs = free_external;
  455. }
  456. if (bfd_seek (abfd, sec->rel_filepos, SEEK_SET) != 0
  457. || bfd_bread (external_relocs, amt, abfd) != amt)
  458. goto error_return;
  459. if (internal_relocs == NULL)
  460. {
  461. amt = sec->reloc_count;
  462. amt *= sizeof (struct internal_reloc);
  463. free_internal = (struct internal_reloc *) bfd_malloc (amt);
  464. if (free_internal == NULL)
  465. goto error_return;
  466. internal_relocs = free_internal;
  467. }
  468. /* Swap in the relocs. */
  469. erel = external_relocs;
  470. erel_end = erel + relsz * sec->reloc_count;
  471. irel = internal_relocs;
  472. for (; erel < erel_end; erel += relsz, irel++)
  473. bfd_coff_swap_reloc_in (abfd, (void *) erel, (void *) irel);
  474. if (free_external != NULL)
  475. {
  476. free (free_external);
  477. free_external = NULL;
  478. }
  479. if (cache && free_internal != NULL)
  480. {
  481. if (coff_section_data (abfd, sec) == NULL)
  482. {
  483. amt = sizeof (struct coff_section_tdata);
  484. sec->used_by_bfd = bfd_zalloc (abfd, amt);
  485. if (sec->used_by_bfd == NULL)
  486. goto error_return;
  487. coff_section_data (abfd, sec)->contents = NULL;
  488. }
  489. coff_section_data (abfd, sec)->relocs = free_internal;
  490. }
  491. return internal_relocs;
  492. error_return:
  493. if (free_external != NULL)
  494. free (free_external);
  495. if (free_internal != NULL)
  496. free (free_internal);
  497. return NULL;
  498. }
  499. /* Set lineno_count for the output sections of a COFF file. */
  500. int
  501. coff_count_linenumbers (bfd *abfd)
  502. {
  503. unsigned int limit = bfd_get_symcount (abfd);
  504. unsigned int i;
  505. int total = 0;
  506. asymbol **p;
  507. asection *s;
  508. if (limit == 0)
  509. {
  510. /* This may be from the backend linker, in which case the
  511. lineno_count in the sections is correct. */
  512. for (s = abfd->sections; s != NULL; s = s->next)
  513. total += s->lineno_count;
  514. return total;
  515. }
  516. for (s = abfd->sections; s != NULL; s = s->next)
  517. BFD_ASSERT (s->lineno_count == 0);
  518. for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
  519. {
  520. asymbol *q_maybe = *p;
  521. if (bfd_family_coff (bfd_asymbol_bfd (q_maybe)))
  522. {
  523. coff_symbol_type *q = coffsymbol (q_maybe);
  524. /* The AIX 4.1 compiler can sometimes generate line numbers
  525. attached to debugging symbols. We try to simply ignore
  526. those here. */
  527. if (q->lineno != NULL
  528. && q->symbol.section->owner != NULL)
  529. {
  530. /* This symbol has line numbers. Increment the owning
  531. section's linenumber count. */
  532. alent *l = q->lineno;
  533. do
  534. {
  535. asection * sec = q->symbol.section->output_section;
  536. /* Do not try to update fields in read-only sections. */
  537. if (! bfd_is_const_section (sec))
  538. sec->lineno_count ++;
  539. ++total;
  540. ++l;
  541. }
  542. while (l->line_number != 0);
  543. }
  544. }
  545. }
  546. return total;
  547. }
  548. static void
  549. fixup_symbol_value (bfd *abfd,
  550. coff_symbol_type *coff_symbol_ptr,
  551. struct internal_syment *syment)
  552. {
  553. /* Normalize the symbol flags. */
  554. if (coff_symbol_ptr->symbol.section
  555. && bfd_is_com_section (coff_symbol_ptr->symbol.section))
  556. {
  557. /* A common symbol is undefined with a value. */
  558. syment->n_scnum = N_UNDEF;
  559. syment->n_value = coff_symbol_ptr->symbol.value;
  560. }
  561. else if ((coff_symbol_ptr->symbol.flags & BSF_DEBUGGING) != 0
  562. && (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING_RELOC) == 0)
  563. {
  564. syment->n_value = coff_symbol_ptr->symbol.value;
  565. }
  566. else if (bfd_is_und_section (coff_symbol_ptr->symbol.section))
  567. {
  568. syment->n_scnum = N_UNDEF;
  569. syment->n_value = 0;
  570. }
  571. /* FIXME: Do we need to handle the absolute section here? */
  572. else
  573. {
  574. if (coff_symbol_ptr->symbol.section)
  575. {
  576. syment->n_scnum =
  577. coff_symbol_ptr->symbol.section->output_section->target_index;
  578. syment->n_value = (coff_symbol_ptr->symbol.value
  579. + coff_symbol_ptr->symbol.section->output_offset);
  580. if (! obj_pe (abfd))
  581. {
  582. syment->n_value += (syment->n_sclass == C_STATLAB)
  583. ? coff_symbol_ptr->symbol.section->output_section->lma
  584. : coff_symbol_ptr->symbol.section->output_section->vma;
  585. }
  586. }
  587. else
  588. {
  589. BFD_ASSERT (0);
  590. /* This can happen, but I don't know why yet (steve@cygnus.com) */
  591. syment->n_scnum = N_ABS;
  592. syment->n_value = coff_symbol_ptr->symbol.value;
  593. }
  594. }
  595. }
  596. /* Run through all the symbols in the symbol table and work out what
  597. their indexes into the symbol table will be when output.
  598. Coff requires that each C_FILE symbol points to the next one in the
  599. chain, and that the last one points to the first external symbol. We
  600. do that here too. */
  601. bfd_boolean
  602. coff_renumber_symbols (bfd *bfd_ptr, int *first_undef)
  603. {
  604. unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
  605. asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
  606. unsigned int native_index = 0;
  607. struct internal_syment *last_file = NULL;
  608. unsigned int symbol_index;
  609. /* COFF demands that undefined symbols come after all other symbols.
  610. Since we don't need to impose this extra knowledge on all our
  611. client programs, deal with that here. Sort the symbol table;
  612. just move the undefined symbols to the end, leaving the rest
  613. alone. The O'Reilly book says that defined global symbols come
  614. at the end before the undefined symbols, so we do that here as
  615. well. */
  616. /* @@ Do we have some condition we could test for, so we don't always
  617. have to do this? I don't think relocatability is quite right, but
  618. I'm not certain. [raeburn:19920508.1711EST] */
  619. {
  620. asymbol **newsyms;
  621. unsigned int i;
  622. bfd_size_type amt;
  623. amt = sizeof (asymbol *) * ((bfd_size_type) symbol_count + 1);
  624. newsyms = (asymbol **) bfd_alloc (bfd_ptr, amt);
  625. if (!newsyms)
  626. return FALSE;
  627. bfd_ptr->outsymbols = newsyms;
  628. for (i = 0; i < symbol_count; i++)
  629. if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) != 0
  630. || (!bfd_is_und_section (symbol_ptr_ptr[i]->section)
  631. && !bfd_is_com_section (symbol_ptr_ptr[i]->section)
  632. && ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) != 0
  633. || ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
  634. == 0))))
  635. *newsyms++ = symbol_ptr_ptr[i];
  636. for (i = 0; i < symbol_count; i++)
  637. if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
  638. && !bfd_is_und_section (symbol_ptr_ptr[i]->section)
  639. && (bfd_is_com_section (symbol_ptr_ptr[i]->section)
  640. || ((symbol_ptr_ptr[i]->flags & BSF_FUNCTION) == 0
  641. && ((symbol_ptr_ptr[i]->flags & (BSF_GLOBAL | BSF_WEAK))
  642. != 0))))
  643. *newsyms++ = symbol_ptr_ptr[i];
  644. *first_undef = newsyms - bfd_ptr->outsymbols;
  645. for (i = 0; i < symbol_count; i++)
  646. if ((symbol_ptr_ptr[i]->flags & BSF_NOT_AT_END) == 0
  647. && bfd_is_und_section (symbol_ptr_ptr[i]->section))
  648. *newsyms++ = symbol_ptr_ptr[i];
  649. *newsyms = (asymbol *) NULL;
  650. symbol_ptr_ptr = bfd_ptr->outsymbols;
  651. }
  652. for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
  653. {
  654. coff_symbol_type *coff_symbol_ptr;
  655. coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
  656. symbol_ptr_ptr[symbol_index]->udata.i = symbol_index;
  657. if (coff_symbol_ptr && coff_symbol_ptr->native)
  658. {
  659. combined_entry_type *s = coff_symbol_ptr->native;
  660. int i;
  661. BFD_ASSERT (s->is_sym);
  662. if (s->u.syment.n_sclass == C_FILE)
  663. {
  664. if (last_file != NULL)
  665. last_file->n_value = native_index;
  666. last_file = &(s->u.syment);
  667. }
  668. else
  669. /* Modify the symbol values according to their section and
  670. type. */
  671. fixup_symbol_value (bfd_ptr, coff_symbol_ptr, &(s->u.syment));
  672. for (i = 0; i < s->u.syment.n_numaux + 1; i++)
  673. s[i].offset = native_index++;
  674. }
  675. else
  676. native_index++;
  677. }
  678. obj_conv_table_size (bfd_ptr) = native_index;
  679. return TRUE;
  680. }
  681. /* Run thorough the symbol table again, and fix it so that all
  682. pointers to entries are changed to the entries' index in the output
  683. symbol table. */
  684. void
  685. coff_mangle_symbols (bfd *bfd_ptr)
  686. {
  687. unsigned int symbol_count = bfd_get_symcount (bfd_ptr);
  688. asymbol **symbol_ptr_ptr = bfd_ptr->outsymbols;
  689. unsigned int symbol_index;
  690. for (symbol_index = 0; symbol_index < symbol_count; symbol_index++)
  691. {
  692. coff_symbol_type *coff_symbol_ptr;
  693. coff_symbol_ptr = coff_symbol_from (symbol_ptr_ptr[symbol_index]);
  694. if (coff_symbol_ptr && coff_symbol_ptr->native)
  695. {
  696. int i;
  697. combined_entry_type *s = coff_symbol_ptr->native;
  698. BFD_ASSERT (s->is_sym);
  699. if (s->fix_value)
  700. {
  701. /* FIXME: We should use a union here. */
  702. s->u.syment.n_value =
  703. (bfd_hostptr_t) ((combined_entry_type *)
  704. ((bfd_hostptr_t) s->u.syment.n_value))->offset;
  705. s->fix_value = 0;
  706. }
  707. if (s->fix_line)
  708. {
  709. /* The value is the offset into the line number entries
  710. for the symbol's section. On output, the symbol's
  711. section should be N_DEBUG. */
  712. s->u.syment.n_value =
  713. (coff_symbol_ptr->symbol.section->output_section->line_filepos
  714. + s->u.syment.n_value * bfd_coff_linesz (bfd_ptr));
  715. coff_symbol_ptr->symbol.section =
  716. coff_section_from_bfd_index (bfd_ptr, N_DEBUG);
  717. BFD_ASSERT (coff_symbol_ptr->symbol.flags & BSF_DEBUGGING);
  718. }
  719. for (i = 0; i < s->u.syment.n_numaux; i++)
  720. {
  721. combined_entry_type *a = s + i + 1;
  722. BFD_ASSERT (! a->is_sym);
  723. if (a->fix_tag)
  724. {
  725. a->u.auxent.x_sym.x_tagndx.l =
  726. a->u.auxent.x_sym.x_tagndx.p->offset;
  727. a->fix_tag = 0;
  728. }
  729. if (a->fix_end)
  730. {
  731. a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l =
  732. a->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p->offset;
  733. a->fix_end = 0;
  734. }
  735. if (a->fix_scnlen)
  736. {
  737. a->u.auxent.x_csect.x_scnlen.l =
  738. a->u.auxent.x_csect.x_scnlen.p->offset;
  739. a->fix_scnlen = 0;
  740. }
  741. }
  742. }
  743. }
  744. }
  745. static void
  746. coff_fix_symbol_name (bfd *abfd,
  747. asymbol *symbol,
  748. combined_entry_type *native,
  749. bfd_size_type *string_size_p,
  750. asection **debug_string_section_p,
  751. bfd_size_type *debug_string_size_p)
  752. {
  753. unsigned int name_length;
  754. union internal_auxent *auxent;
  755. char *name = (char *) (symbol->name);
  756. if (name == NULL)
  757. {
  758. /* COFF symbols always have names, so we'll make one up. */
  759. symbol->name = "strange";
  760. name = (char *) symbol->name;
  761. }
  762. name_length = strlen (name);
  763. BFD_ASSERT (native->is_sym);
  764. if (native->u.syment.n_sclass == C_FILE
  765. && native->u.syment.n_numaux > 0)
  766. {
  767. unsigned int filnmlen;
  768. if (bfd_coff_force_symnames_in_strings (abfd))
  769. {
  770. native->u.syment._n._n_n._n_offset =
  771. (*string_size_p + STRING_SIZE_SIZE);
  772. native->u.syment._n._n_n._n_zeroes = 0;
  773. *string_size_p += 6; /* strlen(".file") + 1 */
  774. }
  775. else
  776. strncpy (native->u.syment._n._n_name, ".file", SYMNMLEN);
  777. BFD_ASSERT (! (native + 1)->is_sym);
  778. auxent = &(native + 1)->u.auxent;
  779. filnmlen = bfd_coff_filnmlen (abfd);
  780. if (bfd_coff_long_filenames (abfd))
  781. {
  782. if (name_length <= filnmlen)
  783. strncpy (auxent->x_file.x_fname, name, filnmlen);
  784. else
  785. {
  786. auxent->x_file.x_n.x_offset = *string_size_p + STRING_SIZE_SIZE;
  787. auxent->x_file.x_n.x_zeroes = 0;
  788. *string_size_p += name_length + 1;
  789. }
  790. }
  791. else
  792. {
  793. strncpy (auxent->x_file.x_fname, name, filnmlen);
  794. if (name_length > filnmlen)
  795. name[filnmlen] = '\0';
  796. }
  797. }
  798. else
  799. {
  800. if (name_length <= SYMNMLEN && !bfd_coff_force_symnames_in_strings (abfd))
  801. /* This name will fit into the symbol neatly. */
  802. strncpy (native->u.syment._n._n_name, symbol->name, SYMNMLEN);
  803. else if (!bfd_coff_symname_in_debug (abfd, &native->u.syment))
  804. {
  805. native->u.syment._n._n_n._n_offset = (*string_size_p
  806. + STRING_SIZE_SIZE);
  807. native->u.syment._n._n_n._n_zeroes = 0;
  808. *string_size_p += name_length + 1;
  809. }
  810. else
  811. {
  812. file_ptr filepos;
  813. bfd_byte buf[4];
  814. int prefix_len = bfd_coff_debug_string_prefix_length (abfd);
  815. /* This name should be written into the .debug section. For
  816. some reason each name is preceded by a two byte length
  817. and also followed by a null byte. FIXME: We assume that
  818. the .debug section has already been created, and that it
  819. is large enough. */
  820. if (*debug_string_section_p == (asection *) NULL)
  821. *debug_string_section_p = bfd_get_section_by_name (abfd, ".debug");
  822. filepos = bfd_tell (abfd);
  823. if (prefix_len == 4)
  824. bfd_put_32 (abfd, (bfd_vma) (name_length + 1), buf);
  825. else
  826. bfd_put_16 (abfd, (bfd_vma) (name_length + 1), buf);
  827. if (!bfd_set_section_contents (abfd,
  828. *debug_string_section_p,
  829. (void *) buf,
  830. (file_ptr) *debug_string_size_p,
  831. (bfd_size_type) prefix_len)
  832. || !bfd_set_section_contents (abfd,
  833. *debug_string_section_p,
  834. (void *) symbol->name,
  835. (file_ptr) (*debug_string_size_p
  836. + prefix_len),
  837. (bfd_size_type) name_length + 1))
  838. abort ();
  839. if (bfd_seek (abfd, filepos, SEEK_SET) != 0)
  840. abort ();
  841. native->u.syment._n._n_n._n_offset =
  842. *debug_string_size_p + prefix_len;
  843. native->u.syment._n._n_n._n_zeroes = 0;
  844. *debug_string_size_p += name_length + 1 + prefix_len;
  845. }
  846. }
  847. }
  848. /* We need to keep track of the symbol index so that when we write out
  849. the relocs we can get the index for a symbol. This method is a
  850. hack. FIXME. */
  851. #define set_index(symbol, idx) ((symbol)->udata.i = (idx))
  852. /* Write a symbol out to a COFF file. */
  853. static bfd_boolean
  854. coff_write_symbol (bfd *abfd,
  855. asymbol *symbol,
  856. combined_entry_type *native,
  857. bfd_vma *written,
  858. bfd_size_type *string_size_p,
  859. asection **debug_string_section_p,
  860. bfd_size_type *debug_string_size_p)
  861. {
  862. unsigned int numaux = native->u.syment.n_numaux;
  863. int type = native->u.syment.n_type;
  864. int n_sclass = (int) native->u.syment.n_sclass;
  865. asection *output_section = symbol->section->output_section
  866. ? symbol->section->output_section
  867. : symbol->section;
  868. void * buf;
  869. bfd_size_type symesz;
  870. BFD_ASSERT (native->is_sym);
  871. if (native->u.syment.n_sclass == C_FILE)
  872. symbol->flags |= BSF_DEBUGGING;
  873. if (symbol->flags & BSF_DEBUGGING
  874. && bfd_is_abs_section (symbol->section))
  875. native->u.syment.n_scnum = N_DEBUG;
  876. else if (bfd_is_abs_section (symbol->section))
  877. native->u.syment.n_scnum = N_ABS;
  878. else if (bfd_is_und_section (symbol->section))
  879. native->u.syment.n_scnum = N_UNDEF;
  880. else
  881. native->u.syment.n_scnum =
  882. output_section->target_index;
  883. coff_fix_symbol_name (abfd, symbol, native, string_size_p,
  884. debug_string_section_p, debug_string_size_p);
  885. symesz = bfd_coff_symesz (abfd);
  886. buf = bfd_alloc (abfd, symesz);
  887. if (!buf)
  888. return FALSE;
  889. bfd_coff_swap_sym_out (abfd, &native->u.syment, buf);
  890. if (bfd_bwrite (buf, symesz, abfd) != symesz)
  891. return FALSE;
  892. bfd_release (abfd, buf);
  893. if (native->u.syment.n_numaux > 0)
  894. {
  895. bfd_size_type auxesz;
  896. unsigned int j;
  897. auxesz = bfd_coff_auxesz (abfd);
  898. buf = bfd_alloc (abfd, auxesz);
  899. if (!buf)
  900. return FALSE;
  901. for (j = 0; j < native->u.syment.n_numaux; j++)
  902. {
  903. BFD_ASSERT (! (native + j + 1)->is_sym);
  904. bfd_coff_swap_aux_out (abfd,
  905. &((native + j + 1)->u.auxent),
  906. type, n_sclass, (int) j,
  907. native->u.syment.n_numaux,
  908. buf);
  909. if (bfd_bwrite (buf, auxesz, abfd) != auxesz)
  910. return FALSE;
  911. }
  912. bfd_release (abfd, buf);
  913. }
  914. /* Store the index for use when we write out the relocs. */
  915. set_index (symbol, *written);
  916. *written += numaux + 1;
  917. return TRUE;
  918. }
  919. /* Write out a symbol to a COFF file that does not come from a COFF
  920. file originally. This symbol may have been created by the linker,
  921. or we may be linking a non COFF file to a COFF file. */
  922. bfd_boolean
  923. coff_write_alien_symbol (bfd *abfd,
  924. asymbol *symbol,
  925. struct internal_syment *isym,
  926. bfd_vma *written,
  927. bfd_size_type *string_size_p,
  928. asection **debug_string_section_p,
  929. bfd_size_type *debug_string_size_p)
  930. {
  931. combined_entry_type *native;
  932. combined_entry_type dummy[2];
  933. asection *output_section = symbol->section->output_section
  934. ? symbol->section->output_section
  935. : symbol->section;
  936. struct bfd_link_info *link_info = coff_data (abfd)->link_info;
  937. bfd_boolean ret;
  938. if ((!link_info || link_info->strip_discarded)
  939. && !bfd_is_abs_section (symbol->section)
  940. && symbol->section->output_section == bfd_abs_section_ptr)
  941. {
  942. symbol->name = "";
  943. if (isym != NULL)
  944. memset (isym, 0, sizeof (*isym));
  945. return TRUE;
  946. }
  947. native = dummy;
  948. native->is_sym = TRUE;
  949. native[1].is_sym = FALSE;
  950. native->u.syment.n_type = T_NULL;
  951. native->u.syment.n_flags = 0;
  952. native->u.syment.n_numaux = 0;
  953. if (bfd_is_und_section (symbol->section))
  954. {
  955. native->u.syment.n_scnum = N_UNDEF;
  956. native->u.syment.n_value = symbol->value;
  957. }
  958. else if (bfd_is_com_section (symbol->section))
  959. {
  960. native->u.syment.n_scnum = N_UNDEF;
  961. native->u.syment.n_value = symbol->value;
  962. }
  963. else if (symbol->flags & BSF_FILE)
  964. {
  965. native->u.syment.n_scnum = N_DEBUG;
  966. native->u.syment.n_numaux = 1;
  967. }
  968. else if (symbol->flags & BSF_DEBUGGING)
  969. {
  970. /* There isn't much point to writing out a debugging symbol
  971. unless we are prepared to convert it into COFF debugging
  972. format. So, we just ignore them. We must clobber the symbol
  973. name to keep it from being put in the string table. */
  974. symbol->name = "";
  975. if (isym != NULL)
  976. memset (isym, 0, sizeof (*isym));
  977. return TRUE;
  978. }
  979. else
  980. {
  981. native->u.syment.n_scnum = output_section->target_index;
  982. native->u.syment.n_value = (symbol->value
  983. + symbol->section->output_offset);
  984. if (! obj_pe (abfd))
  985. native->u.syment.n_value += output_section->vma;
  986. /* Copy the any flags from the file header into the symbol.
  987. FIXME: Why? */
  988. {
  989. coff_symbol_type *c = coff_symbol_from (symbol);
  990. if (c != (coff_symbol_type *) NULL)
  991. native->u.syment.n_flags = bfd_asymbol_bfd (&c->symbol)->flags;
  992. }
  993. }
  994. native->u.syment.n_type = 0;
  995. if (symbol->flags & BSF_FILE)
  996. native->u.syment.n_sclass = C_FILE;
  997. else if (symbol->flags & BSF_LOCAL)
  998. native->u.syment.n_sclass = C_STAT;
  999. else if (symbol->flags & BSF_WEAK)
  1000. native->u.syment.n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
  1001. else
  1002. native->u.syment.n_sclass = C_EXT;
  1003. ret = coff_write_symbol (abfd, symbol, native, written, string_size_p,
  1004. debug_string_section_p, debug_string_size_p);
  1005. if (isym != NULL)
  1006. *isym = native->u.syment;
  1007. return ret;
  1008. }
  1009. /* Write a native symbol to a COFF file. */
  1010. static bfd_boolean
  1011. coff_write_native_symbol (bfd *abfd,
  1012. coff_symbol_type *symbol,
  1013. bfd_vma *written,
  1014. bfd_size_type *string_size_p,
  1015. asection **debug_string_section_p,
  1016. bfd_size_type *debug_string_size_p)
  1017. {
  1018. combined_entry_type *native = symbol->native;
  1019. alent *lineno = symbol->lineno;
  1020. struct bfd_link_info *link_info = coff_data (abfd)->link_info;
  1021. if ((!link_info || link_info->strip_discarded)
  1022. && !bfd_is_abs_section (symbol->symbol.section)
  1023. && symbol->symbol.section->output_section == bfd_abs_section_ptr)
  1024. {
  1025. symbol->symbol.name = "";
  1026. return TRUE;
  1027. }
  1028. BFD_ASSERT (native->is_sym);
  1029. /* If this symbol has an associated line number, we must store the
  1030. symbol index in the line number field. We also tag the auxent to
  1031. point to the right place in the lineno table. */
  1032. if (lineno && !symbol->done_lineno && symbol->symbol.section->owner != NULL)
  1033. {
  1034. unsigned int count = 0;
  1035. lineno[count].u.offset = *written;
  1036. if (native->u.syment.n_numaux)
  1037. {
  1038. union internal_auxent *a = &((native + 1)->u.auxent);
  1039. a->x_sym.x_fcnary.x_fcn.x_lnnoptr =
  1040. symbol->symbol.section->output_section->moving_line_filepos;
  1041. }
  1042. /* Count and relocate all other linenumbers. */
  1043. count++;
  1044. while (lineno[count].line_number != 0)
  1045. {
  1046. lineno[count].u.offset +=
  1047. (symbol->symbol.section->output_section->vma
  1048. + symbol->symbol.section->output_offset);
  1049. count++;
  1050. }
  1051. symbol->done_lineno = TRUE;
  1052. if (! bfd_is_const_section (symbol->symbol.section->output_section))
  1053. symbol->symbol.section->output_section->moving_line_filepos +=
  1054. count * bfd_coff_linesz (abfd);
  1055. }
  1056. return coff_write_symbol (abfd, &(symbol->symbol), native, written,
  1057. string_size_p, debug_string_section_p,
  1058. debug_string_size_p);
  1059. }
  1060. static void
  1061. null_error_handler (const char * fmt ATTRIBUTE_UNUSED, ...)
  1062. {
  1063. }
  1064. /* Write out the COFF symbols. */
  1065. bfd_boolean
  1066. coff_write_symbols (bfd *abfd)
  1067. {
  1068. bfd_size_type string_size;
  1069. asection *debug_string_section;
  1070. bfd_size_type debug_string_size;
  1071. unsigned int i;
  1072. unsigned int limit = bfd_get_symcount (abfd);
  1073. bfd_vma written = 0;
  1074. asymbol **p;
  1075. string_size = 0;
  1076. debug_string_section = NULL;
  1077. debug_string_size = 0;
  1078. /* If this target supports long section names, they must be put into
  1079. the string table. This is supported by PE. This code must
  1080. handle section names just as they are handled in
  1081. coff_write_object_contents. */
  1082. if (bfd_coff_long_section_names (abfd))
  1083. {
  1084. asection *o;
  1085. for (o = abfd->sections; o != NULL; o = o->next)
  1086. {
  1087. size_t len;
  1088. len = strlen (o->name);
  1089. if (len > SCNNMLEN)
  1090. string_size += len + 1;
  1091. }
  1092. }
  1093. /* Seek to the right place. */
  1094. if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0)
  1095. return FALSE;
  1096. /* Output all the symbols we have. */
  1097. written = 0;
  1098. for (p = abfd->outsymbols, i = 0; i < limit; i++, p++)
  1099. {
  1100. asymbol *symbol = *p;
  1101. coff_symbol_type *c_symbol = coff_symbol_from (symbol);
  1102. if (c_symbol == (coff_symbol_type *) NULL
  1103. || c_symbol->native == (combined_entry_type *) NULL)
  1104. {
  1105. if (!coff_write_alien_symbol (abfd, symbol, NULL, &written,
  1106. &string_size, &debug_string_section,
  1107. &debug_string_size))
  1108. return FALSE;
  1109. }
  1110. else
  1111. {
  1112. if (coff_backend_info (abfd)->_bfd_coff_classify_symbol != NULL)
  1113. {
  1114. bfd_error_handler_type current_error_handler;
  1115. enum coff_symbol_classification sym_class;
  1116. unsigned char *n_sclass;
  1117. /* Suppress error reporting by bfd_coff_classify_symbol.
  1118. Error messages can be generated when we are processing a local
  1119. symbol which has no associated section and we do not have to
  1120. worry about this, all we need to know is that it is local. */
  1121. current_error_handler = bfd_set_error_handler (null_error_handler);
  1122. BFD_ASSERT (c_symbol->native->is_sym);
  1123. sym_class = bfd_coff_classify_symbol (abfd,
  1124. &c_symbol->native->u.syment);
  1125. (void) bfd_set_error_handler (current_error_handler);
  1126. n_sclass = &c_symbol->native->u.syment.n_sclass;
  1127. /* If the symbol class has been changed (eg objcopy/ld script/etc)
  1128. we cannot retain the existing sclass from the original symbol.
  1129. Weak symbols only have one valid sclass, so just set it always.
  1130. If it is not local class and should be, set it C_STAT.
  1131. If it is global and not classified as global, or if it is
  1132. weak (which is also classified as global), set it C_EXT. */
  1133. if (symbol->flags & BSF_WEAK)
  1134. *n_sclass = obj_pe (abfd) ? C_NT_WEAK : C_WEAKEXT;
  1135. else if (symbol->flags & BSF_LOCAL && sym_class != COFF_SYMBOL_LOCAL)
  1136. *n_sclass = C_STAT;
  1137. else if (symbol->flags & BSF_GLOBAL
  1138. && (sym_class != COFF_SYMBOL_GLOBAL
  1139. #ifdef COFF_WITH_PE
  1140. || *n_sclass == C_NT_WEAK
  1141. #endif
  1142. || *n_sclass == C_WEAKEXT))
  1143. c_symbol->native->u.syment.n_sclass = C_EXT;
  1144. }
  1145. if (!coff_write_native_symbol (abfd, c_symbol, &written,
  1146. &string_size, &debug_string_section,
  1147. &debug_string_size))
  1148. return FALSE;
  1149. }
  1150. }
  1151. obj_raw_syment_count (abfd) = written;
  1152. /* Now write out strings. */
  1153. if (string_size != 0)
  1154. {
  1155. unsigned int size = string_size + STRING_SIZE_SIZE;
  1156. bfd_byte buffer[STRING_SIZE_SIZE];
  1157. #if STRING_SIZE_SIZE == 4
  1158. H_PUT_32 (abfd, size, buffer);
  1159. #else
  1160. #error Change H_PUT_32
  1161. #endif
  1162. if (bfd_bwrite ((void *) buffer, (bfd_size_type) sizeof (buffer), abfd)
  1163. != sizeof (buffer))
  1164. return FALSE;
  1165. /* Handle long section names. This code must handle section
  1166. names just as they are handled in coff_write_object_contents. */
  1167. if (bfd_coff_long_section_names (abfd))
  1168. {
  1169. asection *o;
  1170. for (o = abfd->sections; o != NULL; o = o->next)
  1171. {
  1172. size_t len;
  1173. len = strlen (o->name);
  1174. if (len > SCNNMLEN)
  1175. {
  1176. if (bfd_bwrite (o->name, (bfd_size_type) (len + 1), abfd)
  1177. != len + 1)
  1178. return FALSE;
  1179. }
  1180. }
  1181. }
  1182. for (p = abfd->outsymbols, i = 0;
  1183. i < limit;
  1184. i++, p++)
  1185. {
  1186. asymbol *q = *p;
  1187. size_t name_length = strlen (q->name);
  1188. coff_symbol_type *c_symbol = coff_symbol_from (q);
  1189. size_t maxlen;
  1190. /* Figure out whether the symbol name should go in the string
  1191. table. Symbol names that are short enough are stored
  1192. directly in the syment structure. File names permit a
  1193. different, longer, length in the syment structure. On
  1194. XCOFF, some symbol names are stored in the .debug section
  1195. rather than in the string table. */
  1196. if (c_symbol == NULL
  1197. || c_symbol->native == NULL)
  1198. /* This is not a COFF symbol, so it certainly is not a
  1199. file name, nor does it go in the .debug section. */
  1200. maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
  1201. else if (! c_symbol->native->is_sym)
  1202. maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
  1203. else if (bfd_coff_symname_in_debug (abfd,
  1204. &c_symbol->native->u.syment))
  1205. /* This symbol name is in the XCOFF .debug section.
  1206. Don't write it into the string table. */
  1207. maxlen = name_length;
  1208. else if (c_symbol->native->u.syment.n_sclass == C_FILE
  1209. && c_symbol->native->u.syment.n_numaux > 0)
  1210. {
  1211. if (bfd_coff_force_symnames_in_strings (abfd))
  1212. {
  1213. if (bfd_bwrite (".file", (bfd_size_type) 6, abfd) != 6)
  1214. return FALSE;
  1215. }
  1216. maxlen = bfd_coff_filnmlen (abfd);
  1217. }
  1218. else
  1219. maxlen = bfd_coff_force_symnames_in_strings (abfd) ? 0 : SYMNMLEN;
  1220. if (name_length > maxlen)
  1221. {
  1222. if (bfd_bwrite ((void *) (q->name), (bfd_size_type) name_length + 1,
  1223. abfd) != name_length + 1)
  1224. return FALSE;
  1225. }
  1226. }
  1227. }
  1228. else
  1229. {
  1230. /* We would normally not write anything here, but we'll write
  1231. out 4 so that any stupid coff reader which tries to read the
  1232. string table even when there isn't one won't croak. */
  1233. unsigned int size = STRING_SIZE_SIZE;
  1234. bfd_byte buffer[STRING_SIZE_SIZE];
  1235. #if STRING_SIZE_SIZE == 4
  1236. H_PUT_32 (abfd, size, buffer);
  1237. #else
  1238. #error Change H_PUT_32
  1239. #endif
  1240. if (bfd_bwrite ((void *) buffer, (bfd_size_type) STRING_SIZE_SIZE, abfd)
  1241. != STRING_SIZE_SIZE)
  1242. return FALSE;
  1243. }
  1244. /* Make sure the .debug section was created to be the correct size.
  1245. We should create it ourselves on the fly, but we don't because
  1246. BFD won't let us write to any section until we know how large all
  1247. the sections are. We could still do it by making another pass
  1248. over the symbols. FIXME. */
  1249. BFD_ASSERT (debug_string_size == 0
  1250. || (debug_string_section != (asection *) NULL
  1251. && (BFD_ALIGN (debug_string_size,
  1252. 1 << debug_string_section->alignment_power)
  1253. == debug_string_section->size)));
  1254. return TRUE;
  1255. }
  1256. bfd_boolean
  1257. coff_write_linenumbers (bfd *abfd)
  1258. {
  1259. asection *s;
  1260. bfd_size_type linesz;
  1261. void * buff;
  1262. linesz = bfd_coff_linesz (abfd);
  1263. buff = bfd_alloc (abfd, linesz);
  1264. if (!buff)
  1265. return FALSE;
  1266. for (s = abfd->sections; s != (asection *) NULL; s = s->next)
  1267. {
  1268. if (s->lineno_count)
  1269. {
  1270. asymbol **q = abfd->outsymbols;
  1271. if (bfd_seek (abfd, s->line_filepos, SEEK_SET) != 0)
  1272. return FALSE;
  1273. /* Find all the linenumbers in this section. */
  1274. while (*q)
  1275. {
  1276. asymbol *p = *q;
  1277. if (p->section->output_section == s)
  1278. {
  1279. alent *l =
  1280. BFD_SEND (bfd_asymbol_bfd (p), _get_lineno,
  1281. (bfd_asymbol_bfd (p), p));
  1282. if (l)
  1283. {
  1284. /* Found a linenumber entry, output. */
  1285. struct internal_lineno out;
  1286. memset ((void *) & out, 0, sizeof (out));
  1287. out.l_lnno = 0;
  1288. out.l_addr.l_symndx = l->u.offset;
  1289. bfd_coff_swap_lineno_out (abfd, &out, buff);
  1290. if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
  1291. != linesz)
  1292. return FALSE;
  1293. l++;
  1294. while (l->line_number)
  1295. {
  1296. out.l_lnno = l->line_number;
  1297. out.l_addr.l_symndx = l->u.offset;
  1298. bfd_coff_swap_lineno_out (abfd, &out, buff);
  1299. if (bfd_bwrite (buff, (bfd_size_type) linesz, abfd)
  1300. != linesz)
  1301. return FALSE;
  1302. l++;
  1303. }
  1304. }
  1305. }
  1306. q++;
  1307. }
  1308. }
  1309. }
  1310. bfd_release (abfd, buff);
  1311. return TRUE;
  1312. }
  1313. alent *
  1314. coff_get_lineno (bfd *ignore_abfd ATTRIBUTE_UNUSED, asymbol *symbol)
  1315. {
  1316. return coffsymbol (symbol)->lineno;
  1317. }
  1318. /* This function transforms the offsets into the symbol table into
  1319. pointers to syments. */
  1320. static void
  1321. coff_pointerize_aux (bfd *abfd,
  1322. combined_entry_type *table_base,
  1323. combined_entry_type *symbol,
  1324. unsigned int indaux,
  1325. combined_entry_type *auxent)
  1326. {
  1327. unsigned int type = symbol->u.syment.n_type;
  1328. unsigned int n_sclass = symbol->u.syment.n_sclass;
  1329. BFD_ASSERT (symbol->is_sym);
  1330. if (coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
  1331. {
  1332. if ((*coff_backend_info (abfd)->_bfd_coff_pointerize_aux_hook)
  1333. (abfd, table_base, symbol, indaux, auxent))
  1334. return;
  1335. }
  1336. /* Don't bother if this is a file or a section. */
  1337. if (n_sclass == C_STAT && type == T_NULL)
  1338. return;
  1339. if (n_sclass == C_FILE)
  1340. return;
  1341. BFD_ASSERT (! auxent->is_sym);
  1342. /* Otherwise patch up. */
  1343. #define N_TMASK coff_data (abfd)->local_n_tmask
  1344. #define N_BTSHFT coff_data (abfd)->local_n_btshft
  1345. if ((ISFCN (type) || ISTAG (n_sclass) || n_sclass == C_BLOCK
  1346. || n_sclass == C_FCN)
  1347. && auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l > 0)
  1348. {
  1349. auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p =
  1350. table_base + auxent->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
  1351. auxent->fix_end = 1;
  1352. }
  1353. /* A negative tagndx is meaningless, but the SCO 3.2v4 cc can
  1354. generate one, so we must be careful to ignore it. */
  1355. if (auxent->u.auxent.x_sym.x_tagndx.l > 0)
  1356. {
  1357. auxent->u.auxent.x_sym.x_tagndx.p =
  1358. table_base + auxent->u.auxent.x_sym.x_tagndx.l;
  1359. auxent->fix_tag = 1;
  1360. }
  1361. }
  1362. /* Allocate space for the ".debug" section, and read it.
  1363. We did not read the debug section until now, because
  1364. we didn't want to go to the trouble until someone needed it. */
  1365. static char *
  1366. build_debug_section (bfd *abfd, asection ** sect_return)
  1367. {
  1368. char *debug_section;
  1369. file_ptr position;
  1370. bfd_size_type sec_size;
  1371. asection *sect = bfd_get_section_by_name (abfd, ".debug");
  1372. if (!sect)
  1373. {
  1374. bfd_set_error (bfd_error_no_debug_section);
  1375. return NULL;
  1376. }
  1377. sec_size = sect->size;
  1378. debug_section = (char *) bfd_alloc (abfd, sec_size);
  1379. if (debug_section == NULL)
  1380. return NULL;
  1381. /* Seek to the beginning of the `.debug' section and read it.
  1382. Save the current position first; it is needed by our caller.
  1383. Then read debug section and reset the file pointer. */
  1384. position = bfd_tell (abfd);
  1385. if (bfd_seek (abfd, sect->filepos, SEEK_SET) != 0
  1386. || bfd_bread (debug_section, sec_size, abfd) != sec_size
  1387. || bfd_seek (abfd, position, SEEK_SET) != 0)
  1388. return NULL;
  1389. * sect_return = sect;
  1390. return debug_section;
  1391. }
  1392. /* Return a pointer to a malloc'd copy of 'name'. 'name' may not be
  1393. \0-terminated, but will not exceed 'maxlen' characters. The copy *will*
  1394. be \0-terminated. */
  1395. static char *
  1396. copy_name (bfd *abfd, char *name, size_t maxlen)
  1397. {
  1398. size_t len;
  1399. char *newname;
  1400. for (len = 0; len < maxlen; ++len)
  1401. if (name[len] == '\0')
  1402. break;
  1403. if ((newname = (char *) bfd_alloc (abfd, (bfd_size_type) len + 1)) == NULL)
  1404. return NULL;
  1405. strncpy (newname, name, len);
  1406. newname[len] = '\0';
  1407. return newname;
  1408. }
  1409. /* Read in the external symbols. */
  1410. bfd_boolean
  1411. _bfd_coff_get_external_symbols (bfd *abfd)
  1412. {
  1413. bfd_size_type symesz;
  1414. bfd_size_type size;
  1415. void * syms;
  1416. if (obj_coff_external_syms (abfd) != NULL)
  1417. return TRUE;
  1418. symesz = bfd_coff_symesz (abfd);
  1419. size = obj_raw_syment_count (abfd) * symesz;
  1420. if (size == 0)
  1421. return TRUE;
  1422. syms = bfd_malloc (size);
  1423. if (syms == NULL)
  1424. return FALSE;
  1425. if (bfd_seek (abfd, obj_sym_filepos (abfd), SEEK_SET) != 0
  1426. || bfd_bread (syms, size, abfd) != size)
  1427. {
  1428. if (syms != NULL)
  1429. free (syms);
  1430. return FALSE;
  1431. }
  1432. obj_coff_external_syms (abfd) = syms;
  1433. return TRUE;
  1434. }
  1435. /* Read in the external strings. The strings are not loaded until
  1436. they are needed. This is because we have no simple way of
  1437. detecting a missing string table in an archive. If the strings
  1438. are loaded then the STRINGS and STRINGS_LEN fields in the
  1439. coff_tdata structure will be set. */
  1440. const char *
  1441. _bfd_coff_read_string_table (bfd *abfd)
  1442. {
  1443. char extstrsize[STRING_SIZE_SIZE];
  1444. bfd_size_type strsize;
  1445. char *strings;
  1446. file_ptr pos;
  1447. if (obj_coff_strings (abfd) != NULL)
  1448. return obj_coff_strings (abfd);
  1449. if (obj_sym_filepos (abfd) == 0)
  1450. {
  1451. bfd_set_error (bfd_error_no_symbols);
  1452. return NULL;
  1453. }
  1454. pos = obj_sym_filepos (abfd);
  1455. pos += obj_raw_syment_count (abfd) * bfd_coff_symesz (abfd);
  1456. if (bfd_seek (abfd, pos, SEEK_SET) != 0)
  1457. return NULL;
  1458. if (bfd_bread (extstrsize, (bfd_size_type) sizeof extstrsize, abfd)
  1459. != sizeof extstrsize)
  1460. {
  1461. if (bfd_get_error () != bfd_error_file_truncated)
  1462. return NULL;
  1463. /* There is no string table. */
  1464. strsize = STRING_SIZE_SIZE;
  1465. }
  1466. else
  1467. {
  1468. #if STRING_SIZE_SIZE == 4
  1469. strsize = H_GET_32 (abfd, extstrsize);
  1470. #else
  1471. #error Change H_GET_32
  1472. #endif
  1473. }
  1474. if (strsize < STRING_SIZE_SIZE)
  1475. {
  1476. (*_bfd_error_handler)
  1477. (_("%B: bad string table size %lu"), abfd, (unsigned long) strsize);
  1478. bfd_set_error (bfd_error_bad_value);
  1479. return NULL;
  1480. }
  1481. strings = (char *) bfd_malloc (strsize + 1);
  1482. if (strings == NULL)
  1483. return NULL;
  1484. /* PR 17521 file: 079-54929-0.004.
  1485. A corrupt file could contain an index that points into the first
  1486. STRING_SIZE_SIZE bytes of the string table, so make sure that
  1487. they are zero. */
  1488. memset (strings, 0, STRING_SIZE_SIZE);
  1489. if (bfd_bread (strings + STRING_SIZE_SIZE, strsize - STRING_SIZE_SIZE, abfd)
  1490. != strsize - STRING_SIZE_SIZE)
  1491. {
  1492. free (strings);
  1493. return NULL;
  1494. }
  1495. obj_coff_strings (abfd) = strings;
  1496. obj_coff_strings_len (abfd) = strsize;
  1497. /* Terminate the string table, just in case. */
  1498. strings[strsize] = 0;
  1499. return strings;
  1500. }
  1501. /* Free up the external symbols and strings read from a COFF file. */
  1502. bfd_boolean
  1503. _bfd_coff_free_symbols (bfd *abfd)
  1504. {
  1505. if (obj_coff_external_syms (abfd) != NULL
  1506. && ! obj_coff_keep_syms (abfd))
  1507. {
  1508. free (obj_coff_external_syms (abfd));
  1509. obj_coff_external_syms (abfd) = NULL;
  1510. }
  1511. if (obj_coff_strings (abfd) != NULL
  1512. && ! obj_coff_keep_strings (abfd))
  1513. {
  1514. free (obj_coff_strings (abfd));
  1515. obj_coff_strings (abfd) = NULL;
  1516. obj_coff_strings_len (abfd) = 0;
  1517. }
  1518. return TRUE;
  1519. }
  1520. /* Read a symbol table into freshly bfd_allocated memory, swap it, and
  1521. knit the symbol names into a normalized form. By normalized here I
  1522. mean that all symbols have an n_offset pointer that points to a null-
  1523. terminated string. */
  1524. combined_entry_type *
  1525. coff_get_normalized_symtab (bfd *abfd)
  1526. {
  1527. combined_entry_type *internal;
  1528. combined_entry_type *internal_ptr;
  1529. combined_entry_type *symbol_ptr;
  1530. combined_entry_type *internal_end;
  1531. size_t symesz;
  1532. char *raw_src;
  1533. char *raw_end;
  1534. const char *string_table = NULL;
  1535. asection * debug_sec = NULL;
  1536. char *debug_sec_data = NULL;
  1537. bfd_size_type size;
  1538. if (obj_raw_syments (abfd) != NULL)
  1539. return obj_raw_syments (abfd);
  1540. if (! _bfd_coff_get_external_symbols (abfd))
  1541. return NULL;
  1542. size = obj_raw_syment_count (abfd) * sizeof (combined_entry_type);
  1543. internal = (combined_entry_type *) bfd_zalloc (abfd, size);
  1544. if (internal == NULL && size != 0)
  1545. return NULL;
  1546. internal_end = internal + obj_raw_syment_count (abfd);
  1547. raw_src = (char *) obj_coff_external_syms (abfd);
  1548. /* Mark the end of the symbols. */
  1549. symesz = bfd_coff_symesz (abfd);
  1550. raw_end = (char *) raw_src + obj_raw_syment_count (abfd) * symesz;
  1551. /* FIXME SOMEDAY. A string table size of zero is very weird, but
  1552. probably possible. If one shows up, it will probably kill us. */
  1553. /* Swap all the raw entries. */
  1554. for (internal_ptr = internal;
  1555. raw_src < raw_end;
  1556. raw_src += symesz, internal_ptr++)
  1557. {
  1558. unsigned int i;
  1559. bfd_coff_swap_sym_in (abfd, (void *) raw_src,
  1560. (void *) & internal_ptr->u.syment);
  1561. symbol_ptr = internal_ptr;
  1562. internal_ptr->is_sym = TRUE;
  1563. /* PR 17512: file: 1353-1166-0.004. */
  1564. if (symbol_ptr->u.syment.n_sclass == C_FILE
  1565. && symbol_ptr->u.syment.n_numaux > 0
  1566. && raw_src + symesz + symbol_ptr->u.syment.n_numaux
  1567. * symesz > raw_end)
  1568. {
  1569. bfd_release (abfd, internal);
  1570. return NULL;
  1571. }
  1572. for (i = 0;
  1573. i < symbol_ptr->u.syment.n_numaux;
  1574. i++)
  1575. {
  1576. internal_ptr++;
  1577. /* PR 17512: Prevent buffer overrun. */
  1578. if (internal_ptr >= internal_end)
  1579. {
  1580. bfd_release (abfd, internal);
  1581. return NULL;
  1582. }
  1583. raw_src += symesz;
  1584. bfd_coff_swap_aux_in (abfd, (void *) raw_src,
  1585. symbol_ptr->u.syment.n_type,
  1586. symbol_ptr->u.syment.n_sclass,
  1587. (int) i, symbol_ptr->u.syment.n_numaux,
  1588. &(internal_ptr->u.auxent));
  1589. internal_ptr->is_sym = FALSE;
  1590. coff_pointerize_aux (abfd, internal, symbol_ptr, i,
  1591. internal_ptr);
  1592. }
  1593. }
  1594. /* Free the raw symbols, but not the strings (if we have them). */
  1595. obj_coff_keep_strings (abfd) = TRUE;
  1596. if (! _bfd_coff_free_symbols (abfd))
  1597. return NULL;
  1598. for (internal_ptr = internal; internal_ptr < internal_end;
  1599. internal_ptr++)
  1600. {
  1601. BFD_ASSERT (internal_ptr->is_sym);
  1602. if (internal_ptr->u.syment.n_sclass == C_FILE
  1603. && internal_ptr->u.syment.n_numaux > 0)
  1604. {
  1605. combined_entry_type * aux = internal_ptr + 1;
  1606. /* Make a file symbol point to the name in the auxent, since
  1607. the text ".file" is redundant. */
  1608. BFD_ASSERT (! aux->is_sym);
  1609. if (aux->u.auxent.x_file.x_n.x_zeroes == 0)
  1610. {
  1611. /* The filename is a long one, point into the string table. */
  1612. if (string_table == NULL)
  1613. {
  1614. string_table = _bfd_coff_read_string_table (abfd);
  1615. if (string_table == NULL)
  1616. return NULL;
  1617. }
  1618. if ((bfd_size_type)(aux->u.auxent.x_file.x_n.x_offset)
  1619. >= obj_coff_strings_len (abfd))
  1620. internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) _("<corrupt>");
  1621. else
  1622. internal_ptr->u.syment._n._n_n._n_offset =
  1623. (bfd_hostptr_t) (string_table + (aux->u.auxent.x_file.x_n.x_offset));
  1624. }
  1625. else
  1626. {
  1627. /* Ordinary short filename, put into memory anyway. The
  1628. Microsoft PE tools sometimes store a filename in
  1629. multiple AUX entries. */
  1630. if (internal_ptr->u.syment.n_numaux > 1
  1631. && coff_data (abfd)->pe)
  1632. internal_ptr->u.syment._n._n_n._n_offset =
  1633. (bfd_hostptr_t)
  1634. copy_name (abfd,
  1635. aux->u.auxent.x_file.x_fname,
  1636. internal_ptr->u.syment.n_numaux * symesz);
  1637. else
  1638. internal_ptr->u.syment._n._n_n._n_offset =
  1639. ((bfd_hostptr_t)
  1640. copy_name (abfd,
  1641. aux->u.auxent.x_file.x_fname,
  1642. (size_t) bfd_coff_filnmlen (abfd)));
  1643. }
  1644. }
  1645. else
  1646. {
  1647. if (internal_ptr->u.syment._n._n_n._n_zeroes != 0)
  1648. {
  1649. /* This is a "short" name. Make it long. */
  1650. size_t i;
  1651. char *newstring;
  1652. /* Find the length of this string without walking into memory
  1653. that isn't ours. */
  1654. for (i = 0; i < 8; ++i)
  1655. if (internal_ptr->u.syment._n._n_name[i] == '\0')
  1656. break;
  1657. newstring = (char *) bfd_zalloc (abfd, (bfd_size_type) (i + 1));
  1658. if (newstring == NULL)
  1659. return NULL;
  1660. strncpy (newstring, internal_ptr->u.syment._n._n_name, i);
  1661. internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) newstring;
  1662. internal_ptr->u.syment._n._n_n._n_zeroes = 0;
  1663. }
  1664. else if (internal_ptr->u.syment._n._n_n._n_offset == 0)
  1665. internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) "";
  1666. else if (!bfd_coff_symname_in_debug (abfd, &internal_ptr->u.syment))
  1667. {
  1668. /* Long name already. Point symbol at the string in the
  1669. table. */
  1670. if (string_table == NULL)
  1671. {
  1672. string_table = _bfd_coff_read_string_table (abfd);
  1673. if (string_table == NULL)
  1674. return NULL;
  1675. }
  1676. if (internal_ptr->u.syment._n._n_n._n_offset >= obj_coff_strings_len (abfd)
  1677. || string_table + internal_ptr->u.syment._n._n_n._n_offset < string_table)
  1678. internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) _("<corrupt>");
  1679. else
  1680. internal_ptr->u.syment._n._n_n._n_offset =
  1681. ((bfd_hostptr_t)
  1682. (string_table
  1683. + internal_ptr->u.syment._n._n_n._n_offset));
  1684. }
  1685. else
  1686. {
  1687. /* Long name in debug section. Very similar. */
  1688. if (debug_sec_data == NULL)
  1689. debug_sec_data = build_debug_section (abfd, & debug_sec);
  1690. if (debug_sec_data != NULL)
  1691. {
  1692. BFD_ASSERT (debug_sec != NULL);
  1693. /* PR binutils/17512: Catch out of range offsets into the debug data. */
  1694. if (internal_ptr->u.syment._n._n_n._n_offset > debug_sec->size
  1695. || debug_sec_data + internal_ptr->u.syment._n._n_n._n_offset < debug_sec_data)
  1696. internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) _("<corrupt>");
  1697. else
  1698. internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t)
  1699. (debug_sec_data + internal_ptr->u.syment._n._n_n._n_offset);
  1700. }
  1701. else
  1702. internal_ptr->u.syment._n._n_n._n_offset = (bfd_hostptr_t) "";
  1703. }
  1704. }
  1705. internal_ptr += internal_ptr->u.syment.n_numaux;
  1706. }
  1707. obj_raw_syments (abfd) = internal;
  1708. BFD_ASSERT (obj_raw_syment_count (abfd)
  1709. == (unsigned int) (internal_ptr - internal));
  1710. return internal;
  1711. }
  1712. long
  1713. coff_get_reloc_upper_bound (bfd *abfd, sec_ptr asect)
  1714. {
  1715. if (bfd_get_format (abfd) != bfd_object)
  1716. {
  1717. bfd_set_error (bfd_error_invalid_operation);
  1718. return -1;
  1719. }
  1720. return (asect->reloc_count + 1) * sizeof (arelent *);
  1721. }
  1722. asymbol *
  1723. coff_make_empty_symbol (bfd *abfd)
  1724. {
  1725. bfd_size_type amt = sizeof (coff_symbol_type);
  1726. coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_zalloc (abfd, amt);
  1727. if (new_symbol == NULL)
  1728. return NULL;
  1729. new_symbol->symbol.section = 0;
  1730. new_symbol->native = NULL;
  1731. new_symbol->lineno = NULL;
  1732. new_symbol->done_lineno = FALSE;
  1733. new_symbol->symbol.the_bfd = abfd;
  1734. return & new_symbol->symbol;
  1735. }
  1736. /* Make a debugging symbol. */
  1737. asymbol *
  1738. coff_bfd_make_debug_symbol (bfd *abfd,
  1739. void * ptr ATTRIBUTE_UNUSED,
  1740. unsigned long sz ATTRIBUTE_UNUSED)
  1741. {
  1742. bfd_size_type amt = sizeof (coff_symbol_type);
  1743. coff_symbol_type *new_symbol = (coff_symbol_type *) bfd_alloc (abfd, amt);
  1744. if (new_symbol == NULL)
  1745. return NULL;
  1746. /* @@ The 10 is a guess at a plausible maximum number of aux entries
  1747. (but shouldn't be a constant). */
  1748. amt = sizeof (combined_entry_type) * 10;
  1749. new_symbol->native = (combined_entry_type *) bfd_zalloc (abfd, amt);
  1750. if (!new_symbol->native)
  1751. return NULL;
  1752. new_symbol->native->is_sym = TRUE;
  1753. new_symbol->symbol.section = bfd_abs_section_ptr;
  1754. new_symbol->symbol.flags = BSF_DEBUGGING;
  1755. new_symbol->lineno = NULL;
  1756. new_symbol->done_lineno = FALSE;
  1757. new_symbol->symbol.the_bfd = abfd;
  1758. return & new_symbol->symbol;
  1759. }
  1760. void
  1761. coff_get_symbol_info (bfd *abfd, asymbol *symbol, symbol_info *ret)
  1762. {
  1763. bfd_symbol_info (symbol, ret);
  1764. if (coffsymbol (symbol)->native != NULL
  1765. && coffsymbol (symbol)->native->fix_value
  1766. && coffsymbol (symbol)->native->is_sym)
  1767. ret->value = coffsymbol (symbol)->native->u.syment.n_value -
  1768. (bfd_hostptr_t) obj_raw_syments (abfd);
  1769. }
  1770. /* Print out information about COFF symbol. */
  1771. void
  1772. coff_print_symbol (bfd *abfd,
  1773. void * filep,
  1774. asymbol *symbol,
  1775. bfd_print_symbol_type how)
  1776. {
  1777. FILE * file = (FILE *) filep;
  1778. switch (how)
  1779. {
  1780. case bfd_print_symbol_name:
  1781. fprintf (file, "%s", symbol->name);
  1782. break;
  1783. case bfd_print_symbol_more:
  1784. fprintf (file, "coff %s %s",
  1785. coffsymbol (symbol)->native ? "n" : "g",
  1786. coffsymbol (symbol)->lineno ? "l" : " ");
  1787. break;
  1788. case bfd_print_symbol_all:
  1789. if (coffsymbol (symbol)->native)
  1790. {
  1791. bfd_vma val;
  1792. unsigned int aux;
  1793. combined_entry_type *combined = coffsymbol (symbol)->native;
  1794. combined_entry_type *root = obj_raw_syments (abfd);
  1795. struct lineno_cache_entry *l = coffsymbol (symbol)->lineno;
  1796. fprintf (file, "[%3ld]", (long) (combined - root));
  1797. /* PR 17512: file: 079-33786-0.001:0.1. */
  1798. if (combined < obj_raw_syments (abfd)
  1799. || combined >= obj_raw_syments (abfd) + obj_raw_syment_count (abfd))
  1800. {
  1801. fprintf (file, _("<corrupt info> %s"), symbol->name);
  1802. break;
  1803. }
  1804. BFD_ASSERT (combined->is_sym);
  1805. if (! combined->fix_value)
  1806. val = (bfd_vma) combined->u.syment.n_value;
  1807. else
  1808. val = combined->u.syment.n_value - (bfd_hostptr_t) root;
  1809. fprintf (file, "(sec %2d)(fl 0x%02x)(ty %3x)(scl %3d) (nx %d) 0x",
  1810. combined->u.syment.n_scnum,
  1811. combined->u.syment.n_flags,
  1812. combined->u.syment.n_type,
  1813. combined->u.syment.n_sclass,
  1814. combined->u.syment.n_numaux);
  1815. bfd_fprintf_vma (abfd, file, val);
  1816. fprintf (file, " %s", symbol->name);
  1817. for (aux = 0; aux < combined->u.syment.n_numaux; aux++)
  1818. {
  1819. combined_entry_type *auxp = combined + aux + 1;
  1820. long tagndx;
  1821. BFD_ASSERT (! auxp->is_sym);
  1822. if (auxp->fix_tag)
  1823. tagndx = auxp->u.auxent.x_sym.x_tagndx.p - root;
  1824. else
  1825. tagndx = auxp->u.auxent.x_sym.x_tagndx.l;
  1826. fprintf (file, "\n");
  1827. if (bfd_coff_print_aux (abfd, file, root, combined, auxp, aux))
  1828. continue;
  1829. switch (combined->u.syment.n_sclass)
  1830. {
  1831. case C_FILE:
  1832. fprintf (file, "File ");
  1833. break;
  1834. case C_STAT:
  1835. if (combined->u.syment.n_type == T_NULL)
  1836. /* Probably a section symbol ? */
  1837. {
  1838. fprintf (file, "AUX scnlen 0x%lx nreloc %d nlnno %d",
  1839. (unsigned long) auxp->u.auxent.x_scn.x_scnlen,
  1840. auxp->u.auxent.x_scn.x_nreloc,
  1841. auxp->u.auxent.x_scn.x_nlinno);
  1842. if (auxp->u.auxent.x_scn.x_checksum != 0
  1843. || auxp->u.auxent.x_scn.x_associated != 0
  1844. || auxp->u.auxent.x_scn.x_comdat != 0)
  1845. fprintf (file, " checksum 0x%lx assoc %d comdat %d",
  1846. auxp->u.auxent.x_scn.x_checksum,
  1847. auxp->u.auxent.x_scn.x_associated,
  1848. auxp->u.auxent.x_scn.x_comdat);
  1849. break;
  1850. }
  1851. /* Otherwise fall through. */
  1852. case C_EXT:
  1853. case C_AIX_WEAKEXT:
  1854. if (ISFCN (combined->u.syment.n_type))
  1855. {
  1856. long next, llnos;
  1857. if (auxp->fix_end)
  1858. next = (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
  1859. - root);
  1860. else
  1861. next = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.l;
  1862. llnos = auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_lnnoptr;
  1863. fprintf (file,
  1864. "AUX tagndx %ld ttlsiz 0x%lx lnnos %ld next %ld",
  1865. tagndx,
  1866. (unsigned long) auxp->u.auxent.x_sym.x_misc.x_fsize,
  1867. llnos, next);
  1868. break;
  1869. }
  1870. /* Otherwise fall through. */
  1871. default:
  1872. fprintf (file, "AUX lnno %d size 0x%x tagndx %ld",
  1873. auxp->u.auxent.x_sym.x_misc.x_lnsz.x_lnno,
  1874. auxp->u.auxent.x_sym.x_misc.x_lnsz.x_size,
  1875. tagndx);
  1876. if (auxp->fix_end)
  1877. fprintf (file, " endndx %ld",
  1878. ((long)
  1879. (auxp->u.auxent.x_sym.x_fcnary.x_fcn.x_endndx.p
  1880. - root)));
  1881. break;
  1882. }
  1883. }
  1884. if (l)
  1885. {
  1886. fprintf (file, "\n%s :", l->u.sym->name);
  1887. l++;
  1888. while (l->line_number)
  1889. {
  1890. if (l->line_number > 0)
  1891. {
  1892. fprintf (file, "\n%4d : ", l->line_number);
  1893. bfd_fprintf_vma (abfd, file, l->u.offset + symbol->section->vma);
  1894. }
  1895. l++;
  1896. }
  1897. }
  1898. }
  1899. else
  1900. {
  1901. bfd_print_symbol_vandf (abfd, (void *) file, symbol);
  1902. fprintf (file, " %-5s %s %s %s",
  1903. symbol->section->name,
  1904. coffsymbol (symbol)->native ? "n" : "g",
  1905. coffsymbol (symbol)->lineno ? "l" : " ",
  1906. symbol->name);
  1907. }
  1908. }
  1909. }
  1910. /* Return whether a symbol name implies a local symbol. In COFF,
  1911. local symbols generally start with ``.L''. Most targets use this
  1912. function for the is_local_label_name entry point, but some may
  1913. override it. */
  1914. bfd_boolean
  1915. _bfd_coff_is_local_label_name (bfd *abfd ATTRIBUTE_UNUSED,
  1916. const char *name)
  1917. {
  1918. return name[0] == '.' && name[1] == 'L';
  1919. }
  1920. /* Provided a BFD, a section and an offset (in bytes, not octets) into the
  1921. section, calculate and return the name of the source file and the line
  1922. nearest to the wanted location. */
  1923. bfd_boolean
  1924. coff_find_nearest_line_with_names (bfd *abfd,
  1925. asymbol **symbols,
  1926. asection *section,
  1927. bfd_vma offset,
  1928. const char **filename_ptr,
  1929. const char **functionname_ptr,
  1930. unsigned int *line_ptr,
  1931. const struct dwarf_debug_section *debug_sections)
  1932. {
  1933. bfd_boolean found;
  1934. unsigned int i;
  1935. unsigned int line_base;
  1936. coff_data_type *cof = coff_data (abfd);
  1937. /* Run through the raw syments if available. */
  1938. combined_entry_type *p;
  1939. combined_entry_type *pend;
  1940. alent *l;
  1941. struct coff_section_tdata *sec_data;
  1942. bfd_size_type amt;
  1943. /* Before looking through the symbol table, try to use a .stab
  1944. section to find the information. */
  1945. if (! _bfd_stab_section_find_nearest_line (abfd, symbols, section, offset,
  1946. &found, filename_ptr,
  1947. functionname_ptr, line_ptr,
  1948. &coff_data(abfd)->line_info))
  1949. return FALSE;
  1950. if (found)
  1951. return TRUE;
  1952. /* Also try examining DWARF2 debugging information. */
  1953. if (_bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section, offset,
  1954. filename_ptr, functionname_ptr,
  1955. line_ptr, NULL, debug_sections, 0,
  1956. &coff_data(abfd)->dwarf2_find_line_info))
  1957. return TRUE;
  1958. /* If the DWARF lookup failed, but there is DWARF information available
  1959. then the problem might be that the file has been rebased. This tool
  1960. changes the VMAs of all the sections, but it does not update the DWARF
  1961. information. So try again, using a bias against the address sought. */
  1962. if (coff_data (abfd)->dwarf2_find_line_info != NULL)
  1963. {
  1964. bfd_signed_vma bias;
  1965. bias = _bfd_dwarf2_find_symbol_bias (symbols,
  1966. & coff_data (abfd)->dwarf2_find_line_info);
  1967. if (bias
  1968. && _bfd_dwarf2_find_nearest_line (abfd, symbols, NULL, section,
  1969. offset + bias,
  1970. filename_ptr, functionname_ptr,
  1971. line_ptr, NULL, debug_sections, 0,
  1972. &coff_data(abfd)->dwarf2_find_line_info))
  1973. return TRUE;
  1974. }
  1975. *filename_ptr = 0;
  1976. *functionname_ptr = 0;
  1977. *line_ptr = 0;
  1978. /* Don't try and find line numbers in a non coff file. */
  1979. if (!bfd_family_coff (abfd))
  1980. return FALSE;
  1981. if (cof == NULL)
  1982. return FALSE;
  1983. /* Find the first C_FILE symbol. */
  1984. p = cof->raw_syments;
  1985. if (!p)
  1986. return FALSE;
  1987. pend = p + cof->raw_syment_count;
  1988. while (p < pend)
  1989. {
  1990. BFD_ASSERT (p->is_sym);
  1991. if (p->u.syment.n_sclass == C_FILE)
  1992. break;
  1993. p += 1 + p->u.syment.n_numaux;
  1994. }
  1995. if (p < pend)
  1996. {
  1997. bfd_vma sec_vma;
  1998. bfd_vma maxdiff;
  1999. /* Look through the C_FILE symbols to find the best one. */
  2000. sec_vma = bfd_get_section_vma (abfd, section);
  2001. *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
  2002. maxdiff = (bfd_vma) 0 - (bfd_vma) 1;
  2003. while (1)
  2004. {
  2005. bfd_vma file_addr;
  2006. combined_entry_type *p2;
  2007. for (p2 = p + 1 + p->u.syment.n_numaux;
  2008. p2 < pend;
  2009. p2 += 1 + p2->u.syment.n_numaux)
  2010. {
  2011. BFD_ASSERT (p2->is_sym);
  2012. if (p2->u.syment.n_scnum > 0
  2013. && (section
  2014. == coff_section_from_bfd_index (abfd,
  2015. p2->u.syment.n_scnum)))
  2016. break;
  2017. if (p2->u.syment.n_sclass == C_FILE)
  2018. {
  2019. p2 = pend;
  2020. break;
  2021. }
  2022. }
  2023. if (p2 >= pend)
  2024. break;
  2025. file_addr = (bfd_vma) p2->u.syment.n_value;
  2026. /* PR 11512: Include the section address of the function name symbol. */
  2027. if (p2->u.syment.n_scnum > 0)
  2028. file_addr += coff_section_from_bfd_index (abfd,
  2029. p2->u.syment.n_scnum)->vma;
  2030. /* We use <= MAXDIFF here so that if we get a zero length
  2031. file, we actually use the next file entry. */
  2032. if (p2 < pend
  2033. && offset + sec_vma >= file_addr
  2034. && offset + sec_vma - file_addr <= maxdiff)
  2035. {
  2036. *filename_ptr = (char *) p->u.syment._n._n_n._n_offset;
  2037. maxdiff = offset + sec_vma - p2->u.syment.n_value;
  2038. }
  2039. /* Avoid endless loops on erroneous files by ensuring that
  2040. we always move forward in the file. */
  2041. if (p >= cof->raw_syments + p->u.syment.n_value)
  2042. break;
  2043. p = cof->raw_syments + p->u.syment.n_value;
  2044. if (p > pend || p->u.syment.n_sclass != C_FILE)
  2045. break;
  2046. }
  2047. }
  2048. /* Now wander though the raw linenumbers of the section. */
  2049. /* If we have been called on this section before, and the offset we
  2050. want is further down then we can prime the lookup loop. */
  2051. sec_data = coff_section_data (abfd, section);
  2052. if (sec_data != NULL
  2053. && sec_data->i > 0
  2054. && offset >= sec_data->offset)
  2055. {
  2056. i = sec_data->i;
  2057. *functionname_ptr = sec_data->function;
  2058. line_base = sec_data->line_base;
  2059. }
  2060. else
  2061. {
  2062. i = 0;
  2063. line_base = 0;
  2064. }
  2065. if (section->lineno != NULL)
  2066. {
  2067. bfd_vma last_value = 0;
  2068. l = &section->lineno[i];
  2069. for (; i < section->lineno_count; i++)
  2070. {
  2071. if (l->line_number == 0)
  2072. {
  2073. /* Get the symbol this line number points at. */
  2074. coff_symbol_type *coff = (coff_symbol_type *) (l->u.sym);
  2075. if (coff->symbol.value > offset)
  2076. break;
  2077. *functionname_ptr = coff->symbol.name;
  2078. last_value = coff->symbol.value;
  2079. if (coff->native)
  2080. {
  2081. combined_entry_type *s = coff->native;
  2082. BFD_ASSERT (s->is_sym);
  2083. s = s + 1 + s->u.syment.n_numaux;
  2084. /* In XCOFF a debugging symbol can follow the
  2085. function symbol. */
  2086. if (s->u.syment.n_scnum == N_DEBUG)
  2087. s = s + 1 + s->u.syment.n_numaux;
  2088. /* S should now point to the .bf of the function. */
  2089. if (s->u.syment.n_numaux)
  2090. {
  2091. /* The linenumber is stored in the auxent. */
  2092. union internal_auxent *a = &((s + 1)->u.auxent);
  2093. line_base = a->x_sym.x_misc.x_lnsz.x_lnno;
  2094. *line_ptr = line_base;
  2095. }
  2096. }
  2097. }
  2098. else
  2099. {
  2100. if (l->u.offset > offset)
  2101. break;
  2102. *line_ptr = l->line_number + line_base - 1;
  2103. }
  2104. l++;
  2105. }
  2106. /* If we fell off the end of the loop, then assume that this
  2107. symbol has no line number info. Otherwise, symbols with no
  2108. line number info get reported with the line number of the
  2109. last line of the last symbol which does have line number
  2110. info. We use 0x100 as a slop to account for cases where the
  2111. last line has executable code. */
  2112. if (i >= section->lineno_count
  2113. && last_value != 0
  2114. && offset - last_value > 0x100)
  2115. {
  2116. *functionname_ptr = NULL;
  2117. *line_ptr = 0;
  2118. }
  2119. }
  2120. /* Cache the results for the next call. */
  2121. if (sec_data == NULL && section->owner == abfd)
  2122. {
  2123. amt = sizeof (struct coff_section_tdata);
  2124. section->used_by_bfd = bfd_zalloc (abfd, amt);
  2125. sec_data = (struct coff_section_tdata *) section->used_by_bfd;
  2126. }
  2127. if (sec_data != NULL)
  2128. {
  2129. sec_data->offset = offset;
  2130. sec_data->i = i - 1;
  2131. sec_data->function = *functionname_ptr;
  2132. sec_data->line_base = line_base;
  2133. }
  2134. return TRUE;
  2135. }
  2136. bfd_boolean
  2137. coff_find_nearest_line (bfd *abfd,
  2138. asymbol **symbols,
  2139. asection *section,
  2140. bfd_vma offset,
  2141. const char **filename_ptr,
  2142. const char **functionname_ptr,
  2143. unsigned int *line_ptr,
  2144. unsigned int *discriminator_ptr)
  2145. {
  2146. if (discriminator_ptr)
  2147. *discriminator_ptr = 0;
  2148. return coff_find_nearest_line_with_names (abfd, symbols, section, offset,
  2149. filename_ptr, functionname_ptr,
  2150. line_ptr, dwarf_debug_sections);
  2151. }
  2152. bfd_boolean
  2153. coff_find_inliner_info (bfd *abfd,
  2154. const char **filename_ptr,
  2155. const char **functionname_ptr,
  2156. unsigned int *line_ptr)
  2157. {
  2158. bfd_boolean found;
  2159. found = _bfd_dwarf2_find_inliner_info (abfd, filename_ptr,
  2160. functionname_ptr, line_ptr,
  2161. &coff_data(abfd)->dwarf2_find_line_info);
  2162. return (found);
  2163. }
  2164. int
  2165. coff_sizeof_headers (bfd *abfd, struct bfd_link_info *info)
  2166. {
  2167. size_t size;
  2168. if (!bfd_link_relocatable (info))
  2169. size = bfd_coff_filhsz (abfd) + bfd_coff_aoutsz (abfd);
  2170. else
  2171. size = bfd_coff_filhsz (abfd);
  2172. size += abfd->section_count * bfd_coff_scnhsz (abfd);
  2173. return size;
  2174. }
  2175. /* Change the class of a coff symbol held by BFD. */
  2176. bfd_boolean
  2177. bfd_coff_set_symbol_class (bfd * abfd,
  2178. asymbol * symbol,
  2179. unsigned int symbol_class)
  2180. {
  2181. coff_symbol_type * csym;
  2182. csym = coff_symbol_from (symbol);
  2183. if (csym == NULL)
  2184. {
  2185. bfd_set_error (bfd_error_invalid_operation);
  2186. return FALSE;
  2187. }
  2188. else if (csym->native == NULL)
  2189. {
  2190. /* This is an alien symbol which no native coff backend data.
  2191. We cheat here by creating a fake native entry for it and
  2192. then filling in the class. This code is based on that in
  2193. coff_write_alien_symbol(). */
  2194. combined_entry_type * native;
  2195. bfd_size_type amt = sizeof (* native);
  2196. native = (combined_entry_type *) bfd_zalloc (abfd, amt);
  2197. if (native == NULL)
  2198. return FALSE;
  2199. native->is_sym = TRUE;
  2200. native->u.syment.n_type = T_NULL;
  2201. native->u.syment.n_sclass = symbol_class;
  2202. if (bfd_is_und_section (symbol->section))
  2203. {
  2204. native->u.syment.n_scnum = N_UNDEF;
  2205. native->u.syment.n_value = symbol->value;
  2206. }
  2207. else if (bfd_is_com_section (symbol->section))
  2208. {
  2209. native->u.syment.n_scnum = N_UNDEF;
  2210. native->u.syment.n_value = symbol->value;
  2211. }
  2212. else
  2213. {
  2214. native->u.syment.n_scnum =
  2215. symbol->section->output_section->target_index;
  2216. native->u.syment.n_value = (symbol->value
  2217. + symbol->section->output_offset);
  2218. if (! obj_pe (abfd))
  2219. native->u.syment.n_value += symbol->section->output_section->vma;
  2220. /* Copy the any flags from the file header into the symbol.
  2221. FIXME: Why? */
  2222. native->u.syment.n_flags = bfd_asymbol_bfd (& csym->symbol)->flags;
  2223. }
  2224. csym->native = native;
  2225. }
  2226. else
  2227. csym->native->u.syment.n_sclass = symbol_class;
  2228. return TRUE;
  2229. }
  2230. bfd_boolean
  2231. _bfd_coff_section_already_linked (bfd *abfd,
  2232. asection *sec,
  2233. struct bfd_link_info *info)
  2234. {
  2235. flagword flags;
  2236. const char *name, *key;
  2237. struct bfd_section_already_linked *l;
  2238. struct bfd_section_already_linked_hash_entry *already_linked_list;
  2239. struct coff_comdat_info *s_comdat;
  2240. flags = sec->flags;
  2241. if ((flags & SEC_LINK_ONCE) == 0)
  2242. return FALSE;
  2243. /* The COFF backend linker doesn't support group sections. */
  2244. if ((flags & SEC_GROUP) != 0)
  2245. return FALSE;
  2246. name = bfd_get_section_name (abfd, sec);
  2247. s_comdat = bfd_coff_get_comdat_section (abfd, sec);
  2248. if (s_comdat != NULL)
  2249. key = s_comdat->name;
  2250. else
  2251. {
  2252. if (CONST_STRNEQ (name, ".gnu.linkonce.")
  2253. && (key = strchr (name + sizeof (".gnu.linkonce.") - 1, '.')) != NULL)
  2254. key++;
  2255. else
  2256. /* FIXME: gcc as of 2011-09 emits sections like .text$<key>,
  2257. .xdata$<key> and .pdata$<key> only the first of which has a
  2258. comdat key. Should these all match the LTO IR key? */
  2259. key = name;
  2260. }
  2261. already_linked_list = bfd_section_already_linked_table_lookup (key);
  2262. for (l = already_linked_list->entry; l != NULL; l = l->next)
  2263. {
  2264. struct coff_comdat_info *l_comdat;
  2265. l_comdat = bfd_coff_get_comdat_section (l->sec->owner, l->sec);
  2266. /* The section names must match, and both sections must be
  2267. comdat and have the same comdat name, or both sections must
  2268. be non-comdat. LTO IR plugin sections are an exception. They
  2269. are always named .gnu.linkonce.t.<key> (<key> is some string)
  2270. and match any comdat section with comdat name of <key>, and
  2271. any linkonce section with the same suffix, ie.
  2272. .gnu.linkonce.*.<key>. */
  2273. if (((s_comdat != NULL) == (l_comdat != NULL)
  2274. && strcmp (name, l->sec->name) == 0)
  2275. || (l->sec->owner->flags & BFD_PLUGIN) != 0)
  2276. {
  2277. /* The section has already been linked. See if we should
  2278. issue a warning. */
  2279. return _bfd_handle_already_linked (sec, l, info);
  2280. }
  2281. }
  2282. /* This is the first section with this name. Record it. */
  2283. if (!bfd_section_already_linked_table_insert (already_linked_list, sec))
  2284. info->callbacks->einfo (_("%F%P: already_linked_table: %E\n"));
  2285. return FALSE;
  2286. }
  2287. /* Initialize COOKIE for input bfd ABFD. */
  2288. static bfd_boolean
  2289. init_reloc_cookie (struct coff_reloc_cookie *cookie,
  2290. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2291. bfd *abfd)
  2292. {
  2293. /* Sometimes the symbol table does not yet have been loaded here. */
  2294. bfd_coff_slurp_symbol_table (abfd);
  2295. cookie->abfd = abfd;
  2296. cookie->sym_hashes = obj_coff_sym_hashes (abfd);
  2297. cookie->symbols = obj_symbols (abfd);
  2298. return TRUE;
  2299. }
  2300. /* Free the memory allocated by init_reloc_cookie, if appropriate. */
  2301. static void
  2302. fini_reloc_cookie (struct coff_reloc_cookie *cookie ATTRIBUTE_UNUSED,
  2303. bfd *abfd ATTRIBUTE_UNUSED)
  2304. {
  2305. /* Nothing to do. */
  2306. }
  2307. /* Initialize the relocation information in COOKIE for input section SEC
  2308. of input bfd ABFD. */
  2309. static bfd_boolean
  2310. init_reloc_cookie_rels (struct coff_reloc_cookie *cookie,
  2311. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2312. bfd *abfd,
  2313. asection *sec)
  2314. {
  2315. if (sec->reloc_count == 0)
  2316. {
  2317. cookie->rels = NULL;
  2318. cookie->relend = NULL;
  2319. cookie->rel = NULL;
  2320. return TRUE;
  2321. }
  2322. cookie->rels = _bfd_coff_read_internal_relocs (abfd, sec, FALSE, NULL, 0, NULL);
  2323. if (cookie->rels == NULL)
  2324. return FALSE;
  2325. cookie->rel = cookie->rels;
  2326. cookie->relend = (cookie->rels + sec->reloc_count);
  2327. return TRUE;
  2328. }
  2329. /* Free the memory allocated by init_reloc_cookie_rels,
  2330. if appropriate. */
  2331. static void
  2332. fini_reloc_cookie_rels (struct coff_reloc_cookie *cookie,
  2333. asection *sec)
  2334. {
  2335. if (cookie->rels && coff_section_data (NULL, sec)->relocs != cookie->rels)
  2336. free (cookie->rels);
  2337. }
  2338. /* Initialize the whole of COOKIE for input section SEC. */
  2339. static bfd_boolean
  2340. init_reloc_cookie_for_section (struct coff_reloc_cookie *cookie,
  2341. struct bfd_link_info *info,
  2342. asection *sec)
  2343. {
  2344. if (!init_reloc_cookie (cookie, info, sec->owner))
  2345. return FALSE;
  2346. if (!init_reloc_cookie_rels (cookie, info, sec->owner, sec))
  2347. {
  2348. fini_reloc_cookie (cookie, sec->owner);
  2349. return FALSE;
  2350. }
  2351. return TRUE;
  2352. }
  2353. /* Free the memory allocated by init_reloc_cookie_for_section,
  2354. if appropriate. */
  2355. static void
  2356. fini_reloc_cookie_for_section (struct coff_reloc_cookie *cookie,
  2357. asection *sec)
  2358. {
  2359. fini_reloc_cookie_rels (cookie, sec);
  2360. fini_reloc_cookie (cookie, sec->owner);
  2361. }
  2362. static asection *
  2363. _bfd_coff_gc_mark_hook (asection *sec,
  2364. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  2365. struct internal_reloc *rel ATTRIBUTE_UNUSED,
  2366. struct coff_link_hash_entry *h,
  2367. struct internal_syment *sym)
  2368. {
  2369. if (h != NULL)
  2370. {
  2371. switch (h->root.type)
  2372. {
  2373. case bfd_link_hash_defined:
  2374. case bfd_link_hash_defweak:
  2375. return h->root.u.def.section;
  2376. case bfd_link_hash_common:
  2377. return h->root.u.c.p->section;
  2378. case bfd_link_hash_undefined:
  2379. case bfd_link_hash_undefweak:
  2380. default:
  2381. break;
  2382. }
  2383. return NULL;
  2384. }
  2385. return coff_section_from_bfd_index (sec->owner, sym->n_scnum);
  2386. }
  2387. /* COOKIE->rel describes a relocation against section SEC, which is
  2388. a section we've decided to keep. Return the section that contains
  2389. the relocation symbol, or NULL if no section contains it. */
  2390. static asection *
  2391. _bfd_coff_gc_mark_rsec (struct bfd_link_info *info, asection *sec,
  2392. coff_gc_mark_hook_fn gc_mark_hook,
  2393. struct coff_reloc_cookie *cookie)
  2394. {
  2395. struct coff_link_hash_entry *h;
  2396. h = cookie->sym_hashes[cookie->rel->r_symndx];
  2397. if (h != NULL)
  2398. {
  2399. while (h->root.type == bfd_link_hash_indirect
  2400. || h->root.type == bfd_link_hash_warning)
  2401. h = (struct coff_link_hash_entry *) h->root.u.i.link;
  2402. return (*gc_mark_hook) (sec, info, cookie->rel, h, NULL);
  2403. }
  2404. return (*gc_mark_hook) (sec, info, cookie->rel, NULL,
  2405. &(cookie->symbols
  2406. + obj_convert (sec->owner)[cookie->rel->r_symndx])->native->u.syment);
  2407. }
  2408. static bfd_boolean _bfd_coff_gc_mark
  2409. (struct bfd_link_info *, asection *, coff_gc_mark_hook_fn);
  2410. /* COOKIE->rel describes a relocation against section SEC, which is
  2411. a section we've decided to keep. Mark the section that contains
  2412. the relocation symbol. */
  2413. static bfd_boolean
  2414. _bfd_coff_gc_mark_reloc (struct bfd_link_info *info,
  2415. asection *sec,
  2416. coff_gc_mark_hook_fn gc_mark_hook,
  2417. struct coff_reloc_cookie *cookie)
  2418. {
  2419. asection *rsec;
  2420. rsec = _bfd_coff_gc_mark_rsec (info, sec, gc_mark_hook, cookie);
  2421. if (rsec && !rsec->gc_mark)
  2422. {
  2423. if (bfd_get_flavour (rsec->owner) != bfd_target_coff_flavour)
  2424. rsec->gc_mark = 1;
  2425. else if (!_bfd_coff_gc_mark (info, rsec, gc_mark_hook))
  2426. return FALSE;
  2427. }
  2428. return TRUE;
  2429. }
  2430. /* The mark phase of garbage collection. For a given section, mark
  2431. it and any sections in this section's group, and all the sections
  2432. which define symbols to which it refers. */
  2433. static bfd_boolean
  2434. _bfd_coff_gc_mark (struct bfd_link_info *info,
  2435. asection *sec,
  2436. coff_gc_mark_hook_fn gc_mark_hook)
  2437. {
  2438. bfd_boolean ret = TRUE;
  2439. sec->gc_mark = 1;
  2440. /* Look through the section relocs. */
  2441. if ((sec->flags & SEC_RELOC) != 0
  2442. && sec->reloc_count > 0)
  2443. {
  2444. struct coff_reloc_cookie cookie;
  2445. if (!init_reloc_cookie_for_section (&cookie, info, sec))
  2446. ret = FALSE;
  2447. else
  2448. {
  2449. for (; cookie.rel < cookie.relend; cookie.rel++)
  2450. {
  2451. if (!_bfd_coff_gc_mark_reloc (info, sec, gc_mark_hook, &cookie))
  2452. {
  2453. ret = FALSE;
  2454. break;
  2455. }
  2456. }
  2457. fini_reloc_cookie_for_section (&cookie, sec);
  2458. }
  2459. }
  2460. return ret;
  2461. }
  2462. static bfd_boolean
  2463. _bfd_coff_gc_mark_extra_sections (struct bfd_link_info *info,
  2464. coff_gc_mark_hook_fn mark_hook ATTRIBUTE_UNUSED)
  2465. {
  2466. bfd *ibfd;
  2467. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  2468. {
  2469. asection *isec;
  2470. bfd_boolean some_kept;
  2471. if (bfd_get_flavour (ibfd) != bfd_target_elf_flavour)
  2472. continue;
  2473. /* Ensure all linker created sections are kept, and see whether
  2474. any other section is already marked. */
  2475. some_kept = FALSE;
  2476. for (isec = ibfd->sections; isec != NULL; isec = isec->next)
  2477. {
  2478. if ((isec->flags & SEC_LINKER_CREATED) != 0)
  2479. isec->gc_mark = 1;
  2480. else if (isec->gc_mark)
  2481. some_kept = TRUE;
  2482. }
  2483. /* If no section in this file will be kept, then we can
  2484. toss out debug sections. */
  2485. if (!some_kept)
  2486. continue;
  2487. /* Keep debug and special sections like .comment when they are
  2488. not part of a group, or when we have single-member groups. */
  2489. for (isec = ibfd->sections; isec != NULL; isec = isec->next)
  2490. if ((isec->flags & SEC_DEBUGGING) != 0
  2491. || (isec->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
  2492. isec->gc_mark = 1;
  2493. }
  2494. return TRUE;
  2495. }
  2496. /* Sweep symbols in swept sections. Called via coff_link_hash_traverse. */
  2497. static bfd_boolean
  2498. coff_gc_sweep_symbol (struct coff_link_hash_entry *h,
  2499. void *data ATTRIBUTE_UNUSED)
  2500. {
  2501. if (h->root.type == bfd_link_hash_warning)
  2502. h = (struct coff_link_hash_entry *) h->root.u.i.link;
  2503. if ((h->root.type == bfd_link_hash_defined
  2504. || h->root.type == bfd_link_hash_defweak)
  2505. && !h->root.u.def.section->gc_mark
  2506. && !(h->root.u.def.section->owner->flags & DYNAMIC))
  2507. {
  2508. /* Do our best to hide the symbol. */
  2509. h->root.u.def.section = bfd_und_section_ptr;
  2510. h->symbol_class = C_HIDDEN;
  2511. }
  2512. return TRUE;
  2513. }
  2514. /* The sweep phase of garbage collection. Remove all garbage sections. */
  2515. typedef bfd_boolean (*gc_sweep_hook_fn)
  2516. (bfd *, struct bfd_link_info *, asection *, const struct internal_reloc *);
  2517. static bfd_boolean
  2518. coff_gc_sweep (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
  2519. {
  2520. bfd *sub;
  2521. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  2522. {
  2523. asection *o;
  2524. if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
  2525. continue;
  2526. for (o = sub->sections; o != NULL; o = o->next)
  2527. {
  2528. /* Keep debug and special sections. */
  2529. if ((o->flags & (SEC_DEBUGGING | SEC_LINKER_CREATED)) != 0
  2530. || (o->flags & (SEC_ALLOC | SEC_LOAD | SEC_RELOC)) == 0)
  2531. o->gc_mark = 1;
  2532. else if (CONST_STRNEQ (o->name, ".idata")
  2533. || CONST_STRNEQ (o->name, ".pdata")
  2534. || CONST_STRNEQ (o->name, ".xdata")
  2535. || CONST_STRNEQ (o->name, ".rsrc"))
  2536. o->gc_mark = 1;
  2537. if (o->gc_mark)
  2538. continue;
  2539. /* Skip sweeping sections already excluded. */
  2540. if (o->flags & SEC_EXCLUDE)
  2541. continue;
  2542. /* Since this is early in the link process, it is simple
  2543. to remove a section from the output. */
  2544. o->flags |= SEC_EXCLUDE;
  2545. if (info->print_gc_sections && o->size != 0)
  2546. _bfd_error_handler (_("Removing unused section '%s' in file '%B'"), sub, o->name);
  2547. #if 0
  2548. /* But we also have to update some of the relocation
  2549. info we collected before. */
  2550. if (gc_sweep_hook
  2551. && (o->flags & SEC_RELOC) != 0
  2552. && o->reloc_count > 0
  2553. && !bfd_is_abs_section (o->output_section))
  2554. {
  2555. struct internal_reloc *internal_relocs;
  2556. bfd_boolean r;
  2557. internal_relocs
  2558. = _bfd_coff_link_read_relocs (o->owner, o, NULL, NULL,
  2559. info->keep_memory);
  2560. if (internal_relocs == NULL)
  2561. return FALSE;
  2562. r = (*gc_sweep_hook) (o->owner, info, o, internal_relocs);
  2563. if (coff_section_data (o)->relocs != internal_relocs)
  2564. free (internal_relocs);
  2565. if (!r)
  2566. return FALSE;
  2567. }
  2568. #endif
  2569. }
  2570. }
  2571. /* Remove the symbols that were in the swept sections from the dynamic
  2572. symbol table. */
  2573. coff_link_hash_traverse (coff_hash_table (info), coff_gc_sweep_symbol,
  2574. NULL);
  2575. return TRUE;
  2576. }
  2577. /* Keep all sections containing symbols undefined on the command-line,
  2578. and the section containing the entry symbol. */
  2579. static void
  2580. _bfd_coff_gc_keep (struct bfd_link_info *info)
  2581. {
  2582. struct bfd_sym_chain *sym;
  2583. for (sym = info->gc_sym_list; sym != NULL; sym = sym->next)
  2584. {
  2585. struct coff_link_hash_entry *h;
  2586. h = coff_link_hash_lookup (coff_hash_table (info), sym->name,
  2587. FALSE, FALSE, FALSE);
  2588. if (h != NULL
  2589. && (h->root.type == bfd_link_hash_defined
  2590. || h->root.type == bfd_link_hash_defweak)
  2591. && !bfd_is_abs_section (h->root.u.def.section))
  2592. h->root.u.def.section->flags |= SEC_KEEP;
  2593. }
  2594. }
  2595. /* Do mark and sweep of unused sections. */
  2596. bfd_boolean
  2597. bfd_coff_gc_sections (bfd *abfd ATTRIBUTE_UNUSED, struct bfd_link_info *info)
  2598. {
  2599. bfd *sub;
  2600. /* FIXME: Should we implement this? */
  2601. #if 0
  2602. const bfd_coff_backend_data *bed = coff_backend_info (abfd);
  2603. if (!bed->can_gc_sections
  2604. || !is_coff_hash_table (info->hash))
  2605. {
  2606. (*_bfd_error_handler)(_("Warning: gc-sections option ignored"));
  2607. return TRUE;
  2608. }
  2609. #endif
  2610. _bfd_coff_gc_keep (info);
  2611. /* Grovel through relocs to find out who stays ... */
  2612. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  2613. {
  2614. asection *o;
  2615. if (bfd_get_flavour (sub) != bfd_target_coff_flavour)
  2616. continue;
  2617. for (o = sub->sections; o != NULL; o = o->next)
  2618. {
  2619. if (((o->flags & (SEC_EXCLUDE | SEC_KEEP)) == SEC_KEEP
  2620. || CONST_STRNEQ (o->name, ".vectors")
  2621. || CONST_STRNEQ (o->name, ".ctors")
  2622. || CONST_STRNEQ (o->name, ".dtors"))
  2623. && !o->gc_mark)
  2624. {
  2625. if (!_bfd_coff_gc_mark (info, o, _bfd_coff_gc_mark_hook))
  2626. return FALSE;
  2627. }
  2628. }
  2629. }
  2630. /* Allow the backend to mark additional target specific sections. */
  2631. _bfd_coff_gc_mark_extra_sections (info, _bfd_coff_gc_mark_hook);
  2632. /* ... and mark SEC_EXCLUDE for those that go. */
  2633. return coff_gc_sweep (abfd, info);
  2634. }