cofflink.c 90 KB

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