coffgen.c 91 KB

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