cofflink.c 91 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173
  1. /* COFF specific linker code.
  2. Copyright (C) 1994-2022 Free Software Foundation, Inc.
  3. Written by Ian Lance Taylor, 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. /* This file contains the COFF backend linker code. */
  18. #include "sysdep.h"
  19. #include "bfd.h"
  20. #include "bfdlink.h"
  21. #include "libbfd.h"
  22. #include "coff/internal.h"
  23. #include "libcoff.h"
  24. #include "safe-ctype.h"
  25. static bool coff_link_add_object_symbols (bfd *, struct bfd_link_info *);
  26. static bool coff_link_check_archive_element
  27. (bfd *, struct bfd_link_info *, struct bfd_link_hash_entry *, const char *,
  28. bool *);
  29. static bool coff_link_add_symbols (bfd *, struct bfd_link_info *);
  30. /* Return TRUE if SYM is a weak, external symbol. */
  31. #define IS_WEAK_EXTERNAL(abfd, sym) \
  32. ((sym).n_sclass == C_WEAKEXT \
  33. || (obj_pe (abfd) && (sym).n_sclass == C_NT_WEAK))
  34. /* Return TRUE if SYM is an external symbol. */
  35. #define IS_EXTERNAL(abfd, sym) \
  36. ((sym).n_sclass == C_EXT || IS_WEAK_EXTERNAL (abfd, sym))
  37. /* Define macros so that the ISFCN, et. al., macros work correctly.
  38. These macros are defined in include/coff/internal.h in terms of
  39. N_TMASK, etc. These definitions require a user to define local
  40. variables with the appropriate names, and with values from the
  41. coff_data (abfd) structure. */
  42. #define N_TMASK n_tmask
  43. #define N_BTSHFT n_btshft
  44. #define N_BTMASK n_btmask
  45. /* Create an entry in a COFF linker hash table. */
  46. struct bfd_hash_entry *
  47. _bfd_coff_link_hash_newfunc (struct bfd_hash_entry *entry,
  48. struct bfd_hash_table *table,
  49. const char *string)
  50. {
  51. struct coff_link_hash_entry *ret = (struct coff_link_hash_entry *) entry;
  52. /* Allocate the structure if it has not already been allocated by a
  53. subclass. */
  54. if (ret == (struct coff_link_hash_entry *) NULL)
  55. ret = ((struct coff_link_hash_entry *)
  56. bfd_hash_allocate (table, sizeof (struct coff_link_hash_entry)));
  57. if (ret == (struct coff_link_hash_entry *) NULL)
  58. return (struct bfd_hash_entry *) ret;
  59. /* Call the allocation method of the superclass. */
  60. ret = ((struct coff_link_hash_entry *)
  61. _bfd_link_hash_newfunc ((struct bfd_hash_entry *) ret,
  62. table, string));
  63. if (ret != (struct coff_link_hash_entry *) NULL)
  64. {
  65. /* Set local fields. */
  66. ret->indx = -1;
  67. ret->type = T_NULL;
  68. ret->symbol_class = C_NULL;
  69. ret->numaux = 0;
  70. ret->auxbfd = NULL;
  71. ret->aux = NULL;
  72. }
  73. return (struct bfd_hash_entry *) ret;
  74. }
  75. /* Initialize a COFF linker hash table. */
  76. bool
  77. _bfd_coff_link_hash_table_init (struct coff_link_hash_table *table,
  78. bfd *abfd,
  79. struct bfd_hash_entry *(*newfunc) (struct bfd_hash_entry *,
  80. struct bfd_hash_table *,
  81. const char *),
  82. unsigned int entsize)
  83. {
  84. memset (&table->stab_info, 0, sizeof (table->stab_info));
  85. return _bfd_link_hash_table_init (&table->root, abfd, newfunc, entsize);
  86. }
  87. /* Create a COFF linker hash table. */
  88. struct bfd_link_hash_table *
  89. _bfd_coff_link_hash_table_create (bfd *abfd)
  90. {
  91. struct coff_link_hash_table *ret;
  92. size_t amt = sizeof (struct coff_link_hash_table);
  93. ret = (struct coff_link_hash_table *) bfd_malloc (amt);
  94. if (ret == NULL)
  95. return NULL;
  96. if (! _bfd_coff_link_hash_table_init (ret, abfd,
  97. _bfd_coff_link_hash_newfunc,
  98. sizeof (struct coff_link_hash_entry)))
  99. {
  100. free (ret);
  101. return (struct bfd_link_hash_table *) NULL;
  102. }
  103. return &ret->root;
  104. }
  105. /* Create an entry in a COFF debug merge hash table. */
  106. struct bfd_hash_entry *
  107. _bfd_coff_debug_merge_hash_newfunc (struct bfd_hash_entry *entry,
  108. struct bfd_hash_table *table,
  109. const char *string)
  110. {
  111. struct coff_debug_merge_hash_entry *ret =
  112. (struct coff_debug_merge_hash_entry *) entry;
  113. /* Allocate the structure if it has not already been allocated by a
  114. subclass. */
  115. if (ret == (struct coff_debug_merge_hash_entry *) NULL)
  116. ret = ((struct coff_debug_merge_hash_entry *)
  117. bfd_hash_allocate (table,
  118. sizeof (struct coff_debug_merge_hash_entry)));
  119. if (ret == (struct coff_debug_merge_hash_entry *) NULL)
  120. return (struct bfd_hash_entry *) ret;
  121. /* Call the allocation method of the superclass. */
  122. ret = ((struct coff_debug_merge_hash_entry *)
  123. bfd_hash_newfunc ((struct bfd_hash_entry *) ret, table, string));
  124. if (ret != (struct coff_debug_merge_hash_entry *) NULL)
  125. {
  126. /* Set local fields. */
  127. ret->types = NULL;
  128. }
  129. return (struct bfd_hash_entry *) ret;
  130. }
  131. /* Given a COFF BFD, add symbols to the global hash table as
  132. appropriate. */
  133. bool
  134. _bfd_coff_link_add_symbols (bfd *abfd, struct bfd_link_info *info)
  135. {
  136. switch (bfd_get_format (abfd))
  137. {
  138. case bfd_object:
  139. return coff_link_add_object_symbols (abfd, info);
  140. case bfd_archive:
  141. return _bfd_generic_link_add_archive_symbols
  142. (abfd, info, coff_link_check_archive_element);
  143. default:
  144. bfd_set_error (bfd_error_wrong_format);
  145. return false;
  146. }
  147. }
  148. /* Add symbols from a COFF object file. */
  149. static bool
  150. coff_link_add_object_symbols (bfd *abfd, struct bfd_link_info *info)
  151. {
  152. if (! _bfd_coff_get_external_symbols (abfd))
  153. return false;
  154. if (! coff_link_add_symbols (abfd, info))
  155. return false;
  156. if (! info->keep_memory
  157. && ! _bfd_coff_free_symbols (abfd))
  158. return false;
  159. return true;
  160. }
  161. /* Check a single archive element to see if we need to include it in
  162. the link. *PNEEDED is set according to whether this element is
  163. needed in the link or not. This is called via
  164. _bfd_generic_link_add_archive_symbols. */
  165. static bool
  166. coff_link_check_archive_element (bfd *abfd,
  167. struct bfd_link_info *info,
  168. struct bfd_link_hash_entry *h,
  169. const char *name,
  170. bool *pneeded)
  171. {
  172. *pneeded = false;
  173. /* PR 22369 - Skip non COFF objects in the archive. */
  174. if (! bfd_family_coff (abfd))
  175. return true;
  176. /* We are only interested in symbols that are currently undefined.
  177. If a symbol is currently known to be common, COFF linkers do not
  178. bring in an object file which defines it. */
  179. if (h->type != bfd_link_hash_undefined)
  180. return true;
  181. /* If the archive element has already been loaded then one
  182. of the symbols defined by that element might have been
  183. made undefined due to being in a discarded section. */
  184. if (((struct coff_link_hash_entry *) h)->indx == -3)
  185. return true;
  186. /* Include this element? */
  187. if (!(*info->callbacks->add_archive_element) (info, abfd, name, &abfd))
  188. return true;
  189. *pneeded = true;
  190. return bfd_link_add_symbols (abfd, info);
  191. }
  192. /* Add all the symbols from an object file to the hash table. */
  193. static bool
  194. coff_link_add_symbols (bfd *abfd,
  195. struct bfd_link_info *info)
  196. {
  197. unsigned int n_tmask = coff_data (abfd)->local_n_tmask;
  198. unsigned int n_btshft = coff_data (abfd)->local_n_btshft;
  199. unsigned int n_btmask = coff_data (abfd)->local_n_btmask;
  200. bool keep_syms;
  201. bool default_copy;
  202. bfd_size_type symcount;
  203. struct coff_link_hash_entry **sym_hash;
  204. bfd_size_type symesz;
  205. bfd_byte *esym;
  206. bfd_byte *esym_end;
  207. bfd_size_type amt;
  208. symcount = obj_raw_syment_count (abfd);
  209. if (symcount == 0)
  210. return true; /* Nothing to do. */
  211. /* Keep the symbols during this function, in case the linker needs
  212. to read the generic symbols in order to report an error message. */
  213. keep_syms = obj_coff_keep_syms (abfd);
  214. obj_coff_keep_syms (abfd) = true;
  215. if (info->keep_memory)
  216. default_copy = false;
  217. else
  218. default_copy = true;
  219. /* We keep a list of the linker hash table entries that correspond
  220. to particular symbols. */
  221. amt = symcount * sizeof (struct coff_link_hash_entry *);
  222. sym_hash = (struct coff_link_hash_entry **) bfd_zalloc (abfd, amt);
  223. if (sym_hash == NULL)
  224. goto error_return;
  225. obj_coff_sym_hashes (abfd) = sym_hash;
  226. symesz = bfd_coff_symesz (abfd);
  227. BFD_ASSERT (symesz == bfd_coff_auxesz (abfd));
  228. esym = (bfd_byte *) obj_coff_external_syms (abfd);
  229. esym_end = esym + symcount * symesz;
  230. while (esym < esym_end)
  231. {
  232. struct internal_syment sym;
  233. enum coff_symbol_classification classification;
  234. bool copy;
  235. bfd_coff_swap_sym_in (abfd, esym, &sym);
  236. classification = bfd_coff_classify_symbol (abfd, &sym);
  237. if (classification != COFF_SYMBOL_LOCAL)
  238. {
  239. const char *name;
  240. char buf[SYMNMLEN + 1];
  241. flagword flags;
  242. asection *section;
  243. bfd_vma value;
  244. bool addit;
  245. bool discarded = false;
  246. /* This symbol is externally visible. */
  247. name = _bfd_coff_internal_syment_name (abfd, &sym, buf);
  248. if (name == NULL)
  249. goto error_return;
  250. /* We must copy the name into memory if we got it from the
  251. syment itself, rather than the string table. */
  252. copy = default_copy;
  253. if (sym._n._n_n._n_zeroes != 0
  254. || sym._n._n_n._n_offset == 0)
  255. copy = true;
  256. value = sym.n_value;
  257. switch (classification)
  258. {
  259. default:
  260. abort ();
  261. case COFF_SYMBOL_GLOBAL:
  262. flags = BSF_EXPORT | BSF_GLOBAL;
  263. section = coff_section_from_bfd_index (abfd, sym.n_scnum);
  264. if (discarded_section (section))
  265. {
  266. discarded = true;
  267. section = bfd_und_section_ptr;
  268. }
  269. else if (! obj_pe (abfd))
  270. value -= section->vma;
  271. break;
  272. case COFF_SYMBOL_UNDEFINED:
  273. flags = 0;
  274. section = bfd_und_section_ptr;
  275. break;
  276. case COFF_SYMBOL_COMMON:
  277. flags = BSF_GLOBAL;
  278. section = bfd_com_section_ptr;
  279. break;
  280. case COFF_SYMBOL_PE_SECTION:
  281. flags = BSF_SECTION_SYM | BSF_GLOBAL;
  282. section = coff_section_from_bfd_index (abfd, sym.n_scnum);
  283. if (discarded_section (section))
  284. section = bfd_und_section_ptr;
  285. break;
  286. }
  287. if (IS_WEAK_EXTERNAL (abfd, sym))
  288. flags = BSF_WEAK;
  289. addit = true;
  290. /* In the PE format, section symbols actually refer to the
  291. start of the output section. We handle them specially
  292. here. */
  293. if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
  294. {
  295. *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
  296. name, false, copy, false);
  297. if (*sym_hash != NULL)
  298. {
  299. if (((*sym_hash)->coff_link_hash_flags
  300. & COFF_LINK_HASH_PE_SECTION_SYMBOL) == 0
  301. && (*sym_hash)->root.type != bfd_link_hash_undefined
  302. && (*sym_hash)->root.type != bfd_link_hash_undefweak)
  303. _bfd_error_handler
  304. (_("warning: symbol `%s' is both section and non-section"),
  305. name);
  306. addit = false;
  307. }
  308. }
  309. /* The Microsoft Visual C compiler does string pooling by
  310. hashing the constants to an internal symbol name, and
  311. relying on the linker comdat support to discard
  312. duplicate names. However, if one string is a literal and
  313. one is a data initializer, one will end up in the .data
  314. section and one will end up in the .rdata section. The
  315. Microsoft linker will combine them into the .data
  316. section, which seems to be wrong since it might cause the
  317. literal to change.
  318. As long as there are no external references to the
  319. symbols, which there shouldn't be, we can treat the .data
  320. and .rdata instances as separate symbols. The comdat
  321. code in the linker will do the appropriate merging. Here
  322. we avoid getting a multiple definition error for one of
  323. these special symbols.
  324. FIXME: I don't think this will work in the case where
  325. there are two object files which use the constants as a
  326. literal and two object files which use it as a data
  327. initializer. One or the other of the second object files
  328. is going to wind up with an inappropriate reference. */
  329. if (obj_pe (abfd)
  330. && (classification == COFF_SYMBOL_GLOBAL
  331. || classification == COFF_SYMBOL_PE_SECTION)
  332. && coff_section_data (abfd, section) != NULL
  333. && coff_section_data (abfd, section)->comdat != NULL
  334. && startswith (name, "??_")
  335. && strcmp (name, coff_section_data (abfd, section)->comdat->name) == 0)
  336. {
  337. if (*sym_hash == NULL)
  338. *sym_hash = coff_link_hash_lookup (coff_hash_table (info),
  339. name, false, copy, false);
  340. if (*sym_hash != NULL
  341. && (*sym_hash)->root.type == bfd_link_hash_defined
  342. && coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat != NULL
  343. && strcmp (coff_section_data (abfd, (*sym_hash)->root.u.def.section)->comdat->name,
  344. coff_section_data (abfd, section)->comdat->name) == 0)
  345. addit = false;
  346. }
  347. if (addit)
  348. {
  349. if (! (bfd_coff_link_add_one_symbol
  350. (info, abfd, name, flags, section, value,
  351. (const char *) NULL, copy, false,
  352. (struct bfd_link_hash_entry **) sym_hash)))
  353. goto error_return;
  354. if (discarded)
  355. (*sym_hash)->indx = -3;
  356. }
  357. if (obj_pe (abfd) && (flags & BSF_SECTION_SYM) != 0)
  358. (*sym_hash)->coff_link_hash_flags |=
  359. COFF_LINK_HASH_PE_SECTION_SYMBOL;
  360. /* Limit the alignment of a common symbol to the possible
  361. alignment of a section. There is no point to permitting
  362. a higher alignment for a common symbol: we can not
  363. guarantee it, and it may cause us to allocate extra space
  364. in the common section. */
  365. if (section == bfd_com_section_ptr
  366. && (*sym_hash)->root.type == bfd_link_hash_common
  367. && ((*sym_hash)->root.u.c.p->alignment_power
  368. > bfd_coff_default_section_alignment_power (abfd)))
  369. (*sym_hash)->root.u.c.p->alignment_power
  370. = bfd_coff_default_section_alignment_power (abfd);
  371. if (bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd))
  372. {
  373. /* If we don't have any symbol information currently in
  374. the hash table, or if we are looking at a symbol
  375. definition, then update the symbol class and type in
  376. the hash table. */
  377. if (((*sym_hash)->symbol_class == C_NULL
  378. && (*sym_hash)->type == T_NULL)
  379. || sym.n_scnum != 0
  380. || (sym.n_value != 0
  381. && (*sym_hash)->root.type != bfd_link_hash_defined
  382. && (*sym_hash)->root.type != bfd_link_hash_defweak))
  383. {
  384. (*sym_hash)->symbol_class = sym.n_sclass;
  385. if (sym.n_type != T_NULL)
  386. {
  387. /* We want to warn if the type changed, but not
  388. if it changed from an unspecified type.
  389. Testing the whole type byte may work, but the
  390. change from (e.g.) a function of unspecified
  391. type to function of known type also wants to
  392. skip the warning. */
  393. if ((*sym_hash)->type != T_NULL
  394. && (*sym_hash)->type != sym.n_type
  395. && !(DTYPE ((*sym_hash)->type) == DTYPE (sym.n_type)
  396. && (BTYPE ((*sym_hash)->type) == T_NULL
  397. || BTYPE (sym.n_type) == T_NULL)))
  398. _bfd_error_handler
  399. /* xgettext: c-format */
  400. (_("warning: type of symbol `%s' changed"
  401. " from %d to %d in %pB"),
  402. name, (*sym_hash)->type, sym.n_type, abfd);
  403. /* We don't want to change from a meaningful
  404. base type to a null one, but if we know
  405. nothing, take what little we might now know. */
  406. if (BTYPE (sym.n_type) != T_NULL
  407. || (*sym_hash)->type == T_NULL)
  408. (*sym_hash)->type = sym.n_type;
  409. }
  410. (*sym_hash)->auxbfd = abfd;
  411. if (sym.n_numaux != 0)
  412. {
  413. union internal_auxent *alloc;
  414. unsigned int i;
  415. bfd_byte *eaux;
  416. union internal_auxent *iaux;
  417. (*sym_hash)->numaux = sym.n_numaux;
  418. alloc = ((union internal_auxent *)
  419. bfd_hash_allocate (&info->hash->table,
  420. (sym.n_numaux
  421. * sizeof (*alloc))));
  422. if (alloc == NULL)
  423. goto error_return;
  424. for (i = 0, eaux = esym + symesz, iaux = alloc;
  425. i < sym.n_numaux;
  426. i++, eaux += symesz, iaux++)
  427. bfd_coff_swap_aux_in (abfd, eaux, sym.n_type,
  428. sym.n_sclass, (int) i,
  429. sym.n_numaux, iaux);
  430. (*sym_hash)->aux = alloc;
  431. }
  432. }
  433. }
  434. if (classification == COFF_SYMBOL_PE_SECTION
  435. && (*sym_hash)->numaux != 0)
  436. {
  437. /* Some PE sections (such as .bss) have a zero size in
  438. the section header, but a non-zero size in the AUX
  439. record. Correct that here.
  440. FIXME: This is not at all the right place to do this.
  441. For example, it won't help objdump. This needs to be
  442. done when we swap in the section header. */
  443. BFD_ASSERT ((*sym_hash)->numaux == 1);
  444. if (section->size == 0)
  445. section->size = (*sym_hash)->aux[0].x_scn.x_scnlen;
  446. /* FIXME: We could test whether the section sizes
  447. matches the size in the aux entry, but apparently
  448. that sometimes fails unexpectedly. */
  449. }
  450. }
  451. esym += (sym.n_numaux + 1) * symesz;
  452. sym_hash += sym.n_numaux + 1;
  453. }
  454. /* If this is a non-traditional, non-relocatable link, try to
  455. optimize the handling of any .stab/.stabstr sections. */
  456. if (! bfd_link_relocatable (info)
  457. && ! info->traditional_format
  458. && bfd_get_flavour (info->output_bfd) == bfd_get_flavour (abfd)
  459. && (info->strip != strip_all && info->strip != strip_debugger))
  460. {
  461. asection *stabstr;
  462. stabstr = bfd_get_section_by_name (abfd, ".stabstr");
  463. if (stabstr != NULL)
  464. {
  465. bfd_size_type string_offset = 0;
  466. asection *stab;
  467. for (stab = abfd->sections; stab; stab = stab->next)
  468. if (startswith (stab->name, ".stab")
  469. && (!stab->name[5]
  470. || (stab->name[5] == '.' && ISDIGIT (stab->name[6]))))
  471. {
  472. struct coff_link_hash_table *table;
  473. struct coff_section_tdata *secdata
  474. = coff_section_data (abfd, stab);
  475. if (secdata == NULL)
  476. {
  477. amt = sizeof (struct coff_section_tdata);
  478. stab->used_by_bfd = bfd_zalloc (abfd, amt);
  479. if (stab->used_by_bfd == NULL)
  480. goto error_return;
  481. secdata = coff_section_data (abfd, stab);
  482. }
  483. table = coff_hash_table (info);
  484. if (! _bfd_link_section_stabs (abfd, &table->stab_info,
  485. stab, stabstr,
  486. &secdata->stab_info,
  487. &string_offset))
  488. goto error_return;
  489. }
  490. }
  491. }
  492. obj_coff_keep_syms (abfd) = keep_syms;
  493. return true;
  494. error_return:
  495. obj_coff_keep_syms (abfd) = keep_syms;
  496. return false;
  497. }
  498. /* Do the final link step. */
  499. bool
  500. _bfd_coff_final_link (bfd *abfd,
  501. struct bfd_link_info *info)
  502. {
  503. bfd_size_type symesz;
  504. struct coff_final_link_info flaginfo;
  505. bool debug_merge_allocated;
  506. bool long_section_names;
  507. asection *o;
  508. struct bfd_link_order *p;
  509. bfd_size_type max_sym_count;
  510. bfd_size_type max_lineno_count;
  511. bfd_size_type max_reloc_count;
  512. bfd_size_type max_output_reloc_count;
  513. bfd_size_type max_contents_size;
  514. file_ptr rel_filepos;
  515. unsigned int relsz;
  516. file_ptr line_filepos;
  517. unsigned int linesz;
  518. bfd *sub;
  519. bfd_byte *external_relocs = NULL;
  520. char strbuf[STRING_SIZE_SIZE];
  521. bfd_size_type amt;
  522. symesz = bfd_coff_symesz (abfd);
  523. flaginfo.info = info;
  524. flaginfo.output_bfd = abfd;
  525. flaginfo.strtab = NULL;
  526. flaginfo.section_info = NULL;
  527. flaginfo.last_file_index = -1;
  528. flaginfo.last_bf_index = -1;
  529. flaginfo.internal_syms = NULL;
  530. flaginfo.sec_ptrs = NULL;
  531. flaginfo.sym_indices = NULL;
  532. flaginfo.outsyms = NULL;
  533. flaginfo.linenos = NULL;
  534. flaginfo.contents = NULL;
  535. flaginfo.external_relocs = NULL;
  536. flaginfo.internal_relocs = NULL;
  537. flaginfo.global_to_static = false;
  538. debug_merge_allocated = false;
  539. coff_data (abfd)->link_info = info;
  540. flaginfo.strtab = _bfd_stringtab_init ();
  541. if (flaginfo.strtab == NULL)
  542. goto error_return;
  543. if (! coff_debug_merge_hash_table_init (&flaginfo.debug_merge))
  544. goto error_return;
  545. debug_merge_allocated = true;
  546. /* Compute the file positions for all the sections. */
  547. if (! abfd->output_has_begun)
  548. {
  549. if (! bfd_coff_compute_section_file_positions (abfd))
  550. goto error_return;
  551. }
  552. /* Count the line numbers and relocation entries required for the
  553. output file. Set the file positions for the relocs. */
  554. rel_filepos = obj_relocbase (abfd);
  555. relsz = bfd_coff_relsz (abfd);
  556. max_contents_size = 0;
  557. max_lineno_count = 0;
  558. max_reloc_count = 0;
  559. long_section_names = false;
  560. for (o = abfd->sections; o != NULL; o = o->next)
  561. {
  562. o->reloc_count = 0;
  563. o->lineno_count = 0;
  564. for (p = o->map_head.link_order; p != NULL; p = p->next)
  565. {
  566. if (p->type == bfd_indirect_link_order)
  567. {
  568. asection *sec;
  569. sec = p->u.indirect.section;
  570. /* Mark all sections which are to be included in the
  571. link. This will normally be every section. We need
  572. to do this so that we can identify any sections which
  573. the linker has decided to not include. */
  574. sec->linker_mark = true;
  575. if (info->strip == strip_none
  576. || info->strip == strip_some)
  577. o->lineno_count += sec->lineno_count;
  578. if (bfd_link_relocatable (info))
  579. o->reloc_count += sec->reloc_count;
  580. if (sec->rawsize > max_contents_size)
  581. max_contents_size = sec->rawsize;
  582. if (sec->size > max_contents_size)
  583. max_contents_size = sec->size;
  584. if (sec->lineno_count > max_lineno_count)
  585. max_lineno_count = sec->lineno_count;
  586. if (sec->reloc_count > max_reloc_count)
  587. max_reloc_count = sec->reloc_count;
  588. }
  589. else if (bfd_link_relocatable (info)
  590. && (p->type == bfd_section_reloc_link_order
  591. || p->type == bfd_symbol_reloc_link_order))
  592. ++o->reloc_count;
  593. }
  594. if (o->reloc_count == 0)
  595. o->rel_filepos = 0;
  596. else
  597. {
  598. o->flags |= SEC_RELOC;
  599. o->rel_filepos = rel_filepos;
  600. rel_filepos += o->reloc_count * relsz;
  601. /* In PE COFF, if there are at least 0xffff relocations an
  602. extra relocation will be written out to encode the count. */
  603. if ((obj_pe (abfd) || obj_go32 (abfd)) && o->reloc_count >= 0xffff)
  604. rel_filepos += relsz;
  605. }
  606. if (bfd_coff_long_section_names (abfd)
  607. && strlen (o->name) > SCNNMLEN)
  608. {
  609. /* This section has a long name which must go in the string
  610. table. This must correspond to the code in
  611. coff_write_object_contents which puts the string index
  612. into the s_name field of the section header. That is why
  613. we pass hash as FALSE. */
  614. if (_bfd_stringtab_add (flaginfo.strtab, o->name, false, false)
  615. == (bfd_size_type) -1)
  616. goto error_return;
  617. long_section_names = true;
  618. }
  619. }
  620. /* If doing a relocatable link, allocate space for the pointers we
  621. need to keep. */
  622. if (bfd_link_relocatable (info))
  623. {
  624. unsigned int i;
  625. /* We use section_count + 1, rather than section_count, because
  626. the target_index fields are 1 based. */
  627. amt = abfd->section_count + 1;
  628. amt *= sizeof (struct coff_link_section_info);
  629. flaginfo.section_info = (struct coff_link_section_info *) bfd_malloc (amt);
  630. if (flaginfo.section_info == NULL)
  631. goto error_return;
  632. for (i = 0; i <= abfd->section_count; i++)
  633. {
  634. flaginfo.section_info[i].relocs = NULL;
  635. flaginfo.section_info[i].rel_hashes = NULL;
  636. }
  637. }
  638. /* We now know the size of the relocs, so we can determine the file
  639. positions of the line numbers. */
  640. line_filepos = rel_filepos;
  641. linesz = bfd_coff_linesz (abfd);
  642. max_output_reloc_count = 0;
  643. for (o = abfd->sections; o != NULL; o = o->next)
  644. {
  645. if (o->lineno_count == 0)
  646. o->line_filepos = 0;
  647. else
  648. {
  649. o->line_filepos = line_filepos;
  650. line_filepos += o->lineno_count * linesz;
  651. }
  652. if (o->reloc_count != 0)
  653. {
  654. /* We don't know the indices of global symbols until we have
  655. written out all the local symbols. For each section in
  656. the output file, we keep an array of pointers to hash
  657. table entries. Each entry in the array corresponds to a
  658. reloc. When we find a reloc against a global symbol, we
  659. set the corresponding entry in this array so that we can
  660. fix up the symbol index after we have written out all the
  661. local symbols.
  662. Because of this problem, we also keep the relocs in
  663. memory until the end of the link. This wastes memory,
  664. but only when doing a relocatable link, which is not the
  665. common case. */
  666. BFD_ASSERT (bfd_link_relocatable (info));
  667. amt = o->reloc_count;
  668. amt *= sizeof (struct internal_reloc);
  669. flaginfo.section_info[o->target_index].relocs =
  670. (struct internal_reloc *) bfd_malloc (amt);
  671. amt = o->reloc_count;
  672. amt *= sizeof (struct coff_link_hash_entry *);
  673. flaginfo.section_info[o->target_index].rel_hashes =
  674. (struct coff_link_hash_entry **) bfd_malloc (amt);
  675. if (flaginfo.section_info[o->target_index].relocs == NULL
  676. || flaginfo.section_info[o->target_index].rel_hashes == NULL)
  677. goto error_return;
  678. if (o->reloc_count > max_output_reloc_count)
  679. max_output_reloc_count = o->reloc_count;
  680. }
  681. /* Reset the reloc and lineno counts, so that we can use them to
  682. count the number of entries we have output so far. */
  683. o->reloc_count = 0;
  684. o->lineno_count = 0;
  685. }
  686. obj_sym_filepos (abfd) = line_filepos;
  687. /* Figure out the largest number of symbols in an input BFD. Take
  688. the opportunity to clear the output_has_begun fields of all the
  689. input BFD's. */
  690. max_sym_count = 0;
  691. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  692. {
  693. size_t sz;
  694. sub->output_has_begun = false;
  695. sz = bfd_family_coff (sub) ? obj_raw_syment_count (sub) : 2;
  696. if (sz > max_sym_count)
  697. max_sym_count = sz;
  698. }
  699. /* Allocate some buffers used while linking. */
  700. amt = max_sym_count * sizeof (struct internal_syment);
  701. flaginfo.internal_syms = (struct internal_syment *) bfd_malloc (amt);
  702. amt = max_sym_count * sizeof (asection *);
  703. flaginfo.sec_ptrs = (asection **) bfd_malloc (amt);
  704. amt = max_sym_count * sizeof (long);
  705. flaginfo.sym_indices = (long int *) bfd_malloc (amt);
  706. flaginfo.outsyms = (bfd_byte *) bfd_malloc ((max_sym_count + 1) * symesz);
  707. amt = max_lineno_count * bfd_coff_linesz (abfd);
  708. flaginfo.linenos = (bfd_byte *) bfd_malloc (amt);
  709. flaginfo.contents = (bfd_byte *) bfd_malloc (max_contents_size);
  710. amt = max_reloc_count * relsz;
  711. flaginfo.external_relocs = (bfd_byte *) bfd_malloc (amt);
  712. if (! bfd_link_relocatable (info))
  713. {
  714. amt = max_reloc_count * sizeof (struct internal_reloc);
  715. flaginfo.internal_relocs = (struct internal_reloc *) bfd_malloc (amt);
  716. }
  717. if ((flaginfo.internal_syms == NULL && max_sym_count > 0)
  718. || (flaginfo.sec_ptrs == NULL && max_sym_count > 0)
  719. || (flaginfo.sym_indices == NULL && max_sym_count > 0)
  720. || flaginfo.outsyms == NULL
  721. || (flaginfo.linenos == NULL && max_lineno_count > 0)
  722. || (flaginfo.contents == NULL && max_contents_size > 0)
  723. || (flaginfo.external_relocs == NULL && max_reloc_count > 0)
  724. || (! bfd_link_relocatable (info)
  725. && flaginfo.internal_relocs == NULL
  726. && max_reloc_count > 0))
  727. goto error_return;
  728. /* We now know the position of everything in the file, except that
  729. we don't know the size of the symbol table and therefore we don't
  730. know where the string table starts. We just build the string
  731. table in memory as we go along. We process all the relocations
  732. for a single input file at once. */
  733. obj_raw_syment_count (abfd) = 0;
  734. if (coff_backend_info (abfd)->_bfd_coff_start_final_link)
  735. {
  736. if (! bfd_coff_start_final_link (abfd, info))
  737. goto error_return;
  738. }
  739. for (o = abfd->sections; o != NULL; o = o->next)
  740. {
  741. for (p = o->map_head.link_order; p != NULL; p = p->next)
  742. {
  743. if (p->type == bfd_indirect_link_order
  744. && bfd_family_coff (p->u.indirect.section->owner))
  745. {
  746. sub = p->u.indirect.section->owner;
  747. if (! bfd_coff_link_output_has_begun (sub, & flaginfo))
  748. {
  749. if (! _bfd_coff_link_input_bfd (&flaginfo, sub))
  750. goto error_return;
  751. sub->output_has_begun = true;
  752. }
  753. }
  754. else if (p->type == bfd_section_reloc_link_order
  755. || p->type == bfd_symbol_reloc_link_order)
  756. {
  757. if (! _bfd_coff_reloc_link_order (abfd, &flaginfo, o, p))
  758. goto error_return;
  759. }
  760. else
  761. {
  762. if (! _bfd_default_link_order (abfd, info, o, p))
  763. goto error_return;
  764. }
  765. }
  766. }
  767. if (flaginfo.info->strip != strip_all && flaginfo.info->discard != discard_all)
  768. {
  769. /* Add local symbols from foreign inputs. */
  770. for (sub = info->input_bfds; sub != NULL; sub = sub->link.next)
  771. {
  772. unsigned int i;
  773. if (bfd_family_coff (sub) || ! bfd_get_outsymbols (sub))
  774. continue;
  775. for (i = 0; i < bfd_get_symcount (sub); ++i)
  776. {
  777. asymbol *sym = bfd_get_outsymbols (sub) [i];
  778. file_ptr pos;
  779. struct internal_syment isym;
  780. union internal_auxent iaux;
  781. bfd_size_type string_size = 0, indx;
  782. bfd_vma written = 0;
  783. bool rewrite = false, hash;
  784. if (! (sym->flags & BSF_LOCAL)
  785. || (sym->flags & (BSF_SECTION_SYM | BSF_DEBUGGING_RELOC
  786. | BSF_THREAD_LOCAL | BSF_RELC | BSF_SRELC
  787. | BSF_SYNTHETIC))
  788. || ((sym->flags & BSF_DEBUGGING)
  789. && ! (sym->flags & BSF_FILE)))
  790. continue;
  791. /* See if we are discarding symbols with this name. */
  792. if ((flaginfo.info->strip == strip_some
  793. && (bfd_hash_lookup (flaginfo.info->keep_hash,
  794. bfd_asymbol_name(sym), false, false)
  795. == NULL))
  796. || (((flaginfo.info->discard == discard_sec_merge
  797. && (bfd_asymbol_section (sym)->flags & SEC_MERGE)
  798. && ! bfd_link_relocatable (flaginfo.info))
  799. || flaginfo.info->discard == discard_l)
  800. && bfd_is_local_label_name (sub, bfd_asymbol_name(sym))))
  801. continue;
  802. pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd)
  803. * symesz;
  804. if (bfd_seek (abfd, pos, SEEK_SET) != 0)
  805. goto error_return;
  806. if (! coff_write_alien_symbol(abfd, sym, &isym, &iaux, &written,
  807. &string_size, NULL, NULL))
  808. goto error_return;
  809. hash = !flaginfo.info->traditional_format;
  810. if (string_size >= 6 && isym.n_sclass == C_FILE
  811. && ! isym._n._n_n._n_zeroes && isym.n_numaux)
  812. {
  813. indx = _bfd_stringtab_add (flaginfo.strtab, ".file", hash,
  814. false);
  815. if (indx == (bfd_size_type) -1)
  816. goto error_return;
  817. isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
  818. bfd_coff_swap_sym_out (abfd, &isym, flaginfo.outsyms);
  819. if (bfd_seek (abfd, pos, SEEK_SET) != 0
  820. || bfd_bwrite (flaginfo.outsyms, symesz,
  821. abfd) != symesz)
  822. goto error_return;
  823. string_size -= 6;
  824. }
  825. if (string_size)
  826. {
  827. indx = _bfd_stringtab_add (flaginfo.strtab,
  828. bfd_asymbol_name (sym), hash,
  829. false);
  830. if (indx == (bfd_size_type) -1)
  831. goto error_return;
  832. if (isym.n_sclass != C_FILE)
  833. {
  834. isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
  835. bfd_coff_swap_sym_out (abfd, &isym, flaginfo.outsyms);
  836. rewrite = true;
  837. }
  838. else
  839. {
  840. BFD_ASSERT (isym.n_numaux == 1);
  841. iaux.x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
  842. bfd_coff_swap_aux_out (abfd, &iaux, isym.n_type, C_FILE,
  843. 0, 1, flaginfo.outsyms + symesz);
  844. if (bfd_seek (abfd, pos + symesz, SEEK_SET) != 0
  845. || bfd_bwrite (flaginfo.outsyms + symesz, symesz,
  846. abfd) != symesz)
  847. goto error_return;
  848. }
  849. }
  850. if (isym.n_sclass == C_FILE)
  851. {
  852. if (flaginfo.last_file_index != -1)
  853. {
  854. flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
  855. bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
  856. flaginfo.outsyms);
  857. pos = obj_sym_filepos (abfd) + flaginfo.last_file_index
  858. * symesz;
  859. rewrite = true;
  860. }
  861. flaginfo.last_file_index = obj_raw_syment_count (abfd);
  862. flaginfo.last_file = isym;
  863. }
  864. if (rewrite
  865. && (bfd_seek (abfd, pos, SEEK_SET) != 0
  866. || bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz))
  867. goto error_return;
  868. obj_raw_syment_count (abfd) += written;
  869. }
  870. }
  871. }
  872. if (! bfd_coff_final_link_postscript (abfd, & flaginfo))
  873. goto error_return;
  874. /* Free up the buffers used by _bfd_coff_link_input_bfd. */
  875. coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
  876. debug_merge_allocated = false;
  877. free (flaginfo.internal_syms);
  878. flaginfo.internal_syms = NULL;
  879. free (flaginfo.sec_ptrs);
  880. flaginfo.sec_ptrs = NULL;
  881. free (flaginfo.sym_indices);
  882. flaginfo.sym_indices = NULL;
  883. free (flaginfo.linenos);
  884. flaginfo.linenos = NULL;
  885. free (flaginfo.contents);
  886. flaginfo.contents = NULL;
  887. free (flaginfo.external_relocs);
  888. flaginfo.external_relocs = NULL;
  889. free (flaginfo.internal_relocs);
  890. flaginfo.internal_relocs = NULL;
  891. /* The value of the last C_FILE symbol is supposed to be the symbol
  892. index of the first external symbol. Write it out again if
  893. necessary. */
  894. if (flaginfo.last_file_index != -1
  895. && (unsigned int) flaginfo.last_file.n_value != obj_raw_syment_count (abfd))
  896. {
  897. file_ptr pos;
  898. flaginfo.last_file.n_value = obj_raw_syment_count (abfd);
  899. bfd_coff_swap_sym_out (abfd, &flaginfo.last_file,
  900. flaginfo.outsyms);
  901. pos = obj_sym_filepos (abfd) + flaginfo.last_file_index * symesz;
  902. if (bfd_seek (abfd, pos, SEEK_SET) != 0
  903. || bfd_bwrite (flaginfo.outsyms, symesz, abfd) != symesz)
  904. return false;
  905. }
  906. /* If doing task linking (ld --task-link) then make a pass through the
  907. global symbols, writing out any that are defined, and making them
  908. static. */
  909. if (info->task_link)
  910. {
  911. flaginfo.failed = false;
  912. coff_link_hash_traverse (coff_hash_table (info),
  913. _bfd_coff_write_task_globals, &flaginfo);
  914. if (flaginfo.failed)
  915. goto error_return;
  916. }
  917. /* Write out the global symbols. */
  918. flaginfo.failed = false;
  919. bfd_hash_traverse (&info->hash->table, _bfd_coff_write_global_sym, &flaginfo);
  920. if (flaginfo.failed)
  921. goto error_return;
  922. /* The outsyms buffer is used by _bfd_coff_write_global_sym. */
  923. free (flaginfo.outsyms);
  924. flaginfo.outsyms = NULL;
  925. if (bfd_link_relocatable (info) && max_output_reloc_count > 0)
  926. {
  927. /* Now that we have written out all the global symbols, we know
  928. the symbol indices to use for relocs against them, and we can
  929. finally write out the relocs. */
  930. amt = max_output_reloc_count * relsz;
  931. external_relocs = (bfd_byte *) bfd_malloc (amt);
  932. if (external_relocs == NULL)
  933. goto error_return;
  934. for (o = abfd->sections; o != NULL; o = o->next)
  935. {
  936. struct internal_reloc *irel;
  937. struct internal_reloc *irelend;
  938. struct coff_link_hash_entry **rel_hash;
  939. bfd_byte *erel;
  940. if (o->reloc_count == 0)
  941. continue;
  942. irel = flaginfo.section_info[o->target_index].relocs;
  943. irelend = irel + o->reloc_count;
  944. rel_hash = flaginfo.section_info[o->target_index].rel_hashes;
  945. erel = external_relocs;
  946. for (; irel < irelend; irel++, rel_hash++, erel += relsz)
  947. {
  948. if (*rel_hash != NULL)
  949. {
  950. BFD_ASSERT ((*rel_hash)->indx >= 0);
  951. irel->r_symndx = (*rel_hash)->indx;
  952. }
  953. bfd_coff_swap_reloc_out (abfd, irel, erel);
  954. }
  955. if (bfd_seek (abfd, o->rel_filepos, SEEK_SET) != 0)
  956. goto error_return;
  957. if ((obj_pe (abfd) || obj_go32 (abfd)) && o->reloc_count >= 0xffff)
  958. {
  959. /* In PE COFF, write the count of relocs as the first
  960. reloc. The header overflow bit will be set
  961. elsewhere. */
  962. struct internal_reloc incount;
  963. bfd_byte *excount = (bfd_byte *)bfd_malloc (relsz);
  964. memset (&incount, 0, sizeof (incount));
  965. incount.r_vaddr = o->reloc_count + 1;
  966. bfd_coff_swap_reloc_out (abfd, &incount, excount);
  967. if (bfd_bwrite (excount, relsz, abfd) != relsz)
  968. /* We'll leak, but it's an error anyway. */
  969. goto error_return;
  970. free (excount);
  971. }
  972. if (bfd_bwrite (external_relocs,
  973. (bfd_size_type) relsz * o->reloc_count, abfd)
  974. != (bfd_size_type) relsz * o->reloc_count)
  975. goto error_return;
  976. }
  977. free (external_relocs);
  978. external_relocs = NULL;
  979. }
  980. /* Free up the section information. */
  981. if (flaginfo.section_info != NULL)
  982. {
  983. unsigned int i;
  984. for (i = 0; i < abfd->section_count; i++)
  985. {
  986. free (flaginfo.section_info[i].relocs);
  987. free (flaginfo.section_info[i].rel_hashes);
  988. }
  989. free (flaginfo.section_info);
  990. flaginfo.section_info = NULL;
  991. }
  992. /* If we have optimized stabs strings, output them. */
  993. if (coff_hash_table (info)->stab_info.stabstr != NULL)
  994. {
  995. if (! _bfd_write_stab_strings (abfd, &coff_hash_table (info)->stab_info))
  996. return false;
  997. }
  998. /* Write out the string table. */
  999. if (obj_raw_syment_count (abfd) != 0 || long_section_names)
  1000. {
  1001. file_ptr pos;
  1002. pos = obj_sym_filepos (abfd) + obj_raw_syment_count (abfd) * symesz;
  1003. if (bfd_seek (abfd, pos, SEEK_SET) != 0)
  1004. return false;
  1005. #if STRING_SIZE_SIZE == 4
  1006. H_PUT_32 (abfd,
  1007. _bfd_stringtab_size (flaginfo.strtab) + STRING_SIZE_SIZE,
  1008. strbuf);
  1009. #else
  1010. #error Change H_PUT_32 above
  1011. #endif
  1012. if (bfd_bwrite (strbuf, (bfd_size_type) STRING_SIZE_SIZE, abfd)
  1013. != STRING_SIZE_SIZE)
  1014. return false;
  1015. if (! _bfd_stringtab_emit (abfd, flaginfo.strtab))
  1016. return false;
  1017. obj_coff_strings_written (abfd) = true;
  1018. }
  1019. _bfd_stringtab_free (flaginfo.strtab);
  1020. /* Setting symcount to 0 will cause write_object_contents to
  1021. not try to write out the symbols. */
  1022. abfd->symcount = 0;
  1023. return true;
  1024. error_return:
  1025. if (debug_merge_allocated)
  1026. coff_debug_merge_hash_table_free (&flaginfo.debug_merge);
  1027. if (flaginfo.strtab != NULL)
  1028. _bfd_stringtab_free (flaginfo.strtab);
  1029. if (flaginfo.section_info != NULL)
  1030. {
  1031. unsigned int i;
  1032. for (i = 0; i < abfd->section_count; i++)
  1033. {
  1034. free (flaginfo.section_info[i].relocs);
  1035. free (flaginfo.section_info[i].rel_hashes);
  1036. }
  1037. free (flaginfo.section_info);
  1038. }
  1039. free (flaginfo.internal_syms);
  1040. free (flaginfo.sec_ptrs);
  1041. free (flaginfo.sym_indices);
  1042. free (flaginfo.outsyms);
  1043. free (flaginfo.linenos);
  1044. free (flaginfo.contents);
  1045. free (flaginfo.external_relocs);
  1046. free (flaginfo.internal_relocs);
  1047. free (external_relocs);
  1048. return false;
  1049. }
  1050. /* Parse out a -heap <reserved>,<commit> line. */
  1051. static char *
  1052. dores_com (char *ptr, bfd *output_bfd, int heap)
  1053. {
  1054. if (coff_data(output_bfd)->pe)
  1055. {
  1056. int val = strtoul (ptr, &ptr, 0);
  1057. if (heap)
  1058. pe_data(output_bfd)->pe_opthdr.SizeOfHeapReserve = val;
  1059. else
  1060. pe_data(output_bfd)->pe_opthdr.SizeOfStackReserve = val;
  1061. if (ptr[0] == ',')
  1062. {
  1063. val = strtoul (ptr+1, &ptr, 0);
  1064. if (heap)
  1065. pe_data(output_bfd)->pe_opthdr.SizeOfHeapCommit = val;
  1066. else
  1067. pe_data(output_bfd)->pe_opthdr.SizeOfStackCommit = val;
  1068. }
  1069. }
  1070. return ptr;
  1071. }
  1072. static char *
  1073. get_name (char *ptr, char **dst)
  1074. {
  1075. while (*ptr == ' ')
  1076. ptr++;
  1077. *dst = ptr;
  1078. while (*ptr && *ptr != ' ')
  1079. ptr++;
  1080. *ptr = 0;
  1081. return ptr+1;
  1082. }
  1083. /* Process any magic embedded commands in a section called .drectve. */
  1084. static int
  1085. process_embedded_commands (bfd *output_bfd,
  1086. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  1087. bfd *abfd)
  1088. {
  1089. asection *sec = bfd_get_section_by_name (abfd, ".drectve");
  1090. char *s;
  1091. char *e;
  1092. bfd_byte *copy;
  1093. if (!sec)
  1094. return 1;
  1095. if (!bfd_malloc_and_get_section (abfd, sec, &copy))
  1096. {
  1097. free (copy);
  1098. return 0;
  1099. }
  1100. e = (char *) copy + sec->size;
  1101. for (s = (char *) copy; s < e ; )
  1102. {
  1103. if (s[0] != '-')
  1104. {
  1105. s++;
  1106. continue;
  1107. }
  1108. if (startswith (s, "-attr"))
  1109. {
  1110. char *name;
  1111. char *attribs;
  1112. asection *asec;
  1113. int loop = 1;
  1114. int had_write = 0;
  1115. int had_exec= 0;
  1116. s += 5;
  1117. s = get_name (s, &name);
  1118. s = get_name (s, &attribs);
  1119. while (loop)
  1120. {
  1121. switch (*attribs++)
  1122. {
  1123. case 'W':
  1124. had_write = 1;
  1125. break;
  1126. case 'R':
  1127. break;
  1128. case 'S':
  1129. break;
  1130. case 'X':
  1131. had_exec = 1;
  1132. break;
  1133. default:
  1134. loop = 0;
  1135. }
  1136. }
  1137. asec = bfd_get_section_by_name (abfd, name);
  1138. if (asec)
  1139. {
  1140. if (had_exec)
  1141. asec->flags |= SEC_CODE;
  1142. if (!had_write)
  1143. asec->flags |= SEC_READONLY;
  1144. }
  1145. }
  1146. else if (startswith (s, "-heap"))
  1147. s = dores_com (s + 5, output_bfd, 1);
  1148. else if (startswith (s, "-stack"))
  1149. s = dores_com (s + 6, output_bfd, 0);
  1150. /* GNU extension for aligned commons. */
  1151. else if (startswith (s, "-aligncomm:"))
  1152. {
  1153. /* Common symbols must be aligned on reading, as it
  1154. is too late to do anything here, after they have
  1155. already been allocated, so just skip the directive. */
  1156. s += 11;
  1157. }
  1158. else
  1159. s++;
  1160. }
  1161. free (copy);
  1162. return 1;
  1163. }
  1164. /* Place a marker against all symbols which are used by relocations.
  1165. This marker can be picked up by the 'do we skip this symbol ?'
  1166. loop in _bfd_coff_link_input_bfd() and used to prevent skipping
  1167. that symbol. */
  1168. static void
  1169. mark_relocs (struct coff_final_link_info *flaginfo, bfd *input_bfd)
  1170. {
  1171. asection * a;
  1172. if ((bfd_get_file_flags (input_bfd) & HAS_SYMS) == 0)
  1173. return;
  1174. for (a = input_bfd->sections; a != (asection *) NULL; a = a->next)
  1175. {
  1176. struct internal_reloc * internal_relocs;
  1177. struct internal_reloc * irel;
  1178. struct internal_reloc * irelend;
  1179. if ((a->flags & SEC_RELOC) == 0 || a->reloc_count < 1
  1180. || a->linker_mark == 0)
  1181. continue;
  1182. /* Don't mark relocs in excluded sections. */
  1183. if (a->output_section == bfd_abs_section_ptr)
  1184. continue;
  1185. /* Read in the relocs. */
  1186. internal_relocs = _bfd_coff_read_internal_relocs
  1187. (input_bfd, a, false,
  1188. flaginfo->external_relocs,
  1189. bfd_link_relocatable (flaginfo->info),
  1190. (bfd_link_relocatable (flaginfo->info)
  1191. ? (flaginfo->section_info[ a->output_section->target_index ].relocs + a->output_section->reloc_count)
  1192. : flaginfo->internal_relocs)
  1193. );
  1194. if (internal_relocs == NULL)
  1195. continue;
  1196. irel = internal_relocs;
  1197. irelend = irel + a->reloc_count;
  1198. /* Place a mark in the sym_indices array (whose entries have
  1199. been initialised to 0) for all of the symbols that are used
  1200. in the relocation table. This will then be picked up in the
  1201. skip/don't-skip pass. */
  1202. for (; irel < irelend; irel++)
  1203. if ((unsigned long) irel->r_symndx < obj_raw_syment_count (input_bfd))
  1204. flaginfo->sym_indices[irel->r_symndx] = -1;
  1205. }
  1206. }
  1207. /* Link an input file into the linker output file. This function
  1208. handles all the sections and relocations of the input file at once. */
  1209. bool
  1210. _bfd_coff_link_input_bfd (struct coff_final_link_info *flaginfo, bfd *input_bfd)
  1211. {
  1212. unsigned int n_tmask = coff_data (input_bfd)->local_n_tmask;
  1213. unsigned int n_btshft = coff_data (input_bfd)->local_n_btshft;
  1214. bool (*adjust_symndx)
  1215. (bfd *, struct bfd_link_info *, bfd *, asection *,
  1216. struct internal_reloc *, bool *);
  1217. bfd *output_bfd;
  1218. const char *strings;
  1219. bfd_size_type syment_base;
  1220. bool copy, hash;
  1221. bfd_size_type isymesz;
  1222. bfd_size_type osymesz;
  1223. bfd_size_type linesz;
  1224. bfd_byte *esym;
  1225. bfd_byte *esym_end;
  1226. struct internal_syment *isymp;
  1227. asection **secpp;
  1228. long *indexp;
  1229. unsigned long output_index;
  1230. bfd_byte *outsym;
  1231. struct coff_link_hash_entry **sym_hash;
  1232. asection *o;
  1233. /* Move all the symbols to the output file. */
  1234. output_bfd = flaginfo->output_bfd;
  1235. strings = NULL;
  1236. syment_base = obj_raw_syment_count (output_bfd);
  1237. isymesz = bfd_coff_symesz (input_bfd);
  1238. osymesz = bfd_coff_symesz (output_bfd);
  1239. linesz = bfd_coff_linesz (input_bfd);
  1240. BFD_ASSERT (linesz == bfd_coff_linesz (output_bfd));
  1241. copy = false;
  1242. if (! flaginfo->info->keep_memory)
  1243. copy = true;
  1244. hash = true;
  1245. if (flaginfo->info->traditional_format)
  1246. hash = false;
  1247. if (! _bfd_coff_get_external_symbols (input_bfd))
  1248. return false;
  1249. esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
  1250. esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
  1251. isymp = flaginfo->internal_syms;
  1252. secpp = flaginfo->sec_ptrs;
  1253. indexp = flaginfo->sym_indices;
  1254. output_index = syment_base;
  1255. outsym = flaginfo->outsyms;
  1256. if (coff_data (output_bfd)->pe
  1257. && ! process_embedded_commands (output_bfd, flaginfo->info, input_bfd))
  1258. return false;
  1259. /* If we are going to perform relocations and also strip/discard some
  1260. symbols then we must make sure that we do not strip/discard those
  1261. symbols that are going to be involved in the relocations. */
  1262. if (( flaginfo->info->strip != strip_none
  1263. || flaginfo->info->discard != discard_none)
  1264. && bfd_link_relocatable (flaginfo->info))
  1265. {
  1266. /* Mark the symbol array as 'not-used'. */
  1267. memset (indexp, 0, obj_raw_syment_count (input_bfd) * sizeof * indexp);
  1268. mark_relocs (flaginfo, input_bfd);
  1269. }
  1270. while (esym < esym_end)
  1271. {
  1272. struct internal_syment isym;
  1273. enum coff_symbol_classification classification;
  1274. bool skip;
  1275. bool global;
  1276. bool dont_skip_symbol;
  1277. int add;
  1278. bfd_coff_swap_sym_in (input_bfd, esym, isymp);
  1279. /* Make a copy of *isymp so that the relocate_section function
  1280. always sees the original values. This is more reliable than
  1281. always recomputing the symbol value even if we are stripping
  1282. the symbol. */
  1283. isym = *isymp;
  1284. classification = bfd_coff_classify_symbol (input_bfd, &isym);
  1285. switch (classification)
  1286. {
  1287. default:
  1288. abort ();
  1289. case COFF_SYMBOL_GLOBAL:
  1290. case COFF_SYMBOL_PE_SECTION:
  1291. case COFF_SYMBOL_LOCAL:
  1292. *secpp = coff_section_from_bfd_index (input_bfd, isym.n_scnum);
  1293. break;
  1294. case COFF_SYMBOL_COMMON:
  1295. *secpp = bfd_com_section_ptr;
  1296. break;
  1297. case COFF_SYMBOL_UNDEFINED:
  1298. *secpp = bfd_und_section_ptr;
  1299. break;
  1300. }
  1301. /* Extract the flag indicating if this symbol is used by a
  1302. relocation. */
  1303. if ((flaginfo->info->strip != strip_none
  1304. || flaginfo->info->discard != discard_none)
  1305. && bfd_link_relocatable (flaginfo->info))
  1306. dont_skip_symbol = *indexp;
  1307. else
  1308. dont_skip_symbol = false;
  1309. *indexp = -1;
  1310. skip = false;
  1311. global = false;
  1312. add = 1 + isym.n_numaux;
  1313. /* If we are stripping all symbols, we want to skip this one. */
  1314. if (flaginfo->info->strip == strip_all && ! dont_skip_symbol)
  1315. skip = true;
  1316. if (! skip)
  1317. {
  1318. switch (classification)
  1319. {
  1320. default:
  1321. abort ();
  1322. case COFF_SYMBOL_GLOBAL:
  1323. case COFF_SYMBOL_COMMON:
  1324. case COFF_SYMBOL_PE_SECTION:
  1325. /* This is a global symbol. Global symbols come at the
  1326. end of the symbol table, so skip them for now.
  1327. Locally defined function symbols, however, are an
  1328. exception, and are not moved to the end. */
  1329. global = true;
  1330. if (! ISFCN (isym.n_type))
  1331. skip = true;
  1332. break;
  1333. case COFF_SYMBOL_UNDEFINED:
  1334. /* Undefined symbols are left for the end. */
  1335. global = true;
  1336. skip = true;
  1337. break;
  1338. case COFF_SYMBOL_LOCAL:
  1339. /* This is a local symbol. Skip it if we are discarding
  1340. local symbols. */
  1341. if (flaginfo->info->discard == discard_all && ! dont_skip_symbol)
  1342. skip = true;
  1343. break;
  1344. }
  1345. }
  1346. #ifndef COFF_WITH_PE
  1347. /* Skip section symbols for sections which are not going to be
  1348. emitted. */
  1349. if (!skip
  1350. && !dont_skip_symbol
  1351. && isym.n_sclass == C_STAT
  1352. && isym.n_type == T_NULL
  1353. && isym.n_numaux > 0
  1354. && ((*secpp)->output_section == bfd_abs_section_ptr
  1355. || bfd_section_removed_from_list (output_bfd,
  1356. (*secpp)->output_section)))
  1357. skip = true;
  1358. #endif
  1359. /* If we stripping debugging symbols, and this is a debugging
  1360. symbol, then skip it. FIXME: gas sets the section to N_ABS
  1361. for some types of debugging symbols; I don't know if this is
  1362. a bug or not. In any case, we handle it here. */
  1363. if (! skip
  1364. && flaginfo->info->strip == strip_debugger
  1365. && ! dont_skip_symbol
  1366. && (isym.n_scnum == N_DEBUG
  1367. || (isym.n_scnum == N_ABS
  1368. && (isym.n_sclass == C_AUTO
  1369. || isym.n_sclass == C_REG
  1370. || isym.n_sclass == C_MOS
  1371. || isym.n_sclass == C_MOE
  1372. || isym.n_sclass == C_MOU
  1373. || isym.n_sclass == C_ARG
  1374. || isym.n_sclass == C_REGPARM
  1375. || isym.n_sclass == C_FIELD
  1376. || isym.n_sclass == C_EOS))))
  1377. skip = true;
  1378. /* If some symbols are stripped based on the name, work out the
  1379. name and decide whether to skip this symbol. */
  1380. if (! skip
  1381. && (flaginfo->info->strip == strip_some
  1382. || flaginfo->info->discard == discard_l))
  1383. {
  1384. const char *name;
  1385. char buf[SYMNMLEN + 1];
  1386. name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
  1387. if (name == NULL)
  1388. return false;
  1389. if (! dont_skip_symbol
  1390. && ((flaginfo->info->strip == strip_some
  1391. && (bfd_hash_lookup (flaginfo->info->keep_hash, name, false,
  1392. false) == NULL))
  1393. || (! global
  1394. && flaginfo->info->discard == discard_l
  1395. && bfd_is_local_label_name (input_bfd, name))))
  1396. skip = true;
  1397. }
  1398. /* If this is an enum, struct, or union tag, see if we have
  1399. already output an identical type. */
  1400. if (! skip
  1401. && !flaginfo->info->traditional_format
  1402. && (isym.n_sclass == C_ENTAG
  1403. || isym.n_sclass == C_STRTAG
  1404. || isym.n_sclass == C_UNTAG)
  1405. && isym.n_numaux == 1)
  1406. {
  1407. const char *name;
  1408. char buf[SYMNMLEN + 1];
  1409. struct coff_debug_merge_hash_entry *mh;
  1410. struct coff_debug_merge_type *mt;
  1411. union internal_auxent aux;
  1412. struct coff_debug_merge_element **epp;
  1413. bfd_byte *esl, *eslend;
  1414. struct internal_syment *islp;
  1415. size_t amt;
  1416. name = _bfd_coff_internal_syment_name (input_bfd, &isym, buf);
  1417. if (name == NULL)
  1418. return false;
  1419. /* Ignore fake names invented by compiler; treat them all as
  1420. the same name. */
  1421. if (*name == '~' || *name == '.' || *name == '$'
  1422. || (*name == bfd_get_symbol_leading_char (input_bfd)
  1423. && (name[1] == '~' || name[1] == '.' || name[1] == '$')))
  1424. name = "";
  1425. mh = coff_debug_merge_hash_lookup (&flaginfo->debug_merge, name,
  1426. true, true);
  1427. if (mh == NULL)
  1428. return false;
  1429. /* Allocate memory to hold type information. If this turns
  1430. out to be a duplicate, we pass this address to
  1431. bfd_release. */
  1432. amt = sizeof (struct coff_debug_merge_type);
  1433. mt = (struct coff_debug_merge_type *) bfd_alloc (input_bfd, amt);
  1434. if (mt == NULL)
  1435. return false;
  1436. mt->type_class = isym.n_sclass;
  1437. /* Pick up the aux entry, which points to the end of the tag
  1438. entries. */
  1439. bfd_coff_swap_aux_in (input_bfd, (esym + isymesz),
  1440. isym.n_type, isym.n_sclass, 0, isym.n_numaux,
  1441. &aux);
  1442. /* Gather the elements. */
  1443. epp = &mt->elements;
  1444. mt->elements = NULL;
  1445. islp = isymp + 2;
  1446. esl = esym + 2 * isymesz;
  1447. eslend = ((bfd_byte *) obj_coff_external_syms (input_bfd)
  1448. + aux.x_sym.x_fcnary.x_fcn.x_endndx.l * isymesz);
  1449. while (esl < eslend)
  1450. {
  1451. const char *elename;
  1452. char elebuf[SYMNMLEN + 1];
  1453. char *name_copy;
  1454. bfd_coff_swap_sym_in (input_bfd, esl, islp);
  1455. amt = sizeof (struct coff_debug_merge_element);
  1456. *epp = (struct coff_debug_merge_element *)
  1457. bfd_alloc (input_bfd, amt);
  1458. if (*epp == NULL)
  1459. return false;
  1460. elename = _bfd_coff_internal_syment_name (input_bfd, islp,
  1461. elebuf);
  1462. if (elename == NULL)
  1463. return false;
  1464. amt = strlen (elename) + 1;
  1465. name_copy = (char *) bfd_alloc (input_bfd, amt);
  1466. if (name_copy == NULL)
  1467. return false;
  1468. strcpy (name_copy, elename);
  1469. (*epp)->name = name_copy;
  1470. (*epp)->type = islp->n_type;
  1471. (*epp)->tagndx = 0;
  1472. if (islp->n_numaux >= 1
  1473. && islp->n_type != T_NULL
  1474. && islp->n_sclass != C_EOS)
  1475. {
  1476. union internal_auxent eleaux;
  1477. long indx;
  1478. bfd_coff_swap_aux_in (input_bfd, (esl + isymesz),
  1479. islp->n_type, islp->n_sclass, 0,
  1480. islp->n_numaux, &eleaux);
  1481. indx = eleaux.x_sym.x_tagndx.l;
  1482. /* FIXME: If this tagndx entry refers to a symbol
  1483. defined later in this file, we just ignore it.
  1484. Handling this correctly would be tedious, and may
  1485. not be required. */
  1486. if (indx > 0
  1487. && (indx
  1488. < ((esym -
  1489. (bfd_byte *) obj_coff_external_syms (input_bfd))
  1490. / (long) isymesz)))
  1491. {
  1492. (*epp)->tagndx = flaginfo->sym_indices[indx];
  1493. if ((*epp)->tagndx < 0)
  1494. (*epp)->tagndx = 0;
  1495. }
  1496. }
  1497. epp = &(*epp)->next;
  1498. *epp = NULL;
  1499. esl += (islp->n_numaux + 1) * isymesz;
  1500. islp += islp->n_numaux + 1;
  1501. }
  1502. /* See if we already have a definition which matches this
  1503. type. We always output the type if it has no elements,
  1504. for simplicity. */
  1505. if (mt->elements == NULL)
  1506. bfd_release (input_bfd, mt);
  1507. else
  1508. {
  1509. struct coff_debug_merge_type *mtl;
  1510. for (mtl = mh->types; mtl != NULL; mtl = mtl->next)
  1511. {
  1512. struct coff_debug_merge_element *me, *mel;
  1513. if (mtl->type_class != mt->type_class)
  1514. continue;
  1515. for (me = mt->elements, mel = mtl->elements;
  1516. me != NULL && mel != NULL;
  1517. me = me->next, mel = mel->next)
  1518. {
  1519. if (strcmp (me->name, mel->name) != 0
  1520. || me->type != mel->type
  1521. || me->tagndx != mel->tagndx)
  1522. break;
  1523. }
  1524. if (me == NULL && mel == NULL)
  1525. break;
  1526. }
  1527. if (mtl == NULL || (bfd_size_type) mtl->indx >= syment_base)
  1528. {
  1529. /* This is the first definition of this type. */
  1530. mt->indx = output_index;
  1531. mt->next = mh->types;
  1532. mh->types = mt;
  1533. }
  1534. else
  1535. {
  1536. /* This is a redefinition which can be merged. */
  1537. bfd_release (input_bfd, mt);
  1538. *indexp = mtl->indx;
  1539. add = (eslend - esym) / isymesz;
  1540. skip = true;
  1541. }
  1542. }
  1543. }
  1544. /* We now know whether we are to skip this symbol or not. */
  1545. if (! skip)
  1546. {
  1547. /* Adjust the symbol in order to output it. */
  1548. if (isym._n._n_n._n_zeroes == 0
  1549. && isym._n._n_n._n_offset != 0)
  1550. {
  1551. const char *name;
  1552. bfd_size_type indx;
  1553. /* This symbol has a long name. Enter it in the string
  1554. table we are building. Note that we do not check
  1555. bfd_coff_symname_in_debug. That is only true for
  1556. XCOFF, and XCOFF requires different linking code
  1557. anyhow. */
  1558. name = _bfd_coff_internal_syment_name (input_bfd, &isym, NULL);
  1559. if (name == NULL)
  1560. return false;
  1561. indx = _bfd_stringtab_add (flaginfo->strtab, name, hash, copy);
  1562. if (indx == (bfd_size_type) -1)
  1563. return false;
  1564. isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
  1565. }
  1566. switch (isym.n_sclass)
  1567. {
  1568. case C_AUTO:
  1569. case C_MOS:
  1570. case C_EOS:
  1571. case C_MOE:
  1572. case C_MOU:
  1573. case C_UNTAG:
  1574. case C_STRTAG:
  1575. case C_ENTAG:
  1576. case C_TPDEF:
  1577. case C_ARG:
  1578. case C_USTATIC:
  1579. case C_REG:
  1580. case C_REGPARM:
  1581. case C_FIELD:
  1582. /* The symbol value should not be modified. */
  1583. break;
  1584. case C_FCN:
  1585. if (obj_pe (input_bfd)
  1586. && memcmp (isym.n_name, ".bf", sizeof ".bf") != 0
  1587. && isym.n_scnum > 0)
  1588. {
  1589. /* For PE, .lf and .ef get their value left alone,
  1590. while .bf gets relocated. However, they all have
  1591. "real" section numbers, and need to be moved into
  1592. the new section. */
  1593. isym.n_scnum = (*secpp)->output_section->target_index;
  1594. break;
  1595. }
  1596. /* Fall through. */
  1597. default:
  1598. case C_LABEL: /* Not completely sure about these 2 */
  1599. case C_EXTDEF:
  1600. case C_BLOCK:
  1601. case C_EFCN:
  1602. case C_NULL:
  1603. case C_EXT:
  1604. case C_STAT:
  1605. case C_SECTION:
  1606. case C_NT_WEAK:
  1607. /* Compute new symbol location. */
  1608. if (isym.n_scnum > 0)
  1609. {
  1610. isym.n_scnum = (*secpp)->output_section->target_index;
  1611. isym.n_value += (*secpp)->output_offset;
  1612. if (! obj_pe (input_bfd))
  1613. isym.n_value -= (*secpp)->vma;
  1614. if (! obj_pe (flaginfo->output_bfd))
  1615. isym.n_value += (*secpp)->output_section->vma;
  1616. }
  1617. break;
  1618. case C_FILE:
  1619. /* The value of a C_FILE symbol is the symbol index of
  1620. the next C_FILE symbol. The value of the last C_FILE
  1621. symbol is the symbol index to the first external
  1622. symbol (actually, coff_renumber_symbols does not get
  1623. this right--it just sets the value of the last C_FILE
  1624. symbol to zero--and nobody has ever complained about
  1625. it). We try to get this right, below, just before we
  1626. write the symbols out, but in the general case we may
  1627. have to write the symbol out twice. */
  1628. if (flaginfo->last_file_index != -1
  1629. && flaginfo->last_file.n_value != (bfd_vma) output_index)
  1630. {
  1631. /* We must correct the value of the last C_FILE
  1632. entry. */
  1633. flaginfo->last_file.n_value = output_index;
  1634. if ((bfd_size_type) flaginfo->last_file_index >= syment_base)
  1635. {
  1636. /* The last C_FILE symbol is in this input file. */
  1637. bfd_coff_swap_sym_out (output_bfd,
  1638. &flaginfo->last_file,
  1639. (flaginfo->outsyms
  1640. + ((flaginfo->last_file_index
  1641. - syment_base)
  1642. * osymesz)));
  1643. }
  1644. else
  1645. {
  1646. file_ptr pos;
  1647. /* We have already written out the last C_FILE
  1648. symbol. We need to write it out again. We
  1649. borrow *outsym temporarily. */
  1650. bfd_coff_swap_sym_out (output_bfd,
  1651. &flaginfo->last_file, outsym);
  1652. pos = obj_sym_filepos (output_bfd);
  1653. pos += flaginfo->last_file_index * osymesz;
  1654. if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
  1655. || bfd_bwrite (outsym, osymesz, output_bfd) != osymesz)
  1656. return false;
  1657. }
  1658. }
  1659. flaginfo->last_file_index = output_index;
  1660. flaginfo->last_file = isym;
  1661. break;
  1662. }
  1663. /* If doing task linking, convert normal global function symbols to
  1664. static functions. */
  1665. if (flaginfo->info->task_link && IS_EXTERNAL (input_bfd, isym))
  1666. isym.n_sclass = C_STAT;
  1667. /* Output the symbol. */
  1668. bfd_coff_swap_sym_out (output_bfd, &isym, outsym);
  1669. *indexp = output_index;
  1670. if (global)
  1671. {
  1672. long indx;
  1673. struct coff_link_hash_entry *h;
  1674. indx = ((esym - (bfd_byte *) obj_coff_external_syms (input_bfd))
  1675. / isymesz);
  1676. h = obj_coff_sym_hashes (input_bfd)[indx];
  1677. if (h == NULL)
  1678. {
  1679. /* This can happen if there were errors earlier in
  1680. the link. */
  1681. bfd_set_error (bfd_error_bad_value);
  1682. return false;
  1683. }
  1684. h->indx = output_index;
  1685. }
  1686. output_index += add;
  1687. outsym += add * osymesz;
  1688. }
  1689. esym += add * isymesz;
  1690. isymp += add;
  1691. ++secpp;
  1692. ++indexp;
  1693. for (--add; add > 0; --add)
  1694. {
  1695. *secpp++ = NULL;
  1696. *indexp++ = -1;
  1697. }
  1698. }
  1699. /* Fix up the aux entries. This must be done in a separate pass,
  1700. because we don't know the correct symbol indices until we have
  1701. already decided which symbols we are going to keep. */
  1702. esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
  1703. esym_end = esym + obj_raw_syment_count (input_bfd) * isymesz;
  1704. isymp = flaginfo->internal_syms;
  1705. indexp = flaginfo->sym_indices;
  1706. sym_hash = obj_coff_sym_hashes (input_bfd);
  1707. outsym = flaginfo->outsyms;
  1708. while (esym < esym_end)
  1709. {
  1710. int add;
  1711. add = 1 + isymp->n_numaux;
  1712. if ((*indexp < 0
  1713. || (bfd_size_type) *indexp < syment_base)
  1714. && (*sym_hash == NULL
  1715. || (*sym_hash)->auxbfd != input_bfd))
  1716. esym += add * isymesz;
  1717. else
  1718. {
  1719. struct coff_link_hash_entry *h;
  1720. int i;
  1721. h = NULL;
  1722. if (*indexp < 0)
  1723. {
  1724. h = *sym_hash;
  1725. /* The m68k-motorola-sysv assembler will sometimes
  1726. generate two symbols with the same name, but only one
  1727. will have aux entries. */
  1728. BFD_ASSERT (isymp->n_numaux == 0
  1729. || h->numaux == 0
  1730. || h->numaux == isymp->n_numaux);
  1731. }
  1732. esym += isymesz;
  1733. if (h == NULL)
  1734. outsym += osymesz;
  1735. /* Handle the aux entries. This handling is based on
  1736. coff_pointerize_aux. I don't know if it always correct. */
  1737. for (i = 0; i < isymp->n_numaux && esym < esym_end; i++)
  1738. {
  1739. union internal_auxent aux;
  1740. union internal_auxent *auxp;
  1741. if (h != NULL && h->aux != NULL && (h->numaux > i))
  1742. auxp = h->aux + i;
  1743. else
  1744. {
  1745. bfd_coff_swap_aux_in (input_bfd, esym, isymp->n_type,
  1746. isymp->n_sclass, i, isymp->n_numaux, &aux);
  1747. auxp = &aux;
  1748. }
  1749. if (isymp->n_sclass == C_FILE)
  1750. {
  1751. /* If this is a long filename, we must put it in the
  1752. string table. */
  1753. if (auxp->x_file.x_n.x_n.x_zeroes == 0
  1754. && auxp->x_file.x_n.x_n.x_offset != 0)
  1755. {
  1756. const char *filename;
  1757. bfd_size_type indx;
  1758. BFD_ASSERT (auxp->x_file.x_n.x_n.x_offset
  1759. >= STRING_SIZE_SIZE);
  1760. if (strings == NULL)
  1761. {
  1762. strings = _bfd_coff_read_string_table (input_bfd);
  1763. if (strings == NULL)
  1764. return false;
  1765. }
  1766. if ((bfd_size_type) auxp->x_file.x_n.x_n.x_offset >= obj_coff_strings_len (input_bfd))
  1767. filename = _("<corrupt>");
  1768. else
  1769. filename = strings + auxp->x_file.x_n.x_n.x_offset;
  1770. indx = _bfd_stringtab_add (flaginfo->strtab, filename,
  1771. hash, copy);
  1772. if (indx == (bfd_size_type) -1)
  1773. return false;
  1774. auxp->x_file.x_n.x_n.x_offset = STRING_SIZE_SIZE + indx;
  1775. }
  1776. }
  1777. else if ((isymp->n_sclass != C_STAT || isymp->n_type != T_NULL)
  1778. && isymp->n_sclass != C_NT_WEAK)
  1779. {
  1780. unsigned long indx;
  1781. if (ISFCN (isymp->n_type)
  1782. || ISTAG (isymp->n_sclass)
  1783. || isymp->n_sclass == C_BLOCK
  1784. || isymp->n_sclass == C_FCN)
  1785. {
  1786. indx = auxp->x_sym.x_fcnary.x_fcn.x_endndx.l;
  1787. if (indx > 0
  1788. && indx < obj_raw_syment_count (input_bfd))
  1789. {
  1790. /* We look forward through the symbol for
  1791. the index of the next symbol we are going
  1792. to include. I don't know if this is
  1793. entirely right. */
  1794. while ((flaginfo->sym_indices[indx] < 0
  1795. || ((bfd_size_type) flaginfo->sym_indices[indx]
  1796. < syment_base))
  1797. && indx < obj_raw_syment_count (input_bfd))
  1798. ++indx;
  1799. if (indx >= obj_raw_syment_count (input_bfd))
  1800. indx = output_index;
  1801. else
  1802. indx = flaginfo->sym_indices[indx];
  1803. auxp->x_sym.x_fcnary.x_fcn.x_endndx.l = indx;
  1804. }
  1805. }
  1806. indx = auxp->x_sym.x_tagndx.l;
  1807. if (indx > 0 && indx < obj_raw_syment_count (input_bfd))
  1808. {
  1809. long symindx;
  1810. symindx = flaginfo->sym_indices[indx];
  1811. if (symindx < 0)
  1812. auxp->x_sym.x_tagndx.l = 0;
  1813. else
  1814. auxp->x_sym.x_tagndx.l = symindx;
  1815. }
  1816. /* The .bf symbols are supposed to be linked through
  1817. the endndx field. We need to carry this list
  1818. across object files. */
  1819. if (i == 0
  1820. && h == NULL
  1821. && isymp->n_sclass == C_FCN
  1822. && (isymp->_n._n_n._n_zeroes != 0
  1823. || isymp->_n._n_n._n_offset == 0)
  1824. && isymp->_n._n_name[0] == '.'
  1825. && isymp->_n._n_name[1] == 'b'
  1826. && isymp->_n._n_name[2] == 'f'
  1827. && isymp->_n._n_name[3] == '\0')
  1828. {
  1829. if (flaginfo->last_bf_index != -1)
  1830. {
  1831. flaginfo->last_bf.x_sym.x_fcnary.x_fcn.x_endndx.l =
  1832. *indexp;
  1833. if ((bfd_size_type) flaginfo->last_bf_index
  1834. >= syment_base)
  1835. {
  1836. void *auxout;
  1837. /* The last .bf symbol is in this input
  1838. file. This will only happen if the
  1839. assembler did not set up the .bf
  1840. endndx symbols correctly. */
  1841. auxout = (flaginfo->outsyms
  1842. + ((flaginfo->last_bf_index
  1843. - syment_base)
  1844. * osymesz));
  1845. bfd_coff_swap_aux_out (output_bfd,
  1846. &flaginfo->last_bf,
  1847. isymp->n_type,
  1848. isymp->n_sclass,
  1849. 0, isymp->n_numaux,
  1850. auxout);
  1851. }
  1852. else
  1853. {
  1854. file_ptr pos;
  1855. /* We have already written out the last
  1856. .bf aux entry. We need to write it
  1857. out again. We borrow *outsym
  1858. temporarily. FIXME: This case should
  1859. be made faster. */
  1860. bfd_coff_swap_aux_out (output_bfd,
  1861. &flaginfo->last_bf,
  1862. isymp->n_type,
  1863. isymp->n_sclass,
  1864. 0, isymp->n_numaux,
  1865. outsym);
  1866. pos = obj_sym_filepos (output_bfd);
  1867. pos += flaginfo->last_bf_index * osymesz;
  1868. if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
  1869. || (bfd_bwrite (outsym, osymesz, output_bfd)
  1870. != osymesz))
  1871. return false;
  1872. }
  1873. }
  1874. if (auxp->x_sym.x_fcnary.x_fcn.x_endndx.l != 0)
  1875. flaginfo->last_bf_index = -1;
  1876. else
  1877. {
  1878. /* The endndx field of this aux entry must
  1879. be updated with the symbol number of the
  1880. next .bf symbol. */
  1881. flaginfo->last_bf = *auxp;
  1882. flaginfo->last_bf_index = (((outsym - flaginfo->outsyms)
  1883. / osymesz)
  1884. + syment_base);
  1885. }
  1886. }
  1887. }
  1888. if (h == NULL)
  1889. {
  1890. bfd_coff_swap_aux_out (output_bfd, auxp, isymp->n_type,
  1891. isymp->n_sclass, i, isymp->n_numaux,
  1892. outsym);
  1893. outsym += osymesz;
  1894. }
  1895. esym += isymesz;
  1896. }
  1897. }
  1898. indexp += add;
  1899. isymp += add;
  1900. sym_hash += add;
  1901. }
  1902. /* Relocate the line numbers, unless we are stripping them. */
  1903. if (flaginfo->info->strip == strip_none
  1904. || flaginfo->info->strip == strip_some)
  1905. {
  1906. for (o = input_bfd->sections; o != NULL; o = o->next)
  1907. {
  1908. bfd_vma offset;
  1909. bfd_byte *eline;
  1910. bfd_byte *elineend;
  1911. bfd_byte *oeline;
  1912. bool skipping;
  1913. file_ptr pos;
  1914. bfd_size_type amt;
  1915. /* FIXME: If SEC_HAS_CONTENTS is not for the section, then
  1916. build_link_order in ldwrite.c will not have created a
  1917. link order, which means that we will not have seen this
  1918. input section in _bfd_coff_final_link, which means that
  1919. we will not have allocated space for the line numbers of
  1920. this section. I don't think line numbers can be
  1921. meaningful for a section which does not have
  1922. SEC_HAS_CONTENTS set, but, if they do, this must be
  1923. changed. */
  1924. if (o->lineno_count == 0
  1925. || (o->output_section->flags & SEC_HAS_CONTENTS) == 0)
  1926. continue;
  1927. if (bfd_seek (input_bfd, o->line_filepos, SEEK_SET) != 0
  1928. || bfd_bread (flaginfo->linenos, linesz * o->lineno_count,
  1929. input_bfd) != linesz * o->lineno_count)
  1930. return false;
  1931. offset = o->output_section->vma + o->output_offset - o->vma;
  1932. eline = flaginfo->linenos;
  1933. oeline = flaginfo->linenos;
  1934. elineend = eline + linesz * o->lineno_count;
  1935. skipping = false;
  1936. for (; eline < elineend; eline += linesz)
  1937. {
  1938. struct internal_lineno iline;
  1939. bfd_coff_swap_lineno_in (input_bfd, eline, &iline);
  1940. if (iline.l_lnno != 0)
  1941. iline.l_addr.l_paddr += offset;
  1942. else if (iline.l_addr.l_symndx >= 0
  1943. && ((unsigned long) iline.l_addr.l_symndx
  1944. < obj_raw_syment_count (input_bfd)))
  1945. {
  1946. long indx;
  1947. indx = flaginfo->sym_indices[iline.l_addr.l_symndx];
  1948. if (indx < 0)
  1949. {
  1950. /* These line numbers are attached to a symbol
  1951. which we are stripping. We must discard the
  1952. line numbers because reading them back with
  1953. no associated symbol (or associating them all
  1954. with symbol #0) will fail. We can't regain
  1955. the space in the output file, but at least
  1956. they're dense. */
  1957. skipping = true;
  1958. }
  1959. else
  1960. {
  1961. struct internal_syment is;
  1962. union internal_auxent ia;
  1963. /* Fix up the lnnoptr field in the aux entry of
  1964. the symbol. It turns out that we can't do
  1965. this when we modify the symbol aux entries,
  1966. because gas sometimes screws up the lnnoptr
  1967. field and makes it an offset from the start
  1968. of the line numbers rather than an absolute
  1969. file index. */
  1970. bfd_coff_swap_sym_in (output_bfd,
  1971. (flaginfo->outsyms
  1972. + ((indx - syment_base)
  1973. * osymesz)), &is);
  1974. if ((ISFCN (is.n_type)
  1975. || is.n_sclass == C_BLOCK)
  1976. && is.n_numaux >= 1)
  1977. {
  1978. void *auxptr;
  1979. auxptr = (flaginfo->outsyms
  1980. + ((indx - syment_base + 1)
  1981. * osymesz));
  1982. bfd_coff_swap_aux_in (output_bfd, auxptr,
  1983. is.n_type, is.n_sclass,
  1984. 0, is.n_numaux, &ia);
  1985. ia.x_sym.x_fcnary.x_fcn.x_lnnoptr =
  1986. (o->output_section->line_filepos
  1987. + o->output_section->lineno_count * linesz
  1988. + eline - flaginfo->linenos);
  1989. bfd_coff_swap_aux_out (output_bfd, &ia,
  1990. is.n_type, is.n_sclass, 0,
  1991. is.n_numaux, auxptr);
  1992. }
  1993. skipping = false;
  1994. }
  1995. iline.l_addr.l_symndx = indx;
  1996. }
  1997. if (!skipping)
  1998. {
  1999. bfd_coff_swap_lineno_out (output_bfd, &iline, oeline);
  2000. oeline += linesz;
  2001. }
  2002. }
  2003. pos = o->output_section->line_filepos;
  2004. pos += o->output_section->lineno_count * linesz;
  2005. amt = oeline - flaginfo->linenos;
  2006. if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
  2007. || bfd_bwrite (flaginfo->linenos, amt, output_bfd) != amt)
  2008. return false;
  2009. o->output_section->lineno_count += amt / linesz;
  2010. }
  2011. }
  2012. /* If we swapped out a C_FILE symbol, guess that the next C_FILE
  2013. symbol will be the first symbol in the next input file. In the
  2014. normal case, this will save us from writing out the C_FILE symbol
  2015. again. */
  2016. if (flaginfo->last_file_index != -1
  2017. && (bfd_size_type) flaginfo->last_file_index >= syment_base)
  2018. {
  2019. flaginfo->last_file.n_value = output_index;
  2020. bfd_coff_swap_sym_out (output_bfd, &flaginfo->last_file,
  2021. (flaginfo->outsyms
  2022. + ((flaginfo->last_file_index - syment_base)
  2023. * osymesz)));
  2024. }
  2025. /* Write the modified symbols to the output file. */
  2026. if (outsym > flaginfo->outsyms)
  2027. {
  2028. file_ptr pos;
  2029. bfd_size_type amt;
  2030. pos = obj_sym_filepos (output_bfd) + syment_base * osymesz;
  2031. amt = outsym - flaginfo->outsyms;
  2032. if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
  2033. || bfd_bwrite (flaginfo->outsyms, amt, output_bfd) != amt)
  2034. return false;
  2035. BFD_ASSERT ((obj_raw_syment_count (output_bfd)
  2036. + (outsym - flaginfo->outsyms) / osymesz)
  2037. == output_index);
  2038. obj_raw_syment_count (output_bfd) = output_index;
  2039. }
  2040. /* Relocate the contents of each section. */
  2041. adjust_symndx = coff_backend_info (input_bfd)->_bfd_coff_adjust_symndx;
  2042. for (o = input_bfd->sections; o != NULL; o = o->next)
  2043. {
  2044. bfd_byte *contents;
  2045. struct coff_section_tdata *secdata;
  2046. if (! o->linker_mark)
  2047. /* This section was omitted from the link. */
  2048. continue;
  2049. if ((o->flags & SEC_LINKER_CREATED) != 0)
  2050. continue;
  2051. if ((o->flags & SEC_HAS_CONTENTS) == 0
  2052. || (o->size == 0 && (o->flags & SEC_RELOC) == 0))
  2053. {
  2054. if ((o->flags & SEC_RELOC) != 0
  2055. && o->reloc_count != 0)
  2056. {
  2057. _bfd_error_handler
  2058. /* xgettext: c-format */
  2059. (_("%pB: relocs in section `%pA', but it has no contents"),
  2060. input_bfd, o);
  2061. bfd_set_error (bfd_error_no_contents);
  2062. return false;
  2063. }
  2064. continue;
  2065. }
  2066. secdata = coff_section_data (input_bfd, o);
  2067. if (secdata != NULL && secdata->contents != NULL)
  2068. contents = secdata->contents;
  2069. else
  2070. {
  2071. contents = flaginfo->contents;
  2072. if (! bfd_get_full_section_contents (input_bfd, o, &contents))
  2073. return false;
  2074. }
  2075. if ((o->flags & SEC_RELOC) != 0)
  2076. {
  2077. int target_index;
  2078. struct internal_reloc *internal_relocs;
  2079. struct internal_reloc *irel;
  2080. /* Read in the relocs. */
  2081. target_index = o->output_section->target_index;
  2082. internal_relocs = (_bfd_coff_read_internal_relocs
  2083. (input_bfd, o, false, flaginfo->external_relocs,
  2084. bfd_link_relocatable (flaginfo->info),
  2085. (bfd_link_relocatable (flaginfo->info)
  2086. ? (flaginfo->section_info[target_index].relocs
  2087. + o->output_section->reloc_count)
  2088. : flaginfo->internal_relocs)));
  2089. if (internal_relocs == NULL
  2090. && o->reloc_count > 0)
  2091. return false;
  2092. /* Run through the relocs looking for relocs against symbols
  2093. coming from discarded sections and complain about them. */
  2094. irel = internal_relocs;
  2095. for (; irel < &internal_relocs[o->reloc_count]; irel++)
  2096. {
  2097. struct coff_link_hash_entry *h;
  2098. asection *ps = NULL;
  2099. long symndx = irel->r_symndx;
  2100. if (symndx < 0)
  2101. continue;
  2102. h = obj_coff_sym_hashes (input_bfd)[symndx];
  2103. if (h == NULL)
  2104. continue;
  2105. while (h->root.type == bfd_link_hash_indirect
  2106. || h->root.type == bfd_link_hash_warning)
  2107. h = (struct coff_link_hash_entry *) h->root.u.i.link;
  2108. if (h->root.type == bfd_link_hash_defined
  2109. || h->root.type == bfd_link_hash_defweak)
  2110. ps = h->root.u.def.section;
  2111. if (ps == NULL)
  2112. continue;
  2113. /* Complain if definition comes from an excluded section. */
  2114. if (ps->flags & SEC_EXCLUDE)
  2115. (*flaginfo->info->callbacks->einfo)
  2116. /* xgettext: c-format */
  2117. (_("%X`%s' referenced in section `%pA' of %pB: "
  2118. "defined in discarded section `%pA' of %pB\n"),
  2119. h->root.root.string, o, input_bfd, ps, ps->owner);
  2120. }
  2121. /* Call processor specific code to relocate the section
  2122. contents. */
  2123. if (! bfd_coff_relocate_section (output_bfd, flaginfo->info,
  2124. input_bfd, o,
  2125. contents,
  2126. internal_relocs,
  2127. flaginfo->internal_syms,
  2128. flaginfo->sec_ptrs))
  2129. return false;
  2130. if (bfd_link_relocatable (flaginfo->info))
  2131. {
  2132. bfd_vma offset;
  2133. struct internal_reloc *irelend;
  2134. struct coff_link_hash_entry **rel_hash;
  2135. offset = o->output_section->vma + o->output_offset - o->vma;
  2136. irel = internal_relocs;
  2137. irelend = irel + o->reloc_count;
  2138. rel_hash = (flaginfo->section_info[target_index].rel_hashes
  2139. + o->output_section->reloc_count);
  2140. for (; irel < irelend; irel++, rel_hash++)
  2141. {
  2142. struct coff_link_hash_entry *h;
  2143. bool adjusted;
  2144. *rel_hash = NULL;
  2145. /* Adjust the reloc address and symbol index. */
  2146. irel->r_vaddr += offset;
  2147. if (irel->r_symndx == -1)
  2148. continue;
  2149. if (adjust_symndx)
  2150. {
  2151. if (! (*adjust_symndx) (output_bfd, flaginfo->info,
  2152. input_bfd, o, irel,
  2153. &adjusted))
  2154. return false;
  2155. if (adjusted)
  2156. continue;
  2157. }
  2158. h = obj_coff_sym_hashes (input_bfd)[irel->r_symndx];
  2159. if (h != NULL)
  2160. {
  2161. /* This is a global symbol. */
  2162. if (h->indx >= 0)
  2163. irel->r_symndx = h->indx;
  2164. else
  2165. {
  2166. /* This symbol is being written at the end
  2167. of the file, and we do not yet know the
  2168. symbol index. We save the pointer to the
  2169. hash table entry in the rel_hash list.
  2170. We set the indx field to -2 to indicate
  2171. that this symbol must not be stripped. */
  2172. *rel_hash = h;
  2173. h->indx = -2;
  2174. }
  2175. }
  2176. else
  2177. {
  2178. long indx;
  2179. indx = flaginfo->sym_indices[irel->r_symndx];
  2180. if (indx != -1)
  2181. irel->r_symndx = indx;
  2182. else
  2183. {
  2184. struct internal_syment *is;
  2185. const char *name;
  2186. char buf[SYMNMLEN + 1];
  2187. /* This reloc is against a symbol we are
  2188. stripping. This should have been handled
  2189. by the 'dont_skip_symbol' code in the while
  2190. loop at the top of this function. */
  2191. is = flaginfo->internal_syms + irel->r_symndx;
  2192. name = (_bfd_coff_internal_syment_name
  2193. (input_bfd, is, buf));
  2194. if (name == NULL)
  2195. return false;
  2196. (*flaginfo->info->callbacks->unattached_reloc)
  2197. (flaginfo->info, name, input_bfd, o, irel->r_vaddr);
  2198. }
  2199. }
  2200. }
  2201. o->output_section->reloc_count += o->reloc_count;
  2202. }
  2203. }
  2204. /* Write out the modified section contents. */
  2205. if (secdata == NULL || secdata->stab_info == NULL)
  2206. {
  2207. file_ptr loc = (o->output_offset
  2208. * bfd_octets_per_byte (output_bfd, o));
  2209. if (! bfd_set_section_contents (output_bfd, o->output_section,
  2210. contents, loc, o->size))
  2211. return false;
  2212. }
  2213. else
  2214. {
  2215. if (! (_bfd_write_section_stabs
  2216. (output_bfd, &coff_hash_table (flaginfo->info)->stab_info,
  2217. o, &secdata->stab_info, contents)))
  2218. return false;
  2219. }
  2220. }
  2221. if (! flaginfo->info->keep_memory
  2222. && ! _bfd_coff_free_symbols (input_bfd))
  2223. return false;
  2224. return true;
  2225. }
  2226. /* Write out a global symbol. Called via bfd_hash_traverse. */
  2227. bool
  2228. _bfd_coff_write_global_sym (struct bfd_hash_entry *bh, void *data)
  2229. {
  2230. struct coff_link_hash_entry *h = (struct coff_link_hash_entry *) bh;
  2231. struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
  2232. bfd *output_bfd;
  2233. struct internal_syment isym;
  2234. bfd_size_type symesz;
  2235. unsigned int i;
  2236. file_ptr pos;
  2237. output_bfd = flaginfo->output_bfd;
  2238. if (h->root.type == bfd_link_hash_warning)
  2239. {
  2240. h = (struct coff_link_hash_entry *) h->root.u.i.link;
  2241. if (h->root.type == bfd_link_hash_new)
  2242. return true;
  2243. }
  2244. if (h->indx >= 0)
  2245. return true;
  2246. if (h->indx != -2
  2247. && (flaginfo->info->strip == strip_all
  2248. || (flaginfo->info->strip == strip_some
  2249. && (bfd_hash_lookup (flaginfo->info->keep_hash,
  2250. h->root.root.string, false, false)
  2251. == NULL))))
  2252. return true;
  2253. switch (h->root.type)
  2254. {
  2255. default:
  2256. case bfd_link_hash_new:
  2257. case bfd_link_hash_warning:
  2258. abort ();
  2259. return false;
  2260. case bfd_link_hash_undefined:
  2261. if (h->indx == -3)
  2262. return true;
  2263. /* Fall through. */
  2264. case bfd_link_hash_undefweak:
  2265. isym.n_scnum = N_UNDEF;
  2266. isym.n_value = 0;
  2267. break;
  2268. case bfd_link_hash_defined:
  2269. case bfd_link_hash_defweak:
  2270. {
  2271. asection *sec;
  2272. sec = h->root.u.def.section->output_section;
  2273. if (bfd_is_abs_section (sec))
  2274. isym.n_scnum = N_ABS;
  2275. else
  2276. isym.n_scnum = sec->target_index;
  2277. isym.n_value = (h->root.u.def.value
  2278. + h->root.u.def.section->output_offset);
  2279. if (! obj_pe (flaginfo->output_bfd))
  2280. isym.n_value += sec->vma;
  2281. #ifdef BFD64
  2282. if (isym.n_value > (bfd_vma) 0xffffffff)
  2283. {
  2284. if (! h->root.linker_def)
  2285. {
  2286. char value_buf[128];
  2287. sprintf_vma (value_buf, isym.n_value);
  2288. _bfd_error_handler
  2289. (_("%pB: stripping non-representable symbol '%s' (value 0x%s)"),
  2290. output_bfd, h->root.root.string, value_buf);
  2291. }
  2292. return true;
  2293. }
  2294. #endif
  2295. }
  2296. break;
  2297. case bfd_link_hash_common:
  2298. isym.n_scnum = N_UNDEF;
  2299. isym.n_value = h->root.u.c.size;
  2300. break;
  2301. case bfd_link_hash_indirect:
  2302. /* Just ignore these. They can't be handled anyhow. */
  2303. return true;
  2304. }
  2305. if (strlen (h->root.root.string) <= SYMNMLEN)
  2306. strncpy (isym._n._n_name, h->root.root.string, SYMNMLEN);
  2307. else
  2308. {
  2309. bool hash;
  2310. bfd_size_type indx;
  2311. hash = true;
  2312. if (flaginfo->info->traditional_format)
  2313. hash = false;
  2314. indx = _bfd_stringtab_add (flaginfo->strtab, h->root.root.string, hash,
  2315. false);
  2316. if (indx == (bfd_size_type) -1)
  2317. {
  2318. flaginfo->failed = true;
  2319. return false;
  2320. }
  2321. isym._n._n_n._n_zeroes = 0;
  2322. isym._n._n_n._n_offset = STRING_SIZE_SIZE + indx;
  2323. }
  2324. isym.n_sclass = h->symbol_class;
  2325. isym.n_type = h->type;
  2326. if (isym.n_sclass == C_NULL)
  2327. isym.n_sclass = C_EXT;
  2328. /* If doing task linking and this is the pass where we convert
  2329. defined globals to statics, then do that conversion now. If the
  2330. symbol is not being converted, just ignore it and it will be
  2331. output during a later pass. */
  2332. if (flaginfo->global_to_static)
  2333. {
  2334. if (! IS_EXTERNAL (output_bfd, isym))
  2335. return true;
  2336. isym.n_sclass = C_STAT;
  2337. }
  2338. /* When a weak symbol is not overridden by a strong one,
  2339. turn it into an external symbol when not building a
  2340. shared or relocatable object. */
  2341. if (! bfd_link_pic (flaginfo->info)
  2342. && ! bfd_link_relocatable (flaginfo->info)
  2343. && IS_WEAK_EXTERNAL (flaginfo->output_bfd, isym))
  2344. isym.n_sclass = C_EXT;
  2345. isym.n_numaux = h->numaux;
  2346. bfd_coff_swap_sym_out (output_bfd, &isym, flaginfo->outsyms);
  2347. symesz = bfd_coff_symesz (output_bfd);
  2348. pos = obj_sym_filepos (output_bfd);
  2349. pos += obj_raw_syment_count (output_bfd) * symesz;
  2350. if (bfd_seek (output_bfd, pos, SEEK_SET) != 0
  2351. || bfd_bwrite (flaginfo->outsyms, symesz, output_bfd) != symesz)
  2352. {
  2353. flaginfo->failed = true;
  2354. return false;
  2355. }
  2356. h->indx = obj_raw_syment_count (output_bfd);
  2357. ++obj_raw_syment_count (output_bfd);
  2358. /* Write out any associated aux entries. Most of the aux entries
  2359. will have been modified in _bfd_coff_link_input_bfd. We have to
  2360. handle section aux entries here, now that we have the final
  2361. relocation and line number counts. */
  2362. for (i = 0; i < isym.n_numaux; i++)
  2363. {
  2364. union internal_auxent *auxp;
  2365. auxp = h->aux + i;
  2366. /* Look for a section aux entry here using the same tests that
  2367. coff_swap_aux_out uses. */
  2368. if (i == 0
  2369. && (isym.n_sclass == C_STAT
  2370. || isym.n_sclass == C_HIDDEN)
  2371. && isym.n_type == T_NULL
  2372. && (h->root.type == bfd_link_hash_defined
  2373. || h->root.type == bfd_link_hash_defweak))
  2374. {
  2375. asection *sec;
  2376. sec = h->root.u.def.section->output_section;
  2377. if (sec != NULL)
  2378. {
  2379. auxp->x_scn.x_scnlen = sec->size;
  2380. /* For PE, an overflow on the final link reportedly does
  2381. not matter. FIXME: Why not? */
  2382. if (sec->reloc_count > 0xffff
  2383. && (! obj_pe (output_bfd)
  2384. || bfd_link_relocatable (flaginfo->info)))
  2385. _bfd_error_handler
  2386. /* xgettext: c-format */
  2387. (_("%pB: %pA: reloc overflow: %#x > 0xffff"),
  2388. output_bfd, sec, sec->reloc_count);
  2389. if (sec->lineno_count > 0xffff
  2390. && (! obj_pe (output_bfd)
  2391. || bfd_link_relocatable (flaginfo->info)))
  2392. _bfd_error_handler
  2393. /* xgettext: c-format */
  2394. (_("%pB: warning: %pA: line number overflow: %#x > 0xffff"),
  2395. output_bfd, sec, sec->lineno_count);
  2396. auxp->x_scn.x_nreloc = sec->reloc_count;
  2397. auxp->x_scn.x_nlinno = sec->lineno_count;
  2398. auxp->x_scn.x_checksum = 0;
  2399. auxp->x_scn.x_associated = 0;
  2400. auxp->x_scn.x_comdat = 0;
  2401. }
  2402. }
  2403. bfd_coff_swap_aux_out (output_bfd, auxp, isym.n_type,
  2404. isym.n_sclass, (int) i, isym.n_numaux,
  2405. flaginfo->outsyms);
  2406. if (bfd_bwrite (flaginfo->outsyms, symesz, output_bfd) != symesz)
  2407. {
  2408. flaginfo->failed = true;
  2409. return false;
  2410. }
  2411. ++obj_raw_syment_count (output_bfd);
  2412. }
  2413. return true;
  2414. }
  2415. /* Write out task global symbols, converting them to statics. Called
  2416. via coff_link_hash_traverse. Calls bfd_coff_write_global_sym to do
  2417. the dirty work, if the symbol we are processing needs conversion. */
  2418. bool
  2419. _bfd_coff_write_task_globals (struct coff_link_hash_entry *h, void *data)
  2420. {
  2421. struct coff_final_link_info *flaginfo = (struct coff_final_link_info *) data;
  2422. bool rtnval = true;
  2423. bool save_global_to_static;
  2424. if (h->root.type == bfd_link_hash_warning)
  2425. h = (struct coff_link_hash_entry *) h->root.u.i.link;
  2426. if (h->indx < 0)
  2427. {
  2428. switch (h->root.type)
  2429. {
  2430. case bfd_link_hash_defined:
  2431. case bfd_link_hash_defweak:
  2432. save_global_to_static = flaginfo->global_to_static;
  2433. flaginfo->global_to_static = true;
  2434. rtnval = _bfd_coff_write_global_sym (&h->root.root, data);
  2435. flaginfo->global_to_static = save_global_to_static;
  2436. break;
  2437. default:
  2438. break;
  2439. }
  2440. }
  2441. return (rtnval);
  2442. }
  2443. /* Handle a link order which is supposed to generate a reloc. */
  2444. bool
  2445. _bfd_coff_reloc_link_order (bfd *output_bfd,
  2446. struct coff_final_link_info *flaginfo,
  2447. asection *output_section,
  2448. struct bfd_link_order *link_order)
  2449. {
  2450. reloc_howto_type *howto;
  2451. struct internal_reloc *irel;
  2452. struct coff_link_hash_entry **rel_hash_ptr;
  2453. howto = bfd_reloc_type_lookup (output_bfd, link_order->u.reloc.p->reloc);
  2454. if (howto == NULL)
  2455. {
  2456. bfd_set_error (bfd_error_bad_value);
  2457. return false;
  2458. }
  2459. if (link_order->u.reloc.p->addend != 0)
  2460. {
  2461. bfd_size_type size;
  2462. bfd_byte *buf;
  2463. bfd_reloc_status_type rstat;
  2464. bool ok;
  2465. file_ptr loc;
  2466. size = bfd_get_reloc_size (howto);
  2467. buf = (bfd_byte *) bfd_zmalloc (size);
  2468. if (buf == NULL && size != 0)
  2469. return false;
  2470. rstat = _bfd_relocate_contents (howto, output_bfd,
  2471. (bfd_vma) link_order->u.reloc.p->addend,
  2472. buf);
  2473. switch (rstat)
  2474. {
  2475. case bfd_reloc_ok:
  2476. break;
  2477. default:
  2478. case bfd_reloc_outofrange:
  2479. abort ();
  2480. case bfd_reloc_overflow:
  2481. (*flaginfo->info->callbacks->reloc_overflow)
  2482. (flaginfo->info, NULL,
  2483. (link_order->type == bfd_section_reloc_link_order
  2484. ? bfd_section_name (link_order->u.reloc.p->u.section)
  2485. : link_order->u.reloc.p->u.name),
  2486. howto->name, link_order->u.reloc.p->addend,
  2487. (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
  2488. break;
  2489. }
  2490. loc = link_order->offset * bfd_octets_per_byte (output_bfd,
  2491. output_section);
  2492. ok = bfd_set_section_contents (output_bfd, output_section, buf,
  2493. loc, size);
  2494. free (buf);
  2495. if (! ok)
  2496. return false;
  2497. }
  2498. /* Store the reloc information in the right place. It will get
  2499. swapped and written out at the end of the final_link routine. */
  2500. irel = (flaginfo->section_info[output_section->target_index].relocs
  2501. + output_section->reloc_count);
  2502. rel_hash_ptr = (flaginfo->section_info[output_section->target_index].rel_hashes
  2503. + output_section->reloc_count);
  2504. memset (irel, 0, sizeof (struct internal_reloc));
  2505. *rel_hash_ptr = NULL;
  2506. irel->r_vaddr = output_section->vma + link_order->offset;
  2507. if (link_order->type == bfd_section_reloc_link_order)
  2508. {
  2509. /* We need to somehow locate a symbol in the right section. The
  2510. symbol must either have a value of zero, or we must adjust
  2511. the addend by the value of the symbol. FIXME: Write this
  2512. when we need it. The old linker couldn't handle this anyhow. */
  2513. abort ();
  2514. *rel_hash_ptr = NULL;
  2515. irel->r_symndx = 0;
  2516. }
  2517. else
  2518. {
  2519. struct coff_link_hash_entry *h;
  2520. h = ((struct coff_link_hash_entry *)
  2521. bfd_wrapped_link_hash_lookup (output_bfd, flaginfo->info,
  2522. link_order->u.reloc.p->u.name,
  2523. false, false, true));
  2524. if (h != NULL)
  2525. {
  2526. if (h->indx >= 0)
  2527. irel->r_symndx = h->indx;
  2528. else
  2529. {
  2530. /* Set the index to -2 to force this symbol to get
  2531. written out. */
  2532. h->indx = -2;
  2533. *rel_hash_ptr = h;
  2534. irel->r_symndx = 0;
  2535. }
  2536. }
  2537. else
  2538. {
  2539. (*flaginfo->info->callbacks->unattached_reloc)
  2540. (flaginfo->info, link_order->u.reloc.p->u.name,
  2541. (bfd *) NULL, (asection *) NULL, (bfd_vma) 0);
  2542. irel->r_symndx = 0;
  2543. }
  2544. }
  2545. /* FIXME: Is this always right? */
  2546. irel->r_type = howto->type;
  2547. /* r_size is only used on the RS/6000, which needs its own linker
  2548. routines anyhow. r_extern is only used for ECOFF. */
  2549. /* FIXME: What is the right value for r_offset? Is zero OK? */
  2550. ++output_section->reloc_count;
  2551. return true;
  2552. }
  2553. /* A basic reloc handling routine which may be used by processors with
  2554. simple relocs. */
  2555. bool
  2556. _bfd_coff_generic_relocate_section (bfd *output_bfd,
  2557. struct bfd_link_info *info,
  2558. bfd *input_bfd,
  2559. asection *input_section,
  2560. bfd_byte *contents,
  2561. struct internal_reloc *relocs,
  2562. struct internal_syment *syms,
  2563. asection **sections)
  2564. {
  2565. struct internal_reloc *rel;
  2566. struct internal_reloc *relend;
  2567. rel = relocs;
  2568. relend = rel + input_section->reloc_count;
  2569. for (; rel < relend; rel++)
  2570. {
  2571. long symndx;
  2572. struct coff_link_hash_entry *h;
  2573. struct internal_syment *sym;
  2574. bfd_vma addend;
  2575. bfd_vma val;
  2576. asection *sec;
  2577. reloc_howto_type *howto;
  2578. bfd_reloc_status_type rstat;
  2579. symndx = rel->r_symndx;
  2580. if (symndx == -1)
  2581. {
  2582. h = NULL;
  2583. sym = NULL;
  2584. }
  2585. else if (symndx < 0
  2586. || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
  2587. {
  2588. _bfd_error_handler
  2589. /* xgettext: c-format */
  2590. (_("%pB: illegal symbol index %ld in relocs"), input_bfd, symndx);
  2591. return false;
  2592. }
  2593. else
  2594. {
  2595. h = obj_coff_sym_hashes (input_bfd)[symndx];
  2596. sym = syms + symndx;
  2597. }
  2598. /* COFF treats common symbols in one of two ways. Either the
  2599. size of the symbol is included in the section contents, or it
  2600. is not. We assume that the size is not included, and force
  2601. the rtype_to_howto function to adjust the addend as needed. */
  2602. if (sym != NULL && sym->n_scnum != 0)
  2603. addend = - sym->n_value;
  2604. else
  2605. addend = 0;
  2606. howto = bfd_coff_rtype_to_howto (input_bfd, input_section, rel, h,
  2607. sym, &addend);
  2608. if (howto == NULL)
  2609. return false;
  2610. /* If we are doing a relocatable link, then we can just ignore
  2611. a PC relative reloc that is pcrel_offset. It will already
  2612. have the correct value. If this is not a relocatable link,
  2613. then we should ignore the symbol value. */
  2614. if (howto->pc_relative && howto->pcrel_offset)
  2615. {
  2616. if (bfd_link_relocatable (info))
  2617. continue;
  2618. if (sym != NULL && sym->n_scnum != 0)
  2619. addend += sym->n_value;
  2620. }
  2621. val = 0;
  2622. sec = NULL;
  2623. if (h == NULL)
  2624. {
  2625. if (symndx == -1)
  2626. {
  2627. sec = bfd_abs_section_ptr;
  2628. val = 0;
  2629. }
  2630. else
  2631. {
  2632. sec = sections[symndx];
  2633. /* PR 19623: Relocations against symbols in
  2634. the absolute sections should ignored. */
  2635. if (bfd_is_abs_section (sec))
  2636. continue;
  2637. val = (sec->output_section->vma
  2638. + sec->output_offset
  2639. + sym->n_value);
  2640. if (! obj_pe (input_bfd))
  2641. val -= sec->vma;
  2642. }
  2643. }
  2644. else
  2645. {
  2646. if (h->root.type == bfd_link_hash_defined
  2647. || h->root.type == bfd_link_hash_defweak)
  2648. {
  2649. /* Defined weak symbols are a GNU extension. */
  2650. sec = h->root.u.def.section;
  2651. val = (h->root.u.def.value
  2652. + sec->output_section->vma
  2653. + sec->output_offset);
  2654. }
  2655. else if (h->root.type == bfd_link_hash_undefweak)
  2656. {
  2657. if (h->symbol_class == C_NT_WEAK && h->numaux == 1)
  2658. {
  2659. /* See _Microsoft Portable Executable and Common Object
  2660. File Format Specification_, section 5.5.3.
  2661. Note that weak symbols without aux records are a GNU
  2662. extension.
  2663. FIXME: All weak externals are treated as having
  2664. characteristic IMAGE_WEAK_EXTERN_SEARCH_NOLIBRARY (1).
  2665. These behave as per SVR4 ABI: A library member
  2666. will resolve a weak external only if a normal
  2667. external causes the library member to be linked.
  2668. See also linker.c: generic_link_check_archive_element. */
  2669. struct coff_link_hash_entry *h2 =
  2670. h->auxbfd->tdata.coff_obj_data->sym_hashes[
  2671. h->aux->x_sym.x_tagndx.l];
  2672. if (!h2 || h2->root.type == bfd_link_hash_undefined)
  2673. {
  2674. sec = bfd_abs_section_ptr;
  2675. val = 0;
  2676. }
  2677. else
  2678. {
  2679. sec = h2->root.u.def.section;
  2680. val = h2->root.u.def.value
  2681. + sec->output_section->vma + sec->output_offset;
  2682. }
  2683. }
  2684. else
  2685. /* This is a GNU extension. */
  2686. val = 0;
  2687. }
  2688. else if (! bfd_link_relocatable (info))
  2689. {
  2690. (*info->callbacks->undefined_symbol)
  2691. (info, h->root.root.string, input_bfd, input_section,
  2692. rel->r_vaddr - input_section->vma, true);
  2693. /* Stop the linker from issueing errors about truncated relocs
  2694. referencing this undefined symbol by giving it an address
  2695. that should be in range. */
  2696. val = input_section->output_section->vma;
  2697. }
  2698. }
  2699. /* If the input section defining the symbol has been discarded
  2700. then zero this reloc field. */
  2701. if (sec != NULL && discarded_section (sec))
  2702. {
  2703. _bfd_clear_contents (howto, input_bfd, input_section,
  2704. contents, rel->r_vaddr - input_section->vma);
  2705. continue;
  2706. }
  2707. if (info->base_file)
  2708. {
  2709. /* Emit a reloc if the backend thinks it needs it. */
  2710. if (sym && pe_data (output_bfd)->in_reloc_p (output_bfd, howto))
  2711. {
  2712. /* Relocation to a symbol in a section which isn't
  2713. absolute. We output the address here to a file.
  2714. This file is then read by dlltool when generating the
  2715. reloc section. Note that the base file is not
  2716. portable between systems. We write out a bfd_vma here,
  2717. and dlltool reads in a bfd_vma. */
  2718. bfd_vma addr = (rel->r_vaddr
  2719. - input_section->vma
  2720. + input_section->output_offset
  2721. + input_section->output_section->vma);
  2722. if (coff_data (output_bfd)->pe)
  2723. addr -= pe_data(output_bfd)->pe_opthdr.ImageBase;
  2724. if (fwrite (&addr, 1, sizeof (bfd_vma), (FILE *) info->base_file)
  2725. != sizeof (bfd_vma))
  2726. {
  2727. bfd_set_error (bfd_error_system_call);
  2728. return false;
  2729. }
  2730. }
  2731. }
  2732. rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
  2733. contents,
  2734. rel->r_vaddr - input_section->vma,
  2735. val, addend);
  2736. switch (rstat)
  2737. {
  2738. default:
  2739. abort ();
  2740. case bfd_reloc_ok:
  2741. break;
  2742. case bfd_reloc_outofrange:
  2743. _bfd_error_handler
  2744. /* xgettext: c-format */
  2745. (_("%pB: bad reloc address %#" PRIx64 " in section `%pA'"),
  2746. input_bfd, (uint64_t) rel->r_vaddr, input_section);
  2747. return false;
  2748. case bfd_reloc_overflow:
  2749. {
  2750. /* Ignore any weak undef symbols that may have overflowed. Due to
  2751. PR ld/19011 the base address is now in the upper 64-bit address
  2752. range. This means that when _bfd_final_link_relocate calculates
  2753. the overlow it takes the distance between the symbol and the VMA
  2754. which will now always overflow as 0 - 64-bit addr > 32-bit range
  2755. of the relocation. This ends up creating PR ld/26659. */
  2756. if (val == 0
  2757. /* Reverse the hack where 4 is subtracted from the addend. */
  2758. && (addend + 4) == 0
  2759. && sym->n_sclass == C_NT_WEAK
  2760. && bfd_coff_classify_symbol (output_bfd, sym)
  2761. == COFF_SYMBOL_UNDEFINED)
  2762. break;
  2763. const char *name;
  2764. char buf[SYMNMLEN + 1];
  2765. if (symndx == -1)
  2766. name = "*ABS*";
  2767. else if (h != NULL)
  2768. name = NULL;
  2769. else
  2770. {
  2771. name = _bfd_coff_internal_syment_name (input_bfd, sym, buf);
  2772. if (name == NULL)
  2773. return false;
  2774. }
  2775. (*info->callbacks->reloc_overflow)
  2776. (info, (h ? &h->root : NULL), name, howto->name,
  2777. (bfd_vma) 0, input_bfd, input_section,
  2778. rel->r_vaddr - input_section->vma);
  2779. }
  2780. }
  2781. }
  2782. return true;
  2783. }