write.c 87 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249
  1. /* write.c - emit .o file
  2. Copyright (C) 1986-2022 Free Software Foundation, Inc.
  3. This file is part of GAS, the GNU Assembler.
  4. GAS is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3, or (at your option)
  7. any later version.
  8. GAS is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with GAS; see the file COPYING. If not, write to the Free
  14. Software Foundation, 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. /* This thing should be set up to do byte ordering correctly. But... */
  17. #include "as.h"
  18. #include "subsegs.h"
  19. #include "obstack.h"
  20. #include "output-file.h"
  21. #include "dwarf2dbg.h"
  22. #include "compress-debug.h"
  23. #ifndef TC_FORCE_RELOCATION
  24. #define TC_FORCE_RELOCATION(FIX) \
  25. (generic_force_reloc (FIX))
  26. #endif
  27. #ifndef TC_FORCE_RELOCATION_ABS
  28. #define TC_FORCE_RELOCATION_ABS(FIX) \
  29. (TC_FORCE_RELOCATION (FIX))
  30. #endif
  31. #define GENERIC_FORCE_RELOCATION_LOCAL(FIX) \
  32. (!(FIX)->fx_pcrel \
  33. || TC_FORCE_RELOCATION (FIX))
  34. #ifndef TC_FORCE_RELOCATION_LOCAL
  35. #define TC_FORCE_RELOCATION_LOCAL GENERIC_FORCE_RELOCATION_LOCAL
  36. #endif
  37. #define GENERIC_FORCE_RELOCATION_SUB_SAME(FIX, SEG) \
  38. (!SEG_NORMAL (SEG))
  39. #ifndef TC_FORCE_RELOCATION_SUB_SAME
  40. #define TC_FORCE_RELOCATION_SUB_SAME GENERIC_FORCE_RELOCATION_SUB_SAME
  41. #endif
  42. #ifndef md_register_arithmetic
  43. # define md_register_arithmetic 1
  44. #endif
  45. #ifndef TC_FORCE_RELOCATION_SUB_ABS
  46. #define TC_FORCE_RELOCATION_SUB_ABS(FIX, SEG) \
  47. (!md_register_arithmetic && (SEG) == reg_section)
  48. #endif
  49. #ifndef TC_FORCE_RELOCATION_SUB_LOCAL
  50. #ifdef DIFF_EXPR_OK
  51. #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG) \
  52. (!md_register_arithmetic && (SEG) == reg_section)
  53. #else
  54. #define TC_FORCE_RELOCATION_SUB_LOCAL(FIX, SEG) 1
  55. #endif
  56. #endif
  57. #ifndef TC_VALIDATE_FIX_SUB
  58. #define TC_VALIDATE_FIX_SUB(FIX, SEG) 0
  59. #endif
  60. #ifndef TC_LINKRELAX_FIXUP
  61. #define TC_LINKRELAX_FIXUP(SEG) 1
  62. #endif
  63. #ifndef MD_APPLY_SYM_VALUE
  64. #define MD_APPLY_SYM_VALUE(FIX) 1
  65. #endif
  66. #ifndef TC_FINALIZE_SYMS_BEFORE_SIZE_SEG
  67. #define TC_FINALIZE_SYMS_BEFORE_SIZE_SEG 1
  68. #endif
  69. #ifndef MD_PCREL_FROM_SECTION
  70. #define MD_PCREL_FROM_SECTION(FIX, SEC) md_pcrel_from (FIX)
  71. #endif
  72. #ifndef TC_FAKE_LABEL
  73. #define TC_FAKE_LABEL(NAME) (strcmp ((NAME), FAKE_LABEL_NAME) == 0)
  74. #endif
  75. /* Positive values of TC_FX_SIZE_SLACK allow a target to define
  76. fixups that far past the end of a frag. Having such fixups
  77. is of course most most likely a bug in setting fx_size correctly.
  78. A negative value disables the fixup check entirely, which is
  79. appropriate for something like the Renesas / SuperH SH_COUNT
  80. reloc. */
  81. #ifndef TC_FX_SIZE_SLACK
  82. #define TC_FX_SIZE_SLACK(FIX) 0
  83. #endif
  84. /* Used to control final evaluation of expressions. */
  85. int finalize_syms = 0;
  86. int symbol_table_frozen;
  87. symbolS *abs_section_sym;
  88. /* Remember the value of dot when parsing expressions. */
  89. addressT dot_value;
  90. /* The frag that dot_value is based from. */
  91. fragS *dot_frag;
  92. /* Relocs generated by ".reloc" pseudo. */
  93. struct reloc_list* reloc_list;
  94. void print_fixup (fixS *);
  95. /* We generally attach relocs to frag chains. However, after we have
  96. chained these all together into a segment, any relocs we add after
  97. that must be attached to a segment. This will include relocs added
  98. in md_estimate_size_for_relax, for example. */
  99. static int frags_chained = 0;
  100. static int n_fixups;
  101. #define RELOC_ENUM enum bfd_reloc_code_real
  102. /* Create a fixS in obstack 'notes'. */
  103. static fixS *
  104. fix_new_internal (fragS *frag, /* Which frag? */
  105. unsigned long where, /* Where in that frag? */
  106. unsigned long size, /* 1, 2, or 4 usually. */
  107. symbolS *add_symbol, /* X_add_symbol. */
  108. symbolS *sub_symbol, /* X_op_symbol. */
  109. offsetT offset, /* X_add_number. */
  110. int pcrel, /* TRUE if PC-relative relocation. */
  111. RELOC_ENUM r_type /* Relocation type. */,
  112. int at_beginning) /* Add to the start of the list? */
  113. {
  114. fixS *fixP;
  115. n_fixups++;
  116. fixP = (fixS *) obstack_alloc (&notes, sizeof (fixS));
  117. fixP->fx_frag = frag;
  118. fixP->fx_where = where;
  119. fixP->fx_size = size;
  120. /* We've made fx_size a narrow field; check that it's wide enough. */
  121. if (fixP->fx_size != size)
  122. {
  123. as_bad (_("field fx_size too small to hold %lu"), size);
  124. abort ();
  125. }
  126. fixP->fx_addsy = add_symbol;
  127. fixP->fx_subsy = sub_symbol;
  128. fixP->fx_offset = offset;
  129. fixP->fx_dot_value = dot_value;
  130. fixP->fx_dot_frag = dot_frag;
  131. fixP->fx_pcrel = pcrel;
  132. fixP->fx_r_type = r_type;
  133. fixP->fx_pcrel_adjust = 0;
  134. fixP->fx_addnumber = 0;
  135. fixP->fx_tcbit = 0;
  136. fixP->fx_tcbit2 = 0;
  137. fixP->fx_done = 0;
  138. fixP->fx_no_overflow = 0;
  139. fixP->fx_signed = 0;
  140. #ifdef USING_CGEN
  141. fixP->fx_cgen.insn = NULL;
  142. fixP->fx_cgen.opinfo = 0;
  143. #endif
  144. #ifdef TC_FIX_TYPE
  145. TC_INIT_FIX_DATA (fixP);
  146. #endif
  147. fixP->fx_file = as_where (&fixP->fx_line);
  148. {
  149. fixS **seg_fix_rootP = (frags_chained
  150. ? &seg_info (now_seg)->fix_root
  151. : &frchain_now->fix_root);
  152. fixS **seg_fix_tailP = (frags_chained
  153. ? &seg_info (now_seg)->fix_tail
  154. : &frchain_now->fix_tail);
  155. if (at_beginning)
  156. {
  157. fixP->fx_next = *seg_fix_rootP;
  158. *seg_fix_rootP = fixP;
  159. if (fixP->fx_next == NULL)
  160. *seg_fix_tailP = fixP;
  161. }
  162. else
  163. {
  164. fixP->fx_next = NULL;
  165. if (*seg_fix_tailP)
  166. (*seg_fix_tailP)->fx_next = fixP;
  167. else
  168. *seg_fix_rootP = fixP;
  169. *seg_fix_tailP = fixP;
  170. }
  171. }
  172. return fixP;
  173. }
  174. /* Create a fixup relative to a symbol (plus a constant). */
  175. fixS *
  176. fix_new (fragS *frag, /* Which frag? */
  177. unsigned long where, /* Where in that frag? */
  178. unsigned long size, /* 1, 2, or 4 usually. */
  179. symbolS *add_symbol, /* X_add_symbol. */
  180. offsetT offset, /* X_add_number. */
  181. int pcrel, /* TRUE if PC-relative relocation. */
  182. RELOC_ENUM r_type /* Relocation type. */)
  183. {
  184. return fix_new_internal (frag, where, size, add_symbol,
  185. (symbolS *) NULL, offset, pcrel, r_type, false);
  186. }
  187. /* Create a fixup for an expression. Currently we only support fixups
  188. for difference expressions. That is itself more than most object
  189. file formats support anyhow. */
  190. fixS *
  191. fix_new_exp (fragS *frag, /* Which frag? */
  192. unsigned long where, /* Where in that frag? */
  193. unsigned long size, /* 1, 2, or 4 usually. */
  194. expressionS *exp, /* Expression. */
  195. int pcrel, /* TRUE if PC-relative relocation. */
  196. RELOC_ENUM r_type /* Relocation type. */)
  197. {
  198. symbolS *add = NULL;
  199. symbolS *sub = NULL;
  200. offsetT off = 0;
  201. switch (exp->X_op)
  202. {
  203. case O_absent:
  204. break;
  205. case O_register:
  206. as_bad (_("register value used as expression"));
  207. break;
  208. case O_add:
  209. /* This comes up when _GLOBAL_OFFSET_TABLE_+(.-L0) is read, if
  210. the difference expression cannot immediately be reduced. */
  211. {
  212. symbolS *stmp = make_expr_symbol (exp);
  213. exp->X_op = O_symbol;
  214. exp->X_op_symbol = 0;
  215. exp->X_add_symbol = stmp;
  216. exp->X_add_number = 0;
  217. return fix_new_exp (frag, where, size, exp, pcrel, r_type);
  218. }
  219. case O_symbol_rva:
  220. add = exp->X_add_symbol;
  221. off = exp->X_add_number;
  222. r_type = BFD_RELOC_RVA;
  223. break;
  224. case O_uminus:
  225. sub = exp->X_add_symbol;
  226. off = exp->X_add_number;
  227. break;
  228. case O_subtract:
  229. sub = exp->X_op_symbol;
  230. /* Fall through. */
  231. case O_symbol:
  232. add = exp->X_add_symbol;
  233. /* Fall through. */
  234. case O_constant:
  235. off = exp->X_add_number;
  236. break;
  237. default:
  238. add = make_expr_symbol (exp);
  239. break;
  240. }
  241. return fix_new_internal (frag, where, size, add, sub, off, pcrel,
  242. r_type, false);
  243. }
  244. /* Create a fixup at the beginning of FRAG. The arguments are the same
  245. as for fix_new, except that WHERE is implicitly 0. */
  246. fixS *
  247. fix_at_start (fragS *frag, unsigned long size, symbolS *add_symbol,
  248. offsetT offset, int pcrel, RELOC_ENUM r_type)
  249. {
  250. return fix_new_internal (frag, 0, size, add_symbol,
  251. (symbolS *) NULL, offset, pcrel, r_type, true);
  252. }
  253. /* Generic function to determine whether a fixup requires a relocation. */
  254. int
  255. generic_force_reloc (fixS *fix)
  256. {
  257. if (fix->fx_r_type == BFD_RELOC_VTABLE_INHERIT
  258. || fix->fx_r_type == BFD_RELOC_VTABLE_ENTRY)
  259. return 1;
  260. if (fix->fx_addsy == NULL)
  261. return 0;
  262. return S_FORCE_RELOC (fix->fx_addsy, fix->fx_subsy == NULL);
  263. }
  264. /* Append a string onto another string, bumping the pointer along. */
  265. void
  266. append (char **charPP, char *fromP, unsigned long length)
  267. {
  268. /* Don't trust memcpy() of 0 chars. */
  269. if (length == 0)
  270. return;
  271. memcpy (*charPP, fromP, length);
  272. *charPP += length;
  273. }
  274. /* This routine records the largest alignment seen for each segment.
  275. If the beginning of the segment is aligned on the worst-case
  276. boundary, all of the other alignments within it will work. At
  277. least one object format really uses this info. */
  278. void
  279. record_alignment (/* Segment to which alignment pertains. */
  280. segT seg,
  281. /* Alignment, as a power of 2 (e.g., 1 => 2-byte
  282. boundary, 2 => 4-byte boundary, etc.) */
  283. unsigned int align)
  284. {
  285. if (seg == absolute_section)
  286. return;
  287. if (align > bfd_section_alignment (seg))
  288. bfd_set_section_alignment (seg, align);
  289. }
  290. int
  291. get_recorded_alignment (segT seg)
  292. {
  293. if (seg == absolute_section)
  294. return 0;
  295. return bfd_section_alignment (seg);
  296. }
  297. /* Reset the section indices after removing the gas created sections. */
  298. static void
  299. renumber_sections (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *countparg)
  300. {
  301. int *countp = (int *) countparg;
  302. sec->index = *countp;
  303. ++*countp;
  304. }
  305. static fragS *
  306. chain_frchains_together_1 (segT section, struct frchain *frchp)
  307. {
  308. fragS dummy, *prev_frag = &dummy;
  309. fixS fix_dummy, *prev_fix = &fix_dummy;
  310. for (; frchp; frchp = frchp->frch_next)
  311. {
  312. prev_frag->fr_next = frchp->frch_root;
  313. prev_frag = frchp->frch_last;
  314. gas_assert (prev_frag->fr_type != 0);
  315. if (frchp->fix_root != (fixS *) NULL)
  316. {
  317. if (seg_info (section)->fix_root == (fixS *) NULL)
  318. seg_info (section)->fix_root = frchp->fix_root;
  319. prev_fix->fx_next = frchp->fix_root;
  320. seg_info (section)->fix_tail = frchp->fix_tail;
  321. prev_fix = frchp->fix_tail;
  322. }
  323. }
  324. gas_assert (prev_frag != &dummy
  325. && prev_frag->fr_type != 0);
  326. prev_frag->fr_next = 0;
  327. return prev_frag;
  328. }
  329. static void
  330. chain_frchains_together (bfd *abfd ATTRIBUTE_UNUSED,
  331. segT section,
  332. void *xxx ATTRIBUTE_UNUSED)
  333. {
  334. segment_info_type *info;
  335. /* BFD may have introduced its own sections without using
  336. subseg_new, so it is possible that seg_info is NULL. */
  337. info = seg_info (section);
  338. if (info != (segment_info_type *) NULL)
  339. info->frchainP->frch_last
  340. = chain_frchains_together_1 (section, info->frchainP);
  341. /* Now that we've chained the frags together, we must add new fixups
  342. to the segment, not to the frag chain. */
  343. frags_chained = 1;
  344. }
  345. static void
  346. cvt_frag_to_fill (segT sec ATTRIBUTE_UNUSED, fragS *fragP)
  347. {
  348. switch (fragP->fr_type)
  349. {
  350. case rs_space_nop:
  351. goto skip_align;
  352. case rs_align:
  353. case rs_align_code:
  354. case rs_align_test:
  355. case rs_org:
  356. case rs_space:
  357. #ifdef HANDLE_ALIGN
  358. HANDLE_ALIGN (fragP);
  359. #endif
  360. skip_align:
  361. know (fragP->fr_next != NULL);
  362. fragP->fr_offset = (fragP->fr_next->fr_address
  363. - fragP->fr_address
  364. - fragP->fr_fix) / fragP->fr_var;
  365. if (fragP->fr_offset < 0)
  366. {
  367. as_bad_where (fragP->fr_file, fragP->fr_line,
  368. _("attempt to .org/.space/.nops backwards? (%ld)"),
  369. (long) fragP->fr_offset);
  370. fragP->fr_offset = 0;
  371. }
  372. if (fragP->fr_type == rs_space_nop)
  373. fragP->fr_type = rs_fill_nop;
  374. else
  375. fragP->fr_type = rs_fill;
  376. break;
  377. case rs_fill:
  378. case rs_fill_nop:
  379. break;
  380. case rs_leb128:
  381. {
  382. valueT value = S_GET_VALUE (fragP->fr_symbol);
  383. int size;
  384. if (!S_IS_DEFINED (fragP->fr_symbol))
  385. {
  386. as_bad_where (fragP->fr_file, fragP->fr_line,
  387. _("leb128 operand is an undefined symbol: %s"),
  388. S_GET_NAME (fragP->fr_symbol));
  389. }
  390. size = output_leb128 (fragP->fr_literal + fragP->fr_fix, value,
  391. fragP->fr_subtype);
  392. fragP->fr_fix += size;
  393. fragP->fr_type = rs_fill;
  394. fragP->fr_var = 0;
  395. fragP->fr_offset = 0;
  396. fragP->fr_symbol = NULL;
  397. }
  398. break;
  399. case rs_cfa:
  400. eh_frame_convert_frag (fragP);
  401. break;
  402. case rs_dwarf2dbg:
  403. dwarf2dbg_convert_frag (fragP);
  404. break;
  405. case rs_machine_dependent:
  406. md_convert_frag (stdoutput, sec, fragP);
  407. gas_assert (fragP->fr_next == NULL
  408. || (fragP->fr_next->fr_address - fragP->fr_address
  409. == fragP->fr_fix));
  410. /* After md_convert_frag, we make the frag into a ".space 0".
  411. md_convert_frag() should set up any fixSs and constants
  412. required. */
  413. frag_wane (fragP);
  414. break;
  415. #ifndef WORKING_DOT_WORD
  416. case rs_broken_word:
  417. {
  418. struct broken_word *lie;
  419. if (fragP->fr_subtype)
  420. {
  421. fragP->fr_fix += md_short_jump_size;
  422. for (lie = (struct broken_word *) (fragP->fr_symbol);
  423. lie && lie->dispfrag == fragP;
  424. lie = lie->next_broken_word)
  425. if (lie->added == 1)
  426. fragP->fr_fix += md_long_jump_size;
  427. }
  428. frag_wane (fragP);
  429. }
  430. break;
  431. #endif
  432. default:
  433. BAD_CASE (fragP->fr_type);
  434. break;
  435. }
  436. #ifdef md_frag_check
  437. md_frag_check (fragP);
  438. #endif
  439. }
  440. struct relax_seg_info
  441. {
  442. int pass;
  443. int changed;
  444. };
  445. static void
  446. relax_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx)
  447. {
  448. segment_info_type *seginfo = seg_info (sec);
  449. struct relax_seg_info *info = (struct relax_seg_info *) xxx;
  450. if (seginfo && seginfo->frchainP
  451. && relax_segment (seginfo->frchainP->frch_root, sec, info->pass))
  452. info->changed = 1;
  453. }
  454. static void
  455. size_seg (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, void *xxx ATTRIBUTE_UNUSED)
  456. {
  457. flagword flags;
  458. fragS *fragp;
  459. segment_info_type *seginfo;
  460. int x;
  461. valueT size, newsize;
  462. subseg_change (sec, 0);
  463. seginfo = seg_info (sec);
  464. if (seginfo && seginfo->frchainP)
  465. {
  466. for (fragp = seginfo->frchainP->frch_root; fragp; fragp = fragp->fr_next)
  467. cvt_frag_to_fill (sec, fragp);
  468. for (fragp = seginfo->frchainP->frch_root;
  469. fragp->fr_next;
  470. fragp = fragp->fr_next)
  471. /* Walk to last elt. */
  472. ;
  473. size = fragp->fr_address + fragp->fr_fix;
  474. }
  475. else
  476. size = 0;
  477. flags = bfd_section_flags (sec);
  478. if (size == 0 && bfd_section_size (sec) != 0 &&
  479. (flags & SEC_HAS_CONTENTS) != 0)
  480. return;
  481. if (size > 0 && ! seginfo->bss)
  482. flags |= SEC_HAS_CONTENTS;
  483. flags &= ~SEC_RELOC;
  484. x = bfd_set_section_flags (sec, flags);
  485. gas_assert (x);
  486. /* If permitted, allow the backend to pad out the section
  487. to some alignment boundary. */
  488. if (do_not_pad_sections_to_alignment)
  489. newsize = size;
  490. else
  491. newsize = md_section_align (sec, size);
  492. x = bfd_set_section_size (sec, newsize);
  493. gas_assert (x);
  494. /* If the size had to be rounded up, add some padding in the last
  495. non-empty frag. */
  496. gas_assert (newsize >= size);
  497. if (size != newsize)
  498. {
  499. fragS *last = seginfo->frchainP->frch_last;
  500. fragp = seginfo->frchainP->frch_root;
  501. while (fragp->fr_next != last)
  502. fragp = fragp->fr_next;
  503. last->fr_address = size;
  504. if ((newsize - size) % fragp->fr_var == 0)
  505. fragp->fr_offset += (newsize - size) / fragp->fr_var;
  506. else
  507. /* If we hit this abort, it's likely due to subsegs_finish not
  508. providing sufficient alignment on the last frag, and the
  509. machine dependent code using alignment frags with fr_var
  510. greater than 1. */
  511. abort ();
  512. }
  513. #ifdef tc_frob_section
  514. tc_frob_section (sec);
  515. #endif
  516. #ifdef obj_frob_section
  517. obj_frob_section (sec);
  518. #endif
  519. }
  520. #ifdef DEBUG2
  521. static void
  522. dump_section_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec, FILE *stream)
  523. {
  524. segment_info_type *seginfo = seg_info (sec);
  525. fixS *fixp = seginfo->fix_root;
  526. if (!fixp)
  527. return;
  528. fprintf (stream, "sec %s relocs:\n", sec->name);
  529. while (fixp)
  530. {
  531. symbolS *s = fixp->fx_addsy;
  532. fprintf (stream, " %08lx: type %d ", (unsigned long) fixp,
  533. (int) fixp->fx_r_type);
  534. if (s == NULL)
  535. fprintf (stream, "no sym\n");
  536. else
  537. {
  538. print_symbol_value_1 (stream, s);
  539. fprintf (stream, "\n");
  540. }
  541. fixp = fixp->fx_next;
  542. }
  543. }
  544. #else
  545. #define dump_section_relocs(ABFD,SEC,STREAM) ((void) 0)
  546. #endif
  547. #ifndef EMIT_SECTION_SYMBOLS
  548. #define EMIT_SECTION_SYMBOLS 1
  549. #endif
  550. /* Resolve U.A.OFFSET_SYM and U.A.SYM fields of RELOC_LIST entries,
  551. and check for validity. Convert RELOC_LIST from using U.A fields
  552. to U.B fields. */
  553. static void
  554. resolve_reloc_expr_symbols (void)
  555. {
  556. bfd_vma addr_mask = 1;
  557. struct reloc_list *r;
  558. /* Avoid a shift by the width of type. */
  559. addr_mask <<= bfd_arch_bits_per_address (stdoutput) - 1;
  560. addr_mask <<= 1;
  561. addr_mask -= 1;
  562. for (r = reloc_list; r; r = r->next)
  563. {
  564. reloc_howto_type *howto = r->u.a.howto;
  565. expressionS *symval;
  566. symbolS *sym;
  567. bfd_vma offset, addend;
  568. asection *sec;
  569. resolve_symbol_value (r->u.a.offset_sym);
  570. symval = symbol_get_value_expression (r->u.a.offset_sym);
  571. offset = 0;
  572. sym = NULL;
  573. if (symval->X_op == O_constant)
  574. sym = r->u.a.offset_sym;
  575. else if (symval->X_op == O_symbol)
  576. {
  577. sym = symval->X_add_symbol;
  578. offset = symval->X_add_number;
  579. symval = symbol_get_value_expression (symval->X_add_symbol);
  580. }
  581. if (sym == NULL
  582. || symval->X_op != O_constant
  583. || (sec = S_GET_SEGMENT (sym)) == NULL
  584. || !SEG_NORMAL (sec))
  585. {
  586. as_bad_where (r->file, r->line, _("invalid offset expression"));
  587. sec = NULL;
  588. }
  589. else
  590. offset += S_GET_VALUE (sym);
  591. sym = NULL;
  592. addend = r->u.a.addend;
  593. if (r->u.a.sym != NULL)
  594. {
  595. resolve_symbol_value (r->u.a.sym);
  596. symval = symbol_get_value_expression (r->u.a.sym);
  597. if (symval->X_op == O_constant)
  598. sym = r->u.a.sym;
  599. else if (symval->X_op == O_symbol)
  600. {
  601. sym = symval->X_add_symbol;
  602. addend += symval->X_add_number;
  603. symval = symbol_get_value_expression (symval->X_add_symbol);
  604. }
  605. if (symval->X_op != O_constant)
  606. {
  607. as_bad_where (r->file, r->line, _("invalid reloc expression"));
  608. sec = NULL;
  609. }
  610. else if (sym != NULL && sec != NULL)
  611. {
  612. /* Convert relocs against local symbols to refer to the
  613. corresponding section symbol plus offset instead. Keep
  614. PC-relative relocs of the REL variety intact though to
  615. prevent the offset from overflowing the relocated field,
  616. unless it has enough bits to cover the whole address
  617. space. */
  618. if (S_IS_LOCAL (sym)
  619. && S_IS_DEFINED (sym)
  620. && !symbol_section_p (sym)
  621. && (sec->use_rela_p
  622. || (howto->partial_inplace
  623. && (!howto->pc_relative
  624. || howto->src_mask == addr_mask))))
  625. {
  626. asection *symsec = S_GET_SEGMENT (sym);
  627. if (!(((symsec->flags & SEC_MERGE) != 0
  628. && addend != 0)
  629. || (symsec->flags & SEC_THREAD_LOCAL) != 0))
  630. {
  631. addend += S_GET_VALUE (sym);
  632. sym = section_symbol (symsec);
  633. }
  634. }
  635. symbol_mark_used_in_reloc (sym);
  636. }
  637. }
  638. if (sym == NULL)
  639. {
  640. if (abs_section_sym == NULL)
  641. abs_section_sym = section_symbol (absolute_section);
  642. sym = abs_section_sym;
  643. }
  644. r->u.b.sec = sec;
  645. r->u.b.s = symbol_get_bfdsym (sym);
  646. r->u.b.r.sym_ptr_ptr = &r->u.b.s;
  647. r->u.b.r.address = offset;
  648. r->u.b.r.addend = addend;
  649. r->u.b.r.howto = howto;
  650. }
  651. }
  652. /* This pass over fixups decides whether symbols can be replaced with
  653. section symbols. */
  654. static void
  655. adjust_reloc_syms (bfd *abfd ATTRIBUTE_UNUSED,
  656. asection *sec,
  657. void *xxx ATTRIBUTE_UNUSED)
  658. {
  659. segment_info_type *seginfo = seg_info (sec);
  660. fixS *fixp;
  661. if (seginfo == NULL)
  662. return;
  663. dump_section_relocs (abfd, sec, stderr);
  664. for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
  665. if (fixp->fx_done)
  666. /* Ignore it. */
  667. ;
  668. else if (fixp->fx_addsy)
  669. {
  670. symbolS *sym;
  671. asection *symsec;
  672. #ifdef DEBUG5
  673. fprintf (stderr, "\n\nadjusting fixup:\n");
  674. print_fixup (fixp);
  675. #endif
  676. sym = fixp->fx_addsy;
  677. /* All symbols should have already been resolved at this
  678. point. It is possible to see unresolved expression
  679. symbols, though, since they are not in the regular symbol
  680. table. */
  681. resolve_symbol_value (sym);
  682. if (fixp->fx_subsy != NULL)
  683. resolve_symbol_value (fixp->fx_subsy);
  684. /* If this symbol is equated to an undefined or common symbol,
  685. convert the fixup to being against that symbol. */
  686. while (symbol_equated_reloc_p (sym)
  687. || S_IS_WEAKREFR (sym))
  688. {
  689. symbolS *newsym = symbol_get_value_expression (sym)->X_add_symbol;
  690. if (sym == newsym)
  691. break;
  692. fixp->fx_offset += symbol_get_value_expression (sym)->X_add_number;
  693. fixp->fx_addsy = newsym;
  694. sym = newsym;
  695. }
  696. if (symbol_mri_common_p (sym))
  697. {
  698. fixp->fx_offset += S_GET_VALUE (sym);
  699. fixp->fx_addsy = symbol_get_value_expression (sym)->X_add_symbol;
  700. continue;
  701. }
  702. /* If the symbol is undefined, common, weak, or global (ELF
  703. shared libs), we can't replace it with the section symbol. */
  704. if (S_FORCE_RELOC (fixp->fx_addsy, 1))
  705. continue;
  706. /* Is there some other (target cpu dependent) reason we can't adjust
  707. this one? (E.g. relocations involving function addresses on
  708. the PA. */
  709. #ifdef tc_fix_adjustable
  710. if (! tc_fix_adjustable (fixp))
  711. continue;
  712. #endif
  713. /* Since we're reducing to section symbols, don't attempt to reduce
  714. anything that's already using one. */
  715. if (symbol_section_p (sym))
  716. {
  717. /* Mark the section symbol used in relocation so that it will
  718. be included in the symbol table. */
  719. symbol_mark_used_in_reloc (sym);
  720. continue;
  721. }
  722. symsec = S_GET_SEGMENT (sym);
  723. if (symsec == NULL)
  724. abort ();
  725. if (bfd_is_abs_section (symsec)
  726. || symsec == reg_section)
  727. {
  728. /* The fixup_segment routine normally will not use this
  729. symbol in a relocation. */
  730. continue;
  731. }
  732. /* Don't try to reduce relocs which refer to non-local symbols
  733. in .linkonce sections. It can lead to confusion when a
  734. debugging section refers to a .linkonce section. I hope
  735. this will always be correct. */
  736. if (symsec != sec && ! S_IS_LOCAL (sym))
  737. {
  738. if ((symsec->flags & SEC_LINK_ONCE) != 0
  739. || (IS_ELF
  740. /* The GNU toolchain uses an extension for ELF: a
  741. section beginning with the magic string
  742. .gnu.linkonce is a linkonce section. */
  743. && startswith (segment_name (symsec), ".gnu.linkonce")))
  744. continue;
  745. }
  746. /* Never adjust a reloc against local symbol in a merge section
  747. with non-zero addend. */
  748. if ((symsec->flags & SEC_MERGE) != 0
  749. && (fixp->fx_offset != 0 || fixp->fx_subsy != NULL))
  750. continue;
  751. /* Never adjust a reloc against TLS local symbol. */
  752. if ((symsec->flags & SEC_THREAD_LOCAL) != 0)
  753. continue;
  754. /* We refetch the segment when calling section_symbol, rather
  755. than using symsec, because S_GET_VALUE may wind up changing
  756. the section when it calls resolve_symbol_value. */
  757. fixp->fx_offset += S_GET_VALUE (sym);
  758. fixp->fx_addsy = section_symbol (S_GET_SEGMENT (sym));
  759. #ifdef DEBUG5
  760. fprintf (stderr, "\nadjusted fixup:\n");
  761. print_fixup (fixp);
  762. #endif
  763. }
  764. dump_section_relocs (abfd, sec, stderr);
  765. }
  766. void
  767. as_bad_subtract (fixS *fixp)
  768. {
  769. as_bad_where (fixp->fx_file, fixp->fx_line,
  770. _("can't resolve %s - %s"),
  771. fixp->fx_addsy ? S_GET_NAME (fixp->fx_addsy) : "0",
  772. S_GET_NAME (fixp->fx_subsy));
  773. }
  774. /* fixup_segment()
  775. Go through all the fixS's in a segment and see which ones can be
  776. handled now. (These consist of fixS where we have since discovered
  777. the value of a symbol, or the address of the frag involved.)
  778. For each one, call md_apply_fix to put the fix into the frag data.
  779. Ones that we couldn't completely handle here will be output later
  780. by emit_relocations. */
  781. static void
  782. fixup_segment (fixS *fixP, segT this_segment)
  783. {
  784. valueT add_number;
  785. fragS *fragP;
  786. if (fixP != NULL && abs_section_sym == NULL)
  787. abs_section_sym = section_symbol (absolute_section);
  788. /* If the linker is doing the relaxing, we must not do any fixups.
  789. Well, strictly speaking that's not true -- we could do any that
  790. are PC-relative and don't cross regions that could change size. */
  791. if (linkrelax && TC_LINKRELAX_FIXUP (this_segment))
  792. {
  793. for (; fixP; fixP = fixP->fx_next)
  794. if (!fixP->fx_done)
  795. {
  796. if (fixP->fx_addsy == NULL)
  797. {
  798. /* There was no symbol required by this relocation.
  799. However, BFD doesn't really handle relocations
  800. without symbols well. So fake up a local symbol in
  801. the absolute section. */
  802. fixP->fx_addsy = abs_section_sym;
  803. }
  804. symbol_mark_used_in_reloc (fixP->fx_addsy);
  805. if (fixP->fx_subsy != NULL)
  806. symbol_mark_used_in_reloc (fixP->fx_subsy);
  807. }
  808. return;
  809. }
  810. for (; fixP; fixP = fixP->fx_next)
  811. {
  812. segT add_symbol_segment = absolute_section;
  813. #ifdef DEBUG5
  814. fprintf (stderr, "\nprocessing fixup:\n");
  815. print_fixup (fixP);
  816. #endif
  817. fragP = fixP->fx_frag;
  818. know (fragP);
  819. #ifdef TC_VALIDATE_FIX
  820. TC_VALIDATE_FIX (fixP, this_segment, skip);
  821. #endif
  822. add_number = fixP->fx_offset;
  823. if (fixP->fx_addsy != NULL)
  824. add_symbol_segment = S_GET_SEGMENT (fixP->fx_addsy);
  825. if (fixP->fx_subsy != NULL)
  826. {
  827. segT sub_symbol_segment;
  828. resolve_symbol_value (fixP->fx_subsy);
  829. sub_symbol_segment = S_GET_SEGMENT (fixP->fx_subsy);
  830. if (fixP->fx_addsy != NULL
  831. && sub_symbol_segment == add_symbol_segment
  832. && !S_FORCE_RELOC (fixP->fx_addsy, 0)
  833. && !S_FORCE_RELOC (fixP->fx_subsy, 0)
  834. && !TC_FORCE_RELOCATION_SUB_SAME (fixP, add_symbol_segment))
  835. {
  836. add_number += S_GET_VALUE (fixP->fx_addsy);
  837. add_number -= S_GET_VALUE (fixP->fx_subsy);
  838. fixP->fx_offset = add_number;
  839. fixP->fx_addsy = NULL;
  840. fixP->fx_subsy = NULL;
  841. #ifdef TC_M68K
  842. /* See the comment below about 68k weirdness. */
  843. fixP->fx_pcrel = 0;
  844. #endif
  845. }
  846. else if (sub_symbol_segment == absolute_section
  847. && !S_FORCE_RELOC (fixP->fx_subsy, 0)
  848. && !TC_FORCE_RELOCATION_SUB_ABS (fixP, add_symbol_segment))
  849. {
  850. add_number -= S_GET_VALUE (fixP->fx_subsy);
  851. fixP->fx_offset = add_number;
  852. fixP->fx_subsy = NULL;
  853. }
  854. else if (sub_symbol_segment == this_segment
  855. && !S_FORCE_RELOC (fixP->fx_subsy, 0)
  856. && !TC_FORCE_RELOCATION_SUB_LOCAL (fixP, add_symbol_segment))
  857. {
  858. add_number -= S_GET_VALUE (fixP->fx_subsy);
  859. fixP->fx_offset = (add_number + fixP->fx_dot_value
  860. + fixP->fx_dot_frag->fr_address);
  861. /* Make it pc-relative. If the back-end code has not
  862. selected a pc-relative reloc, cancel the adjustment
  863. we do later on all pc-relative relocs. */
  864. if (0
  865. #ifdef TC_M68K
  866. /* Do this for m68k even if it's already described
  867. as pc-relative. On the m68k, an operand of
  868. "pc@(foo-.-2)" should address "foo" in a
  869. pc-relative mode. */
  870. || 1
  871. #endif
  872. || !fixP->fx_pcrel)
  873. add_number += MD_PCREL_FROM_SECTION (fixP, this_segment);
  874. fixP->fx_subsy = NULL;
  875. fixP->fx_pcrel = 1;
  876. }
  877. else if (!TC_VALIDATE_FIX_SUB (fixP, add_symbol_segment))
  878. {
  879. if (!md_register_arithmetic
  880. && (add_symbol_segment == reg_section
  881. || sub_symbol_segment == reg_section))
  882. as_bad_where (fixP->fx_file, fixP->fx_line,
  883. _("register value used as expression"));
  884. else
  885. as_bad_subtract (fixP);
  886. }
  887. else if (sub_symbol_segment != undefined_section
  888. && ! bfd_is_com_section (sub_symbol_segment)
  889. && MD_APPLY_SYM_VALUE (fixP))
  890. add_number -= S_GET_VALUE (fixP->fx_subsy);
  891. }
  892. if (fixP->fx_addsy)
  893. {
  894. if (add_symbol_segment == this_segment
  895. && !S_FORCE_RELOC (fixP->fx_addsy, 0)
  896. && !TC_FORCE_RELOCATION_LOCAL (fixP))
  897. {
  898. /* This fixup was made when the symbol's segment was
  899. SEG_UNKNOWN, but it is now in the local segment.
  900. So we know how to do the address without relocation. */
  901. add_number += S_GET_VALUE (fixP->fx_addsy);
  902. fixP->fx_offset = add_number;
  903. if (fixP->fx_pcrel)
  904. add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
  905. fixP->fx_addsy = NULL;
  906. fixP->fx_pcrel = 0;
  907. }
  908. else if (add_symbol_segment == absolute_section
  909. && !S_FORCE_RELOC (fixP->fx_addsy, 0)
  910. && !TC_FORCE_RELOCATION_ABS (fixP))
  911. {
  912. add_number += S_GET_VALUE (fixP->fx_addsy);
  913. fixP->fx_offset = add_number;
  914. fixP->fx_addsy = NULL;
  915. }
  916. else if (add_symbol_segment != undefined_section
  917. && ! bfd_is_com_section (add_symbol_segment)
  918. && MD_APPLY_SYM_VALUE (fixP))
  919. add_number += S_GET_VALUE (fixP->fx_addsy);
  920. }
  921. if (fixP->fx_pcrel)
  922. {
  923. add_number -= MD_PCREL_FROM_SECTION (fixP, this_segment);
  924. if (!fixP->fx_done && fixP->fx_addsy == NULL)
  925. {
  926. /* There was no symbol required by this relocation.
  927. However, BFD doesn't really handle relocations
  928. without symbols well. So fake up a local symbol in
  929. the absolute section. */
  930. fixP->fx_addsy = abs_section_sym;
  931. }
  932. }
  933. if (!fixP->fx_done)
  934. md_apply_fix (fixP, &add_number, this_segment);
  935. if (!fixP->fx_done)
  936. {
  937. if (fixP->fx_addsy == NULL)
  938. fixP->fx_addsy = abs_section_sym;
  939. symbol_mark_used_in_reloc (fixP->fx_addsy);
  940. if (fixP->fx_subsy != NULL)
  941. symbol_mark_used_in_reloc (fixP->fx_subsy);
  942. }
  943. if (!fixP->fx_no_overflow && fixP->fx_size != 0)
  944. {
  945. if (fixP->fx_size < sizeof (valueT))
  946. {
  947. valueT mask;
  948. mask = 0;
  949. mask--; /* Set all bits to one. */
  950. mask <<= fixP->fx_size * 8 - (fixP->fx_signed ? 1 : 0);
  951. if ((add_number & mask) != 0
  952. && (fixP->fx_signed
  953. ? (add_number & mask) != mask
  954. : (-add_number & mask) != 0))
  955. {
  956. char buf[50], buf2[50];
  957. bfd_sprintf_vma (stdoutput, buf, fragP->fr_address + fixP->fx_where);
  958. if (add_number > 1000)
  959. bfd_sprintf_vma (stdoutput, buf2, add_number);
  960. else
  961. sprintf (buf2, "%ld", (long) add_number);
  962. as_bad_where (fixP->fx_file, fixP->fx_line,
  963. ngettext ("value of %s too large for field "
  964. "of %d byte at %s",
  965. "value of %s too large for field "
  966. "of %d bytes at %s",
  967. fixP->fx_size),
  968. buf2, fixP->fx_size, buf);
  969. } /* Generic error checking. */
  970. }
  971. #ifdef WARN_SIGNED_OVERFLOW_WORD
  972. /* Warn if a .word value is too large when treated as a signed
  973. number. We already know it is not too negative. This is to
  974. catch over-large switches generated by gcc on the 68k. */
  975. if (!flag_signed_overflow_ok
  976. && fixP->fx_size == 2
  977. && add_number > 0x7fff)
  978. as_bad_where (fixP->fx_file, fixP->fx_line,
  979. _("signed .word overflow; switch may be too large; %ld at 0x%lx"),
  980. (long) add_number,
  981. (long) (fragP->fr_address + fixP->fx_where));
  982. #endif
  983. }
  984. #ifdef TC_VALIDATE_FIX
  985. skip: ATTRIBUTE_UNUSED_LABEL
  986. ;
  987. #endif
  988. #ifdef DEBUG5
  989. fprintf (stderr, "result:\n");
  990. print_fixup (fixP);
  991. #endif
  992. } /* For each fixS in this segment. */
  993. }
  994. static void
  995. fix_segment (bfd *abfd ATTRIBUTE_UNUSED,
  996. asection *sec,
  997. void *xxx ATTRIBUTE_UNUSED)
  998. {
  999. segment_info_type *seginfo = seg_info (sec);
  1000. fixup_segment (seginfo->fix_root, sec);
  1001. }
  1002. static void
  1003. install_reloc (asection *sec, arelent *reloc, fragS *fragp,
  1004. const char *file, unsigned int line)
  1005. {
  1006. char *err;
  1007. bfd_reloc_status_type s;
  1008. asymbol *sym;
  1009. if (reloc->sym_ptr_ptr != NULL
  1010. && (sym = *reloc->sym_ptr_ptr) != NULL
  1011. && (sym->flags & BSF_KEEP) == 0
  1012. && ((sym->flags & BSF_SECTION_SYM) == 0
  1013. || (EMIT_SECTION_SYMBOLS
  1014. && !bfd_is_abs_section (sym->section))))
  1015. as_bad_where (file, line, _("redefined symbol cannot be used on reloc"));
  1016. s = bfd_install_relocation (stdoutput, reloc,
  1017. fragp->fr_literal, fragp->fr_address,
  1018. sec, &err);
  1019. switch (s)
  1020. {
  1021. case bfd_reloc_ok:
  1022. break;
  1023. case bfd_reloc_overflow:
  1024. as_bad_where (file, line, _("relocation overflow"));
  1025. break;
  1026. case bfd_reloc_outofrange:
  1027. as_bad_where (file, line, _("relocation out of range"));
  1028. break;
  1029. default:
  1030. as_fatal (_("%s:%u: bad return from bfd_install_relocation: %x"),
  1031. file, line, s);
  1032. }
  1033. }
  1034. static fragS *
  1035. get_frag_for_reloc (fragS *last_frag,
  1036. const segment_info_type *seginfo,
  1037. const struct reloc_list *r)
  1038. {
  1039. fragS *f;
  1040. for (f = last_frag; f != NULL; f = f->fr_next)
  1041. if (f->fr_address <= r->u.b.r.address
  1042. && r->u.b.r.address < f->fr_address + f->fr_fix)
  1043. return f;
  1044. for (f = seginfo->frchainP->frch_root; f != NULL; f = f->fr_next)
  1045. if (f->fr_address <= r->u.b.r.address
  1046. && r->u.b.r.address < f->fr_address + f->fr_fix)
  1047. return f;
  1048. for (f = seginfo->frchainP->frch_root; f != NULL; f = f->fr_next)
  1049. if (f->fr_address <= r->u.b.r.address
  1050. && r->u.b.r.address <= f->fr_address + f->fr_fix)
  1051. return f;
  1052. as_bad_where (r->file, r->line,
  1053. _("reloc not within (fixed part of) section"));
  1054. return NULL;
  1055. }
  1056. static void
  1057. write_relocs (bfd *abfd ATTRIBUTE_UNUSED, asection *sec,
  1058. void *xxx ATTRIBUTE_UNUSED)
  1059. {
  1060. segment_info_type *seginfo = seg_info (sec);
  1061. unsigned int n;
  1062. struct reloc_list *my_reloc_list, **rp, *r;
  1063. arelent **relocs;
  1064. fixS *fixp;
  1065. fragS *last_frag;
  1066. /* If seginfo is NULL, we did not create this section; don't do
  1067. anything with it. */
  1068. if (seginfo == NULL)
  1069. return;
  1070. n = 0;
  1071. for (fixp = seginfo->fix_root; fixp; fixp = fixp->fx_next)
  1072. if (!fixp->fx_done)
  1073. n++;
  1074. #ifdef RELOC_EXPANSION_POSSIBLE
  1075. n *= MAX_RELOC_EXPANSION;
  1076. #endif
  1077. /* Extract relocs for this section from reloc_list. */
  1078. rp = &reloc_list;
  1079. my_reloc_list = NULL;
  1080. while ((r = *rp) != NULL)
  1081. {
  1082. if (r->u.b.sec == sec)
  1083. {
  1084. *rp = r->next;
  1085. r->next = my_reloc_list;
  1086. my_reloc_list = r;
  1087. n++;
  1088. }
  1089. else
  1090. rp = &r->next;
  1091. }
  1092. relocs = XCNEWVEC (arelent *, n);
  1093. n = 0;
  1094. r = my_reloc_list;
  1095. last_frag = NULL;
  1096. for (fixp = seginfo->fix_root; fixp != (fixS *) NULL; fixp = fixp->fx_next)
  1097. {
  1098. int fx_size, slack;
  1099. valueT loc;
  1100. arelent **reloc;
  1101. #ifndef RELOC_EXPANSION_POSSIBLE
  1102. arelent *rel;
  1103. reloc = &rel;
  1104. #endif
  1105. if (fixp->fx_done)
  1106. continue;
  1107. fx_size = fixp->fx_size;
  1108. slack = TC_FX_SIZE_SLACK (fixp);
  1109. if (slack > 0)
  1110. fx_size = fx_size > slack ? fx_size - slack : 0;
  1111. loc = fixp->fx_where + fx_size;
  1112. if (slack >= 0 && loc > fixp->fx_frag->fr_fix)
  1113. as_bad_where (fixp->fx_file, fixp->fx_line,
  1114. _("internal error: fixup not contained within frag"));
  1115. #ifdef obj_fixup_removed_symbol
  1116. if (fixp->fx_addsy && symbol_removed_p (fixp->fx_addsy))
  1117. obj_fixup_removed_symbol (&fixp->fx_addsy);
  1118. if (fixp->fx_subsy && symbol_removed_p (fixp->fx_subsy))
  1119. obj_fixup_removed_symbol (&fixp->fx_subsy);
  1120. #endif
  1121. #ifndef RELOC_EXPANSION_POSSIBLE
  1122. *reloc = tc_gen_reloc (sec, fixp);
  1123. #else
  1124. reloc = tc_gen_reloc (sec, fixp);
  1125. #endif
  1126. while (*reloc)
  1127. {
  1128. while (r != NULL && r->u.b.r.address < (*reloc)->address)
  1129. {
  1130. fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
  1131. if (f != NULL)
  1132. {
  1133. last_frag = f;
  1134. relocs[n++] = &r->u.b.r;
  1135. install_reloc (sec, &r->u.b.r, f, r->file, r->line);
  1136. }
  1137. r = r->next;
  1138. }
  1139. #ifdef GAS_SORT_RELOCS
  1140. if (n != 0 && (*reloc)->address < relocs[n - 1]->address)
  1141. {
  1142. size_t lo = 0;
  1143. size_t hi = n - 1;
  1144. bfd_vma look = (*reloc)->address;
  1145. while (lo < hi)
  1146. {
  1147. size_t mid = (lo + hi) / 2;
  1148. if (relocs[mid]->address > look)
  1149. hi = mid;
  1150. else
  1151. {
  1152. lo = mid + 1;
  1153. if (relocs[mid]->address == look)
  1154. break;
  1155. }
  1156. }
  1157. while (lo < hi && relocs[lo]->address == look)
  1158. lo++;
  1159. memmove (relocs + lo + 1, relocs + lo,
  1160. (n - lo) * sizeof (*relocs));
  1161. n++;
  1162. relocs[lo] = *reloc;
  1163. }
  1164. else
  1165. #endif
  1166. relocs[n++] = *reloc;
  1167. install_reloc (sec, *reloc, fixp->fx_frag,
  1168. fixp->fx_file, fixp->fx_line);
  1169. #ifndef RELOC_EXPANSION_POSSIBLE
  1170. break;
  1171. #else
  1172. reloc++;
  1173. #endif
  1174. }
  1175. }
  1176. while (r != NULL)
  1177. {
  1178. fragS *f = get_frag_for_reloc (last_frag, seginfo, r);
  1179. if (f != NULL)
  1180. {
  1181. last_frag = f;
  1182. relocs[n++] = &r->u.b.r;
  1183. install_reloc (sec, &r->u.b.r, f, r->file, r->line);
  1184. }
  1185. r = r->next;
  1186. }
  1187. #ifdef DEBUG4
  1188. {
  1189. unsigned int k, j, nsyms;
  1190. asymbol **sympp;
  1191. sympp = bfd_get_outsymbols (stdoutput);
  1192. nsyms = bfd_get_symcount (stdoutput);
  1193. for (k = 0; k < n; k++)
  1194. if (((*relocs[k]->sym_ptr_ptr)->flags & BSF_SECTION_SYM) == 0)
  1195. {
  1196. for (j = 0; j < nsyms; j++)
  1197. if (sympp[j] == *relocs[k]->sym_ptr_ptr)
  1198. break;
  1199. if (j == nsyms)
  1200. abort ();
  1201. }
  1202. }
  1203. #endif
  1204. if (n)
  1205. {
  1206. flagword flags = bfd_section_flags (sec);
  1207. flags |= SEC_RELOC;
  1208. bfd_set_section_flags (sec, flags);
  1209. bfd_set_reloc (stdoutput, sec, relocs, n);
  1210. }
  1211. #ifdef SET_SECTION_RELOCS
  1212. SET_SECTION_RELOCS (sec, relocs, n);
  1213. #endif
  1214. #ifdef DEBUG3
  1215. {
  1216. unsigned int k;
  1217. fprintf (stderr, "relocs for sec %s\n", sec->name);
  1218. for (k = 0; k < n; k++)
  1219. {
  1220. arelent *rel = relocs[k];
  1221. asymbol *s = *rel->sym_ptr_ptr;
  1222. fprintf (stderr, " reloc %2d @%p off %4lx : sym %-10s addend %lx\n",
  1223. k, rel, (unsigned long)rel->address, s->name,
  1224. (unsigned long)rel->addend);
  1225. }
  1226. }
  1227. #endif
  1228. }
  1229. static int
  1230. compress_frag (struct z_stream_s *strm, const char *contents, int in_size,
  1231. fragS **last_newf, struct obstack *ob)
  1232. {
  1233. int out_size;
  1234. int total_out_size = 0;
  1235. fragS *f = *last_newf;
  1236. char *next_out;
  1237. int avail_out;
  1238. /* Call the compression routine repeatedly until it has finished
  1239. processing the frag. */
  1240. while (in_size > 0)
  1241. {
  1242. /* Reserve all the space available in the current chunk.
  1243. If none is available, start a new frag. */
  1244. avail_out = obstack_room (ob);
  1245. if (avail_out <= 0)
  1246. {
  1247. obstack_finish (ob);
  1248. f = frag_alloc (ob);
  1249. f->fr_type = rs_fill;
  1250. (*last_newf)->fr_next = f;
  1251. *last_newf = f;
  1252. avail_out = obstack_room (ob);
  1253. }
  1254. if (avail_out <= 0)
  1255. as_fatal (_("can't extend frag"));
  1256. next_out = obstack_next_free (ob);
  1257. obstack_blank_fast (ob, avail_out);
  1258. out_size = compress_data (strm, &contents, &in_size,
  1259. &next_out, &avail_out);
  1260. if (out_size < 0)
  1261. return -1;
  1262. f->fr_fix += out_size;
  1263. total_out_size += out_size;
  1264. /* Return unused space. */
  1265. if (avail_out > 0)
  1266. obstack_blank_fast (ob, -avail_out);
  1267. }
  1268. return total_out_size;
  1269. }
  1270. static void
  1271. compress_debug (bfd *abfd, asection *sec, void *xxx ATTRIBUTE_UNUSED)
  1272. {
  1273. segment_info_type *seginfo = seg_info (sec);
  1274. fragS *f;
  1275. fragS *first_newf;
  1276. fragS *last_newf;
  1277. struct obstack *ob = &seginfo->frchainP->frch_obstack;
  1278. bfd_size_type uncompressed_size = (bfd_size_type) sec->size;
  1279. bfd_size_type compressed_size;
  1280. const char *section_name;
  1281. char *compressed_name;
  1282. char *header;
  1283. struct z_stream_s *strm;
  1284. int x;
  1285. flagword flags = bfd_section_flags (sec);
  1286. unsigned int header_size, compression_header_size;
  1287. if (seginfo == NULL
  1288. || sec->size < 32
  1289. || (flags & (SEC_ALLOC | SEC_HAS_CONTENTS)) == SEC_ALLOC)
  1290. return;
  1291. section_name = bfd_section_name (sec);
  1292. if (!startswith (section_name, ".debug_"))
  1293. return;
  1294. strm = compress_init ();
  1295. if (strm == NULL)
  1296. return;
  1297. if (flag_compress_debug == COMPRESS_DEBUG_GABI_ZLIB)
  1298. {
  1299. compression_header_size
  1300. = bfd_get_compression_header_size (stdoutput, NULL);
  1301. header_size = compression_header_size;
  1302. }
  1303. else
  1304. {
  1305. compression_header_size = 0;
  1306. header_size = 12;
  1307. }
  1308. /* Create a new frag to contain the compression header. */
  1309. first_newf = frag_alloc (ob);
  1310. if (obstack_room (ob) < header_size)
  1311. first_newf = frag_alloc (ob);
  1312. if (obstack_room (ob) < header_size)
  1313. as_fatal (ngettext ("can't extend frag %lu char",
  1314. "can't extend frag %lu chars",
  1315. (unsigned long) header_size),
  1316. (unsigned long) header_size);
  1317. last_newf = first_newf;
  1318. obstack_blank_fast (ob, header_size);
  1319. last_newf->fr_type = rs_fill;
  1320. last_newf->fr_fix = header_size;
  1321. header = last_newf->fr_literal;
  1322. compressed_size = header_size;
  1323. /* Stream the frags through the compression engine, adding new frags
  1324. as necessary to accommodate the compressed output. */
  1325. for (f = seginfo->frchainP->frch_root;
  1326. f;
  1327. f = f->fr_next)
  1328. {
  1329. offsetT fill_size;
  1330. char *fill_literal;
  1331. offsetT count;
  1332. int out_size;
  1333. gas_assert (f->fr_type == rs_fill);
  1334. if (f->fr_fix)
  1335. {
  1336. out_size = compress_frag (strm, f->fr_literal, f->fr_fix,
  1337. &last_newf, ob);
  1338. if (out_size < 0)
  1339. return;
  1340. compressed_size += out_size;
  1341. }
  1342. fill_literal = f->fr_literal + f->fr_fix;
  1343. fill_size = f->fr_var;
  1344. count = f->fr_offset;
  1345. gas_assert (count >= 0);
  1346. if (fill_size && count)
  1347. {
  1348. while (count--)
  1349. {
  1350. out_size = compress_frag (strm, fill_literal, (int) fill_size,
  1351. &last_newf, ob);
  1352. if (out_size < 0)
  1353. return;
  1354. compressed_size += out_size;
  1355. }
  1356. }
  1357. }
  1358. /* Flush the compression state. */
  1359. for (;;)
  1360. {
  1361. int avail_out;
  1362. char *next_out;
  1363. int out_size;
  1364. /* Reserve all the space available in the current chunk.
  1365. If none is available, start a new frag. */
  1366. avail_out = obstack_room (ob);
  1367. if (avail_out <= 0)
  1368. {
  1369. fragS *newf;
  1370. obstack_finish (ob);
  1371. newf = frag_alloc (ob);
  1372. newf->fr_type = rs_fill;
  1373. last_newf->fr_next = newf;
  1374. last_newf = newf;
  1375. avail_out = obstack_room (ob);
  1376. }
  1377. if (avail_out <= 0)
  1378. as_fatal (_("can't extend frag"));
  1379. next_out = obstack_next_free (ob);
  1380. obstack_blank_fast (ob, avail_out);
  1381. x = compress_finish (strm, &next_out, &avail_out, &out_size);
  1382. if (x < 0)
  1383. return;
  1384. last_newf->fr_fix += out_size;
  1385. compressed_size += out_size;
  1386. /* Return unused space. */
  1387. if (avail_out > 0)
  1388. obstack_blank_fast (ob, -avail_out);
  1389. if (x == 0)
  1390. break;
  1391. }
  1392. /* PR binutils/18087: If compression didn't make the section smaller,
  1393. just keep it uncompressed. */
  1394. if (compressed_size >= uncompressed_size)
  1395. return;
  1396. /* Replace the uncompressed frag list with the compressed frag list. */
  1397. seginfo->frchainP->frch_root = first_newf;
  1398. seginfo->frchainP->frch_last = last_newf;
  1399. /* Update the section size and its name. */
  1400. bfd_update_compression_header (abfd, (bfd_byte *) header, sec);
  1401. x = bfd_set_section_size (sec, compressed_size);
  1402. gas_assert (x);
  1403. if (!compression_header_size)
  1404. {
  1405. compressed_name = concat (".z", section_name + 1, (char *) NULL);
  1406. bfd_rename_section (sec, compressed_name);
  1407. }
  1408. }
  1409. #ifndef md_generate_nops
  1410. /* Genenerate COUNT bytes of no-op instructions to WHERE. A target
  1411. backend must override this with proper no-op instructions. */
  1412. static void
  1413. md_generate_nops (fragS *f ATTRIBUTE_UNUSED,
  1414. char *where ATTRIBUTE_UNUSED,
  1415. offsetT count ATTRIBUTE_UNUSED,
  1416. int control ATTRIBUTE_UNUSED)
  1417. {
  1418. as_bad (_("unimplemented .nops directive"));
  1419. }
  1420. #endif
  1421. static void
  1422. write_contents (bfd *abfd ATTRIBUTE_UNUSED,
  1423. asection *sec,
  1424. void *xxx ATTRIBUTE_UNUSED)
  1425. {
  1426. segment_info_type *seginfo = seg_info (sec);
  1427. addressT offset = 0;
  1428. fragS *f;
  1429. /* Write out the frags. */
  1430. if (seginfo == NULL
  1431. || !(bfd_section_flags (sec) & SEC_HAS_CONTENTS))
  1432. return;
  1433. for (f = seginfo->frchainP->frch_root;
  1434. f;
  1435. f = f->fr_next)
  1436. {
  1437. int x;
  1438. addressT fill_size;
  1439. char *fill_literal;
  1440. offsetT count;
  1441. gas_assert (f->fr_type == rs_fill || f->fr_type == rs_fill_nop);
  1442. if (f->fr_fix)
  1443. {
  1444. x = bfd_set_section_contents (stdoutput, sec,
  1445. f->fr_literal, (file_ptr) offset,
  1446. (bfd_size_type) f->fr_fix);
  1447. if (!x)
  1448. as_fatal (ngettext ("can't write %ld byte "
  1449. "to section %s of %s: '%s'",
  1450. "can't write %ld bytes "
  1451. "to section %s of %s: '%s'",
  1452. (long) f->fr_fix),
  1453. (long) f->fr_fix,
  1454. bfd_section_name (sec), bfd_get_filename (stdoutput),
  1455. bfd_errmsg (bfd_get_error ()));
  1456. offset += f->fr_fix;
  1457. }
  1458. fill_size = f->fr_var;
  1459. count = f->fr_offset;
  1460. fill_literal = f->fr_literal + f->fr_fix;
  1461. if (f->fr_type == rs_fill_nop)
  1462. {
  1463. gas_assert (count >= 0 && fill_size == 1);
  1464. if (count > 0)
  1465. {
  1466. char *buf = xmalloc (count);
  1467. md_generate_nops (f, buf, count, *fill_literal);
  1468. x = bfd_set_section_contents
  1469. (stdoutput, sec, buf, (file_ptr) offset,
  1470. (bfd_size_type) count);
  1471. if (!x)
  1472. as_fatal (ngettext ("can't fill %ld byte "
  1473. "in section %s of %s: '%s'",
  1474. "can't fill %ld bytes "
  1475. "in section %s of %s: '%s'",
  1476. (long) count),
  1477. (long) count,
  1478. bfd_section_name (sec),
  1479. bfd_get_filename (stdoutput),
  1480. bfd_errmsg (bfd_get_error ()));
  1481. offset += count;
  1482. free (buf);
  1483. }
  1484. continue;
  1485. }
  1486. gas_assert (count >= 0);
  1487. if (fill_size && count)
  1488. {
  1489. char buf[256];
  1490. if (fill_size > sizeof (buf))
  1491. {
  1492. /* Do it the old way. Can this ever happen? */
  1493. while (count--)
  1494. {
  1495. x = bfd_set_section_contents (stdoutput, sec,
  1496. fill_literal,
  1497. (file_ptr) offset,
  1498. (bfd_size_type) fill_size);
  1499. if (!x)
  1500. as_fatal (ngettext ("can't fill %ld byte "
  1501. "in section %s of %s: '%s'",
  1502. "can't fill %ld bytes "
  1503. "in section %s of %s: '%s'",
  1504. (long) fill_size),
  1505. (long) fill_size,
  1506. bfd_section_name (sec),
  1507. bfd_get_filename (stdoutput),
  1508. bfd_errmsg (bfd_get_error ()));
  1509. offset += fill_size;
  1510. }
  1511. }
  1512. else
  1513. {
  1514. /* Build a buffer full of fill objects and output it as
  1515. often as necessary. This saves on the overhead of
  1516. potentially lots of bfd_set_section_contents calls. */
  1517. int n_per_buf, i;
  1518. if (fill_size == 1)
  1519. {
  1520. n_per_buf = sizeof (buf);
  1521. memset (buf, *fill_literal, n_per_buf);
  1522. }
  1523. else
  1524. {
  1525. char *bufp;
  1526. n_per_buf = sizeof (buf) / fill_size;
  1527. for (i = n_per_buf, bufp = buf; i; i--, bufp += fill_size)
  1528. memcpy (bufp, fill_literal, fill_size);
  1529. }
  1530. for (; count > 0; count -= n_per_buf)
  1531. {
  1532. n_per_buf = n_per_buf > count ? count : n_per_buf;
  1533. x = bfd_set_section_contents
  1534. (stdoutput, sec, buf, (file_ptr) offset,
  1535. (bfd_size_type) n_per_buf * fill_size);
  1536. if (!x)
  1537. as_fatal (ngettext ("can't fill %ld byte "
  1538. "in section %s of %s: '%s'",
  1539. "can't fill %ld bytes "
  1540. "in section %s of %s: '%s'",
  1541. (long) (n_per_buf * fill_size)),
  1542. (long) (n_per_buf * fill_size),
  1543. bfd_section_name (sec),
  1544. bfd_get_filename (stdoutput),
  1545. bfd_errmsg (bfd_get_error ()));
  1546. offset += n_per_buf * fill_size;
  1547. }
  1548. }
  1549. }
  1550. }
  1551. }
  1552. static void
  1553. merge_data_into_text (void)
  1554. {
  1555. seg_info (text_section)->frchainP->frch_last->fr_next =
  1556. seg_info (data_section)->frchainP->frch_root;
  1557. seg_info (text_section)->frchainP->frch_last =
  1558. seg_info (data_section)->frchainP->frch_last;
  1559. seg_info (data_section)->frchainP = 0;
  1560. }
  1561. static void
  1562. set_symtab (void)
  1563. {
  1564. int nsyms;
  1565. asymbol **asympp;
  1566. symbolS *symp;
  1567. bool result;
  1568. /* Count symbols. We can't rely on a count made by the loop in
  1569. write_object_file, because *_frob_file may add a new symbol or
  1570. two. Generate unused section symbols only if needed. */
  1571. nsyms = 0;
  1572. for (symp = symbol_rootP; symp; symp = symbol_next (symp))
  1573. if (!symbol_removed_p (symp)
  1574. && (bfd_keep_unused_section_symbols (stdoutput)
  1575. || !symbol_section_p (symp)
  1576. || symbol_used_in_reloc_p (symp)))
  1577. nsyms++;
  1578. if (nsyms)
  1579. {
  1580. int i;
  1581. bfd_size_type amt = (bfd_size_type) nsyms * sizeof (asymbol *);
  1582. asympp = (asymbol **) bfd_alloc (stdoutput, amt);
  1583. symp = symbol_rootP;
  1584. for (i = 0; i < nsyms; symp = symbol_next (symp))
  1585. if (!symbol_removed_p (symp)
  1586. && (bfd_keep_unused_section_symbols (stdoutput)
  1587. || !symbol_section_p (symp)
  1588. || symbol_used_in_reloc_p (symp)))
  1589. {
  1590. asympp[i] = symbol_get_bfdsym (symp);
  1591. if (asympp[i]->flags != BSF_SECTION_SYM
  1592. || !(bfd_is_const_section (asympp[i]->section)
  1593. && asympp[i]->section->symbol == asympp[i]))
  1594. asympp[i]->flags |= BSF_KEEP;
  1595. symbol_mark_written (symp);
  1596. /* Include this section symbol in the symbol table. */
  1597. if (symbol_section_p (symp))
  1598. asympp[i]->flags |= BSF_SECTION_SYM_USED;
  1599. i++;
  1600. }
  1601. }
  1602. else
  1603. asympp = 0;
  1604. result = bfd_set_symtab (stdoutput, asympp, nsyms);
  1605. gas_assert (result);
  1606. symbol_table_frozen = 1;
  1607. }
  1608. /* Finish the subsegments. After every sub-segment, we fake an
  1609. ".align ...". This conforms to BSD4.2 brain-damage. We then fake
  1610. ".fill 0" because that is the kind of frag that requires least
  1611. thought. ".align" frags like to have a following frag since that
  1612. makes calculating their intended length trivial. */
  1613. #ifndef SUB_SEGMENT_ALIGN
  1614. #ifdef HANDLE_ALIGN
  1615. /* The last subsegment gets an alignment corresponding to the alignment
  1616. of the section. This allows proper nop-filling at the end of
  1617. code-bearing sections. */
  1618. #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) \
  1619. (!(FRCHAIN)->frch_next && subseg_text_p (SEG) \
  1620. && !do_not_pad_sections_to_alignment \
  1621. ? get_recorded_alignment (SEG) \
  1622. : 0)
  1623. #else
  1624. #define SUB_SEGMENT_ALIGN(SEG, FRCHAIN) 0
  1625. #endif
  1626. #endif
  1627. static void
  1628. subsegs_finish_section (asection *s)
  1629. {
  1630. struct frchain *frchainP;
  1631. segment_info_type *seginfo = seg_info (s);
  1632. if (!seginfo)
  1633. return;
  1634. for (frchainP = seginfo->frchainP;
  1635. frchainP != NULL;
  1636. frchainP = frchainP->frch_next)
  1637. {
  1638. int alignment;
  1639. subseg_set (s, frchainP->frch_subseg);
  1640. /* This now gets called even if we had errors. In that case,
  1641. any alignment is meaningless, and, moreover, will look weird
  1642. if we are generating a listing. */
  1643. if (had_errors ())
  1644. do_not_pad_sections_to_alignment = 1;
  1645. alignment = SUB_SEGMENT_ALIGN (now_seg, frchainP);
  1646. if ((bfd_section_flags (now_seg) & SEC_MERGE)
  1647. && now_seg->entsize)
  1648. {
  1649. unsigned int entsize = now_seg->entsize;
  1650. int entalign = 0;
  1651. while ((entsize & 1) == 0)
  1652. {
  1653. ++entalign;
  1654. entsize >>= 1;
  1655. }
  1656. if (entalign > alignment)
  1657. alignment = entalign;
  1658. }
  1659. if (subseg_text_p (now_seg))
  1660. frag_align_code (alignment, 0);
  1661. else
  1662. frag_align (alignment, 0, 0);
  1663. /* frag_align will have left a new frag.
  1664. Use this last frag for an empty ".fill".
  1665. For this segment ...
  1666. Create a last frag. Do not leave a "being filled in frag". */
  1667. frag_wane (frag_now);
  1668. frag_now->fr_fix = 0;
  1669. know (frag_now->fr_next == NULL);
  1670. }
  1671. }
  1672. static void
  1673. subsegs_finish (void)
  1674. {
  1675. asection *s;
  1676. for (s = stdoutput->sections; s; s = s->next)
  1677. subsegs_finish_section (s);
  1678. }
  1679. #ifdef OBJ_ELF
  1680. static void
  1681. create_obj_attrs_section (void)
  1682. {
  1683. segT s;
  1684. char *p;
  1685. offsetT size;
  1686. const char *name;
  1687. size = bfd_elf_obj_attr_size (stdoutput);
  1688. if (size == 0)
  1689. return;
  1690. name = get_elf_backend_data (stdoutput)->obj_attrs_section;
  1691. if (!name)
  1692. name = ".gnu.attributes";
  1693. s = subseg_new (name, 0);
  1694. elf_section_type (s)
  1695. = get_elf_backend_data (stdoutput)->obj_attrs_section_type;
  1696. bfd_set_section_flags (s, SEC_READONLY | SEC_DATA);
  1697. frag_now_fix ();
  1698. p = frag_more (size);
  1699. bfd_elf_set_obj_attr_contents (stdoutput, (bfd_byte *)p, size);
  1700. subsegs_finish_section (s);
  1701. relax_segment (seg_info (s)->frchainP->frch_root, s, 0);
  1702. size_seg (stdoutput, s, NULL);
  1703. }
  1704. /* Create a relocation against an entry in a GNU Build attribute section. */
  1705. static void
  1706. create_note_reloc (segT sec,
  1707. symbolS * sym,
  1708. bfd_size_type note_offset,
  1709. bfd_size_type desc2_offset,
  1710. offsetT desc2_size,
  1711. int reloc_type,
  1712. bfd_vma addend,
  1713. char * note)
  1714. {
  1715. struct reloc_list * reloc;
  1716. reloc = XNEW (struct reloc_list);
  1717. /* We create a .b type reloc as resolve_reloc_expr_symbols() has already been called. */
  1718. reloc->u.b.sec = sec;
  1719. reloc->u.b.s = symbol_get_bfdsym (sym);
  1720. reloc->u.b.r.sym_ptr_ptr = & reloc->u.b.s;
  1721. reloc->u.b.r.address = note_offset + desc2_offset;
  1722. reloc->u.b.r.addend = addend;
  1723. reloc->u.b.r.howto = bfd_reloc_type_lookup (stdoutput, reloc_type);
  1724. if (reloc->u.b.r.howto == NULL)
  1725. {
  1726. as_bad (_("unable to create reloc for build note"));
  1727. return;
  1728. }
  1729. reloc->file = N_("<gnu build note>");
  1730. reloc->line = 0;
  1731. reloc->next = reloc_list;
  1732. reloc_list = reloc;
  1733. /* For REL relocs, store the addend in the section. */
  1734. if (! sec->use_rela_p
  1735. /* The SH target is a special case that uses RELA relocs
  1736. but still stores the addend in the word being relocated. */
  1737. || strstr (bfd_get_target (stdoutput), "-sh") != NULL)
  1738. {
  1739. offsetT i;
  1740. /* Zero out the addend, since it is now stored in the note. */
  1741. reloc->u.b.r.addend = 0;
  1742. if (target_big_endian)
  1743. {
  1744. for (i = desc2_size; addend != 0 && i > 0; addend >>= 8, i--)
  1745. note[desc2_offset + i - 1] = (addend & 0xff);
  1746. }
  1747. else
  1748. {
  1749. for (i = 0; addend != 0 && i < desc2_size; addend >>= 8, i++)
  1750. note[desc2_offset + i] = (addend & 0xff);
  1751. }
  1752. }
  1753. }
  1754. static void
  1755. maybe_generate_build_notes (void)
  1756. {
  1757. segT sec;
  1758. char * note;
  1759. offsetT note_size;
  1760. offsetT total_size;
  1761. offsetT desc_size;
  1762. offsetT desc2_offset;
  1763. int desc_reloc;
  1764. symbolS * sym;
  1765. asymbol * bsym;
  1766. if (! flag_generate_build_notes
  1767. || bfd_get_section_by_name (stdoutput,
  1768. GNU_BUILD_ATTRS_SECTION_NAME) != NULL)
  1769. return;
  1770. /* Create a GNU Build Attribute section. */
  1771. sec = subseg_new (GNU_BUILD_ATTRS_SECTION_NAME, false);
  1772. elf_section_type (sec) = SHT_NOTE;
  1773. bfd_set_section_flags (sec, (SEC_READONLY | SEC_HAS_CONTENTS | SEC_DATA
  1774. | SEC_OCTETS));
  1775. bfd_set_section_alignment (sec, 2);
  1776. /* Work out the size of the notes that we will create,
  1777. and the relocation we should use. */
  1778. if (bfd_arch_bits_per_address (stdoutput) <= 32)
  1779. {
  1780. note_size = 28;
  1781. desc_size = 8; /* Two 4-byte offsets. */
  1782. desc2_offset = 24;
  1783. /* FIXME: The BFD backend for the CRX target does not support the
  1784. BFD_RELOC_32, even though it really should. Likewise for the
  1785. CR16 target. So we have special case code here... */
  1786. if (strstr (bfd_get_target (stdoutput), "-crx") != NULL)
  1787. desc_reloc = BFD_RELOC_CRX_NUM32;
  1788. else if (strstr (bfd_get_target (stdoutput), "-cr16") != NULL)
  1789. desc_reloc = BFD_RELOC_CR16_NUM32;
  1790. else
  1791. desc_reloc = BFD_RELOC_32;
  1792. }
  1793. else
  1794. {
  1795. note_size = 36;
  1796. desc_size = 16; /* Two 8-byte offsets. */
  1797. desc2_offset = 28;
  1798. /* FIXME: The BFD backend for the IA64 target does not support the
  1799. BFD_RELOC_64, even though it really should. The HPPA backend
  1800. has a similar issue, although it does not support BFD_RELOCs at
  1801. all! So we have special case code to handle these targets. */
  1802. if (strstr (bfd_get_target (stdoutput), "-ia64") != NULL)
  1803. desc_reloc = target_big_endian ? BFD_RELOC_IA64_DIR32MSB : BFD_RELOC_IA64_DIR32LSB;
  1804. else if (strstr (bfd_get_target (stdoutput), "-hppa") != NULL)
  1805. desc_reloc = 80; /* R_PARISC_DIR64. */
  1806. else
  1807. desc_reloc = BFD_RELOC_64;
  1808. }
  1809. /* We have to create a note for *each* code section.
  1810. Linker garbage collection might discard some. */
  1811. total_size = 0;
  1812. note = NULL;
  1813. for (sym = symbol_rootP; sym != NULL; sym = symbol_next (sym))
  1814. if ((bsym = symbol_get_bfdsym (sym)) != NULL
  1815. && bsym->flags & BSF_SECTION_SYM
  1816. && bsym->section != NULL
  1817. /* Skip linkonce sections - we cannot use these section symbols as they may disappear. */
  1818. && (bsym->section->flags & (SEC_CODE | SEC_LINK_ONCE)) == SEC_CODE
  1819. /* Not all linkonce sections are flagged... */
  1820. && !startswith (S_GET_NAME (sym), ".gnu.linkonce"))
  1821. {
  1822. /* Create a version note. */
  1823. frag_now_fix ();
  1824. note = frag_more (note_size);
  1825. memset (note, 0, note_size);
  1826. if (target_big_endian)
  1827. {
  1828. note[3] = 8; /* strlen (name) + 1. */
  1829. note[7] = desc_size; /* Two N-byte offsets. */
  1830. note[10] = NT_GNU_BUILD_ATTRIBUTE_OPEN >> 8;
  1831. note[11] = NT_GNU_BUILD_ATTRIBUTE_OPEN & 0xff;
  1832. }
  1833. else
  1834. {
  1835. note[0] = 8; /* strlen (name) + 1. */
  1836. note[4] = desc_size; /* Two N-byte offsets. */
  1837. note[8] = NT_GNU_BUILD_ATTRIBUTE_OPEN & 0xff;
  1838. note[9] = NT_GNU_BUILD_ATTRIBUTE_OPEN >> 8;
  1839. }
  1840. /* The a1 version number indicates that this note was
  1841. generated by the assembler and not the gcc annobin plugin. */
  1842. memcpy (note + 12, "GA$3a1", 8);
  1843. /* Create a relocation to install the start address of the note... */
  1844. create_note_reloc (sec, sym, total_size, 20, desc_size / 2, desc_reloc, 0, note);
  1845. /* ...and another one to install the end address. */
  1846. create_note_reloc (sec, sym, total_size, desc2_offset,
  1847. desc_size / 2,
  1848. desc_reloc,
  1849. bfd_section_size (bsym->section),
  1850. note);
  1851. /* Mark the section symbol used in relocation so that it will be
  1852. included in the symbol table. */
  1853. symbol_mark_used_in_reloc (sym);
  1854. total_size += note_size;
  1855. /* FIXME: Maybe add a note recording the assembler command line and version ? */
  1856. }
  1857. /* Install the note(s) into the section. */
  1858. if (total_size)
  1859. bfd_set_section_contents (stdoutput, sec, (bfd_byte *) note, 0, total_size);
  1860. subsegs_finish_section (sec);
  1861. relax_segment (seg_info (sec)->frchainP->frch_root, sec, 0);
  1862. size_seg (stdoutput, sec, NULL);
  1863. }
  1864. #endif /* OBJ_ELF */
  1865. /* Write the object file. */
  1866. void
  1867. write_object_file (void)
  1868. {
  1869. struct relax_seg_info rsi;
  1870. #ifndef WORKING_DOT_WORD
  1871. fragS *fragP; /* Track along all frags. */
  1872. #endif
  1873. subsegs_finish ();
  1874. #ifdef md_pre_output_hook
  1875. md_pre_output_hook;
  1876. #endif
  1877. #ifdef md_pre_relax_hook
  1878. md_pre_relax_hook;
  1879. #endif
  1880. /* From now on, we don't care about sub-segments. Build one frag chain
  1881. for each segment. Linked through fr_next. */
  1882. /* Remove the sections created by gas for its own purposes. */
  1883. {
  1884. int i;
  1885. bfd_section_list_remove (stdoutput, reg_section);
  1886. bfd_section_list_remove (stdoutput, expr_section);
  1887. stdoutput->section_count -= 2;
  1888. i = 0;
  1889. bfd_map_over_sections (stdoutput, renumber_sections, &i);
  1890. }
  1891. bfd_map_over_sections (stdoutput, chain_frchains_together, (char *) 0);
  1892. /* We have two segments. If user gave -R flag, then we must put the
  1893. data frags into the text segment. Do this before relaxing so
  1894. we know to take advantage of -R and make shorter addresses. */
  1895. if (flag_readonly_data_in_text)
  1896. {
  1897. merge_data_into_text ();
  1898. }
  1899. rsi.pass = 0;
  1900. while (1)
  1901. {
  1902. #ifndef WORKING_DOT_WORD
  1903. /* We need to reset the markers in the broken word list and
  1904. associated frags between calls to relax_segment (via
  1905. relax_seg). Since the broken word list is global, we do it
  1906. once per round, rather than locally in relax_segment for each
  1907. segment. */
  1908. struct broken_word *brokp;
  1909. for (brokp = broken_words;
  1910. brokp != (struct broken_word *) NULL;
  1911. brokp = brokp->next_broken_word)
  1912. {
  1913. brokp->added = 0;
  1914. if (brokp->dispfrag != (fragS *) NULL
  1915. && brokp->dispfrag->fr_type == rs_broken_word)
  1916. brokp->dispfrag->fr_subtype = 0;
  1917. }
  1918. #endif
  1919. rsi.changed = 0;
  1920. bfd_map_over_sections (stdoutput, relax_seg, &rsi);
  1921. rsi.pass++;
  1922. if (!rsi.changed)
  1923. break;
  1924. }
  1925. /* Note - Most ports will use the default value of
  1926. TC_FINALIZE_SYMS_BEFORE_SIZE_SEG, which 1. This will force
  1927. local symbols to be resolved, removing their frag information.
  1928. Some ports however, will not have finished relaxing all of
  1929. their frags and will still need the local symbol frag
  1930. information. These ports can set
  1931. TC_FINALIZE_SYMS_BEFORE_SIZE_SEG to 0. */
  1932. finalize_syms = TC_FINALIZE_SYMS_BEFORE_SIZE_SEG;
  1933. bfd_map_over_sections (stdoutput, size_seg, (char *) 0);
  1934. /* Relaxation has completed. Freeze all syms. */
  1935. finalize_syms = 1;
  1936. dwarf2dbg_final_check ();
  1937. #ifdef md_post_relax_hook
  1938. md_post_relax_hook;
  1939. #endif
  1940. #ifdef OBJ_ELF
  1941. if (IS_ELF)
  1942. create_obj_attrs_section ();
  1943. #endif
  1944. #ifndef WORKING_DOT_WORD
  1945. {
  1946. struct broken_word *lie;
  1947. struct broken_word **prevP;
  1948. prevP = &broken_words;
  1949. for (lie = broken_words; lie; lie = lie->next_broken_word)
  1950. if (!lie->added)
  1951. {
  1952. expressionS exp;
  1953. subseg_change (lie->seg, lie->subseg);
  1954. exp.X_op = O_subtract;
  1955. exp.X_add_symbol = lie->add;
  1956. exp.X_op_symbol = lie->sub;
  1957. exp.X_add_number = lie->addnum;
  1958. #ifdef TC_CONS_FIX_NEW
  1959. TC_CONS_FIX_NEW (lie->frag,
  1960. lie->word_goes_here - lie->frag->fr_literal,
  1961. 2, &exp, TC_PARSE_CONS_RETURN_NONE);
  1962. #else
  1963. fix_new_exp (lie->frag,
  1964. lie->word_goes_here - lie->frag->fr_literal,
  1965. 2, &exp, 0, BFD_RELOC_16);
  1966. #endif
  1967. *prevP = lie->next_broken_word;
  1968. }
  1969. else
  1970. prevP = &(lie->next_broken_word);
  1971. for (lie = broken_words; lie;)
  1972. {
  1973. struct broken_word *untruth;
  1974. char *table_ptr;
  1975. addressT table_addr;
  1976. addressT from_addr, to_addr;
  1977. int n, m;
  1978. subseg_change (lie->seg, lie->subseg);
  1979. fragP = lie->dispfrag;
  1980. /* Find out how many broken_words go here. */
  1981. n = 0;
  1982. for (untruth = lie;
  1983. untruth && untruth->dispfrag == fragP;
  1984. untruth = untruth->next_broken_word)
  1985. if (untruth->added == 1)
  1986. n++;
  1987. table_ptr = lie->dispfrag->fr_opcode;
  1988. table_addr = (lie->dispfrag->fr_address
  1989. + (table_ptr - lie->dispfrag->fr_literal));
  1990. /* Create the jump around the long jumps. This is a short
  1991. jump from table_ptr+0 to table_ptr+n*long_jump_size. */
  1992. from_addr = table_addr;
  1993. to_addr = table_addr + md_short_jump_size + n * md_long_jump_size;
  1994. md_create_short_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
  1995. lie->add);
  1996. table_ptr += md_short_jump_size;
  1997. table_addr += md_short_jump_size;
  1998. for (m = 0;
  1999. lie && lie->dispfrag == fragP;
  2000. m++, lie = lie->next_broken_word)
  2001. {
  2002. if (lie->added == 2)
  2003. continue;
  2004. /* Patch the jump table. */
  2005. for (untruth = (struct broken_word *) (fragP->fr_symbol);
  2006. untruth && untruth->dispfrag == fragP;
  2007. untruth = untruth->next_broken_word)
  2008. {
  2009. if (untruth->use_jump == lie)
  2010. {
  2011. /* This is the offset from ??? to table_ptr+0.
  2012. The target is the same for all users of this
  2013. md_long_jump, but the "sub" bases (and hence the
  2014. offsets) may be different. */
  2015. addressT to_word = table_addr - S_GET_VALUE (untruth->sub);
  2016. #ifdef TC_CHECK_ADJUSTED_BROKEN_DOT_WORD
  2017. TC_CHECK_ADJUSTED_BROKEN_DOT_WORD (to_word, untruth);
  2018. #endif
  2019. md_number_to_chars (untruth->word_goes_here, to_word, 2);
  2020. }
  2021. }
  2022. /* Install the long jump. */
  2023. /* This is a long jump from table_ptr+0 to the final target. */
  2024. from_addr = table_addr;
  2025. to_addr = S_GET_VALUE (lie->add) + lie->addnum;
  2026. md_create_long_jump (table_ptr, from_addr, to_addr, lie->dispfrag,
  2027. lie->add);
  2028. table_ptr += md_long_jump_size;
  2029. table_addr += md_long_jump_size;
  2030. }
  2031. }
  2032. }
  2033. #endif /* not WORKING_DOT_WORD */
  2034. /* Resolve symbol values. This needs to be done before processing
  2035. the relocations. */
  2036. if (symbol_rootP)
  2037. {
  2038. symbolS *symp;
  2039. for (symp = symbol_rootP; symp; symp = symbol_next (symp))
  2040. resolve_symbol_value (symp);
  2041. }
  2042. resolve_local_symbol_values ();
  2043. resolve_reloc_expr_symbols ();
  2044. #ifdef OBJ_ELF
  2045. if (IS_ELF)
  2046. maybe_generate_build_notes ();
  2047. #endif
  2048. PROGRESS (1);
  2049. #ifdef tc_frob_file_before_adjust
  2050. tc_frob_file_before_adjust ();
  2051. #endif
  2052. #ifdef obj_frob_file_before_adjust
  2053. obj_frob_file_before_adjust ();
  2054. #endif
  2055. bfd_map_over_sections (stdoutput, adjust_reloc_syms, (char *) 0);
  2056. #ifdef tc_frob_file_before_fix
  2057. tc_frob_file_before_fix ();
  2058. #endif
  2059. #ifdef obj_frob_file_before_fix
  2060. obj_frob_file_before_fix ();
  2061. #endif
  2062. bfd_map_over_sections (stdoutput, fix_segment, (char *) 0);
  2063. /* Set up symbol table, and write it out. */
  2064. if (symbol_rootP)
  2065. {
  2066. symbolS *symp;
  2067. bool skip_next_symbol = false;
  2068. for (symp = symbol_rootP; symp; symp = symbol_next (symp))
  2069. {
  2070. int punt = 0;
  2071. const char *name;
  2072. if (skip_next_symbol)
  2073. {
  2074. /* Don't do anything besides moving the value of the
  2075. symbol from the GAS value-field to the BFD value-field. */
  2076. symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
  2077. skip_next_symbol = false;
  2078. continue;
  2079. }
  2080. if (symbol_mri_common_p (symp))
  2081. {
  2082. if (S_IS_EXTERNAL (symp))
  2083. as_bad (_("%s: global symbols not supported in common sections"),
  2084. S_GET_NAME (symp));
  2085. symbol_remove (symp, &symbol_rootP, &symbol_lastP);
  2086. continue;
  2087. }
  2088. name = S_GET_NAME (symp);
  2089. if (name)
  2090. {
  2091. const char *name2 =
  2092. decode_local_label_name ((char *) S_GET_NAME (symp));
  2093. /* They only differ if `name' is a fb or dollar local
  2094. label name. */
  2095. if (name2 != name && ! S_IS_DEFINED (symp))
  2096. as_bad (_("local label `%s' is not defined"), name2);
  2097. }
  2098. /* Do it again, because adjust_reloc_syms might introduce
  2099. more symbols. They'll probably only be section symbols,
  2100. but they'll still need to have the values computed. */
  2101. resolve_symbol_value (symp);
  2102. /* Skip symbols which were equated to undefined or common
  2103. symbols. */
  2104. if (symbol_equated_reloc_p (symp)
  2105. || S_IS_WEAKREFR (symp))
  2106. {
  2107. const char *sname = S_GET_NAME (symp);
  2108. if (S_IS_COMMON (symp)
  2109. && !TC_FAKE_LABEL (sname)
  2110. && !S_IS_WEAKREFR (symp))
  2111. {
  2112. expressionS *e = symbol_get_value_expression (symp);
  2113. as_bad (_("`%s' can't be equated to common symbol `%s'"),
  2114. sname, S_GET_NAME (e->X_add_symbol));
  2115. }
  2116. if (S_GET_SEGMENT (symp) == reg_section)
  2117. {
  2118. /* Report error only if we know the symbol name. */
  2119. if (S_GET_NAME (symp) != reg_section->name)
  2120. as_bad (_("can't make global register symbol `%s'"),
  2121. sname);
  2122. }
  2123. symbol_remove (symp, &symbol_rootP, &symbol_lastP);
  2124. continue;
  2125. }
  2126. #ifdef obj_frob_symbol
  2127. obj_frob_symbol (symp, punt);
  2128. #endif
  2129. #ifdef tc_frob_symbol
  2130. if (! punt || symbol_used_in_reloc_p (symp))
  2131. tc_frob_symbol (symp, punt);
  2132. #endif
  2133. /* If we don't want to keep this symbol, splice it out of
  2134. the chain now. If EMIT_SECTION_SYMBOLS is 0, we never
  2135. want section symbols. Otherwise, we skip local symbols
  2136. and symbols that the frob_symbol macros told us to punt,
  2137. but we keep such symbols if they are used in relocs. */
  2138. if (symp == abs_section_sym
  2139. || (! EMIT_SECTION_SYMBOLS
  2140. && symbol_section_p (symp))
  2141. /* Note that S_IS_EXTERNAL and S_IS_LOCAL are not always
  2142. opposites. Sometimes the former checks flags and the
  2143. latter examines the name... */
  2144. || (!S_IS_EXTERNAL (symp)
  2145. && (punt || S_IS_LOCAL (symp) ||
  2146. (S_IS_WEAKREFD (symp) && ! symbol_used_p (symp)))
  2147. && ! symbol_used_in_reloc_p (symp)))
  2148. {
  2149. symbol_remove (symp, &symbol_rootP, &symbol_lastP);
  2150. /* After symbol_remove, symbol_next(symp) still returns
  2151. the one that came after it in the chain. So we don't
  2152. need to do any extra cleanup work here. */
  2153. continue;
  2154. }
  2155. /* Make sure we really got a value for the symbol. */
  2156. if (! symbol_resolved_p (symp))
  2157. {
  2158. as_bad (_("can't resolve value for symbol `%s'"),
  2159. S_GET_NAME (symp));
  2160. symbol_mark_resolved (symp);
  2161. }
  2162. /* Set the value into the BFD symbol. Up til now the value
  2163. has only been kept in the gas symbolS struct. */
  2164. symbol_get_bfdsym (symp)->value = S_GET_VALUE (symp);
  2165. /* A warning construct is a warning symbol followed by the
  2166. symbol warned about. Don't let anything object-format or
  2167. target-specific muck with it; it's ready for output. */
  2168. if (symbol_get_bfdsym (symp)->flags & BSF_WARNING)
  2169. skip_next_symbol = true;
  2170. }
  2171. }
  2172. PROGRESS (1);
  2173. /* Now do any format-specific adjustments to the symbol table, such
  2174. as adding file symbols. */
  2175. #ifdef tc_adjust_symtab
  2176. tc_adjust_symtab ();
  2177. #endif
  2178. #ifdef obj_adjust_symtab
  2179. obj_adjust_symtab ();
  2180. #endif
  2181. /* Stop if there is an error. */
  2182. if (!flag_always_generate_output && had_errors ())
  2183. return;
  2184. /* Now that all the sizes are known, and contents correct, we can
  2185. start writing to the file. */
  2186. set_symtab ();
  2187. /* If *_frob_file changes the symbol value at this point, it is
  2188. responsible for moving the changed value into symp->bsym->value
  2189. as well. Hopefully all symbol value changing can be done in
  2190. *_frob_symbol. */
  2191. #ifdef tc_frob_file
  2192. tc_frob_file ();
  2193. #endif
  2194. #ifdef obj_frob_file
  2195. obj_frob_file ();
  2196. #endif
  2197. #ifdef obj_coff_generate_pdata
  2198. obj_coff_generate_pdata ();
  2199. #endif
  2200. bfd_map_over_sections (stdoutput, write_relocs, (char *) 0);
  2201. #ifdef tc_frob_file_after_relocs
  2202. tc_frob_file_after_relocs ();
  2203. #endif
  2204. #ifdef obj_frob_file_after_relocs
  2205. obj_frob_file_after_relocs ();
  2206. #endif
  2207. #if defined OBJ_ELF || defined OBJ_MAYBE_ELF
  2208. if (IS_ELF && flag_use_elf_stt_common)
  2209. stdoutput->flags |= BFD_CONVERT_ELF_COMMON | BFD_USE_ELF_STT_COMMON;
  2210. #endif
  2211. /* Once all relocations have been written, we can compress the
  2212. contents of the debug sections. This needs to be done before
  2213. we start writing any sections, because it will affect the file
  2214. layout, which is fixed once we start writing contents. */
  2215. if (flag_compress_debug)
  2216. {
  2217. if (flag_compress_debug == COMPRESS_DEBUG_GABI_ZLIB)
  2218. stdoutput->flags |= BFD_COMPRESS | BFD_COMPRESS_GABI;
  2219. else
  2220. stdoutput->flags |= BFD_COMPRESS;
  2221. bfd_map_over_sections (stdoutput, compress_debug, (char *) 0);
  2222. }
  2223. bfd_map_over_sections (stdoutput, write_contents, (char *) 0);
  2224. }
  2225. #ifdef TC_GENERIC_RELAX_TABLE
  2226. #ifndef md_generic_table_relax_frag
  2227. #define md_generic_table_relax_frag relax_frag
  2228. #endif
  2229. /* Relax a fragment by scanning TC_GENERIC_RELAX_TABLE. */
  2230. long
  2231. relax_frag (segT segment, fragS *fragP, long stretch)
  2232. {
  2233. const relax_typeS *this_type;
  2234. const relax_typeS *start_type;
  2235. relax_substateT next_state;
  2236. relax_substateT this_state;
  2237. offsetT growth;
  2238. offsetT aim;
  2239. addressT target;
  2240. addressT address;
  2241. symbolS *symbolP;
  2242. const relax_typeS *table;
  2243. target = fragP->fr_offset;
  2244. address = fragP->fr_address + fragP->fr_fix;
  2245. table = TC_GENERIC_RELAX_TABLE;
  2246. this_state = fragP->fr_subtype;
  2247. start_type = this_type = table + this_state;
  2248. symbolP = fragP->fr_symbol;
  2249. if (symbolP)
  2250. {
  2251. fragS *sym_frag;
  2252. sym_frag = symbol_get_frag (symbolP);
  2253. #ifndef DIFF_EXPR_OK
  2254. know (sym_frag != NULL);
  2255. #endif
  2256. know (S_GET_SEGMENT (symbolP) != absolute_section
  2257. || sym_frag == &zero_address_frag);
  2258. target += S_GET_VALUE (symbolP);
  2259. /* If SYM_FRAG has yet to be reached on this pass, assume it
  2260. will move by STRETCH just as we did, unless there is an
  2261. alignment frag between here and SYM_FRAG. An alignment may
  2262. well absorb any STRETCH, and we don't want to choose a larger
  2263. branch insn by overestimating the needed reach of this
  2264. branch. It isn't critical to calculate TARGET exactly; We
  2265. know we'll be doing another pass if STRETCH is non-zero. */
  2266. if (stretch != 0
  2267. && sym_frag->relax_marker != fragP->relax_marker
  2268. && S_GET_SEGMENT (symbolP) == segment)
  2269. {
  2270. if (stretch < 0
  2271. || sym_frag->region == fragP->region)
  2272. target += stretch;
  2273. /* If we get here we know we have a forward branch. This
  2274. relax pass may have stretched previous instructions so
  2275. far that omitting STRETCH would make the branch
  2276. negative. Don't allow this in case the negative reach is
  2277. large enough to require a larger branch instruction. */
  2278. else if (target < address)
  2279. return 0;
  2280. }
  2281. }
  2282. aim = target - address;
  2283. #ifdef TC_PCREL_ADJUST
  2284. /* Currently only the ns32k and arc needs this. */
  2285. aim += TC_PCREL_ADJUST (fragP);
  2286. #endif
  2287. #ifdef md_prepare_relax_scan
  2288. /* Formerly called M68K_AIM_KLUDGE. */
  2289. md_prepare_relax_scan (fragP, address, aim, this_state, this_type);
  2290. #endif
  2291. if (aim < 0)
  2292. {
  2293. /* Look backwards. */
  2294. for (next_state = this_type->rlx_more; next_state;)
  2295. if (aim >= this_type->rlx_backward)
  2296. next_state = 0;
  2297. else
  2298. {
  2299. /* Grow to next state. */
  2300. this_state = next_state;
  2301. this_type = table + this_state;
  2302. next_state = this_type->rlx_more;
  2303. }
  2304. }
  2305. else
  2306. {
  2307. /* Look forwards. */
  2308. for (next_state = this_type->rlx_more; next_state;)
  2309. if (aim <= this_type->rlx_forward)
  2310. next_state = 0;
  2311. else
  2312. {
  2313. /* Grow to next state. */
  2314. this_state = next_state;
  2315. this_type = table + this_state;
  2316. next_state = this_type->rlx_more;
  2317. }
  2318. }
  2319. growth = this_type->rlx_length - start_type->rlx_length;
  2320. if (growth != 0)
  2321. fragP->fr_subtype = this_state;
  2322. return growth;
  2323. }
  2324. #endif /* defined (TC_GENERIC_RELAX_TABLE) */
  2325. /* Relax_align. Advance location counter to next address that has 'alignment'
  2326. lowest order bits all 0s, return size of adjustment made. */
  2327. static relax_addressT
  2328. relax_align (relax_addressT address, /* Address now. */
  2329. int alignment /* Alignment (binary). */)
  2330. {
  2331. relax_addressT mask;
  2332. relax_addressT new_address;
  2333. mask = ~((relax_addressT) ~0 << alignment);
  2334. new_address = (address + mask) & (~mask);
  2335. #ifdef LINKER_RELAXING_SHRINKS_ONLY
  2336. if (linkrelax)
  2337. /* We must provide lots of padding, so the linker can discard it
  2338. when needed. The linker will not add extra space, ever. */
  2339. new_address += (1 << alignment);
  2340. #endif
  2341. return (new_address - address);
  2342. }
  2343. /* Now we have a segment, not a crowd of sub-segments, we can make
  2344. fr_address values.
  2345. Relax the frags.
  2346. After this, all frags in this segment have addresses that are correct
  2347. within the segment. Since segments live in different file addresses,
  2348. these frag addresses may not be the same as final object-file
  2349. addresses. */
  2350. int
  2351. relax_segment (struct frag *segment_frag_root, segT segment, int pass)
  2352. {
  2353. unsigned long frag_count;
  2354. struct frag *fragP;
  2355. relax_addressT address;
  2356. int region;
  2357. int ret;
  2358. /* In case md_estimate_size_before_relax() wants to make fixSs. */
  2359. subseg_change (segment, 0);
  2360. /* For each frag in segment: count and store (a 1st guess of)
  2361. fr_address. */
  2362. address = 0;
  2363. region = 0;
  2364. for (frag_count = 0, fragP = segment_frag_root;
  2365. fragP;
  2366. fragP = fragP->fr_next, frag_count ++)
  2367. {
  2368. fragP->region = region;
  2369. fragP->relax_marker = 0;
  2370. fragP->fr_address = address;
  2371. address += fragP->fr_fix;
  2372. switch (fragP->fr_type)
  2373. {
  2374. case rs_fill:
  2375. address += fragP->fr_offset * fragP->fr_var;
  2376. break;
  2377. case rs_align:
  2378. case rs_align_code:
  2379. case rs_align_test:
  2380. {
  2381. addressT offset = relax_align (address, (int) fragP->fr_offset);
  2382. if (fragP->fr_subtype != 0 && offset > fragP->fr_subtype)
  2383. offset = 0;
  2384. if (offset % fragP->fr_var != 0)
  2385. {
  2386. as_bad_where (fragP->fr_file, fragP->fr_line,
  2387. ngettext ("alignment padding (%lu byte) "
  2388. "not a multiple of %ld",
  2389. "alignment padding (%lu bytes) "
  2390. "not a multiple of %ld",
  2391. (unsigned long) offset),
  2392. (unsigned long) offset, (long) fragP->fr_var);
  2393. offset -= (offset % fragP->fr_var);
  2394. }
  2395. address += offset;
  2396. region += 1;
  2397. }
  2398. break;
  2399. case rs_org:
  2400. /* Assume .org is nugatory. It will grow with 1st relax. */
  2401. region += 1;
  2402. break;
  2403. case rs_space:
  2404. case rs_space_nop:
  2405. break;
  2406. case rs_machine_dependent:
  2407. /* If fr_symbol is an expression, this call to
  2408. resolve_symbol_value sets up the correct segment, which will
  2409. likely be needed in md_estimate_size_before_relax. */
  2410. if (fragP->fr_symbol)
  2411. resolve_symbol_value (fragP->fr_symbol);
  2412. address += md_estimate_size_before_relax (fragP, segment);
  2413. break;
  2414. #ifndef WORKING_DOT_WORD
  2415. /* Broken words don't concern us yet. */
  2416. case rs_broken_word:
  2417. break;
  2418. #endif
  2419. case rs_leb128:
  2420. /* Initial guess is always 1; doing otherwise can result in
  2421. stable solutions that are larger than the minimum. */
  2422. address += fragP->fr_offset = 1;
  2423. break;
  2424. case rs_cfa:
  2425. address += eh_frame_estimate_size_before_relax (fragP);
  2426. break;
  2427. case rs_dwarf2dbg:
  2428. address += dwarf2dbg_estimate_size_before_relax (fragP);
  2429. break;
  2430. default:
  2431. BAD_CASE (fragP->fr_type);
  2432. break;
  2433. }
  2434. }
  2435. /* Do relax(). */
  2436. {
  2437. unsigned long max_iterations;
  2438. /* Cumulative address adjustment. */
  2439. offsetT stretch;
  2440. /* Have we made any adjustment this pass? We can't just test
  2441. stretch because one piece of code may have grown and another
  2442. shrank. */
  2443. int stretched;
  2444. /* Most horrible, but gcc may give us some exception data that
  2445. is impossible to assemble, of the form
  2446. .align 4
  2447. .byte 0, 0
  2448. .uleb128 end - start
  2449. start:
  2450. .space 128*128 - 1
  2451. .align 4
  2452. end:
  2453. If the leb128 is two bytes in size, then end-start is 128*128,
  2454. which requires a three byte leb128. If the leb128 is three
  2455. bytes in size, then end-start is 128*128-1, which requires a
  2456. two byte leb128. We work around this dilemma by inserting
  2457. an extra 4 bytes of alignment just after the .align. This
  2458. works because the data after the align is accessed relative to
  2459. the end label.
  2460. This counter is used in a tiny state machine to detect
  2461. whether a leb128 followed by an align is impossible to
  2462. relax. */
  2463. int rs_leb128_fudge = 0;
  2464. /* We want to prevent going into an infinite loop where one frag grows
  2465. depending upon the location of a symbol which is in turn moved by
  2466. the growing frag. eg:
  2467. foo = .
  2468. .org foo+16
  2469. foo = .
  2470. So we dictate that this algorithm can be at most O2. */
  2471. max_iterations = frag_count * frag_count;
  2472. /* Check for overflow. */
  2473. if (max_iterations < frag_count)
  2474. max_iterations = frag_count;
  2475. ret = 0;
  2476. do
  2477. {
  2478. stretch = 0;
  2479. stretched = 0;
  2480. for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
  2481. {
  2482. offsetT growth = 0;
  2483. addressT was_address;
  2484. offsetT offset;
  2485. symbolS *symbolP;
  2486. fragP->relax_marker ^= 1;
  2487. was_address = fragP->fr_address;
  2488. address = fragP->fr_address += stretch;
  2489. symbolP = fragP->fr_symbol;
  2490. offset = fragP->fr_offset;
  2491. switch (fragP->fr_type)
  2492. {
  2493. case rs_fill: /* .fill never relaxes. */
  2494. growth = 0;
  2495. break;
  2496. #ifndef WORKING_DOT_WORD
  2497. /* JF: This is RMS's idea. I do *NOT* want to be blamed
  2498. for it I do not want to write it. I do not want to have
  2499. anything to do with it. This is not the proper way to
  2500. implement this misfeature. */
  2501. case rs_broken_word:
  2502. {
  2503. struct broken_word *lie;
  2504. struct broken_word *untruth;
  2505. /* Yes this is ugly (storing the broken_word pointer
  2506. in the symbol slot). Still, this whole chunk of
  2507. code is ugly, and I don't feel like doing anything
  2508. about it. Think of it as stubbornness in action. */
  2509. growth = 0;
  2510. for (lie = (struct broken_word *) (fragP->fr_symbol);
  2511. lie && lie->dispfrag == fragP;
  2512. lie = lie->next_broken_word)
  2513. {
  2514. if (lie->added)
  2515. continue;
  2516. offset = (S_GET_VALUE (lie->add)
  2517. + lie->addnum
  2518. - S_GET_VALUE (lie->sub));
  2519. if (offset <= -32768 || offset >= 32767)
  2520. {
  2521. if (flag_warn_displacement)
  2522. {
  2523. char buf[50];
  2524. bfd_sprintf_vma (stdoutput, buf,
  2525. (addressT) lie->addnum);
  2526. as_warn_where (fragP->fr_file, fragP->fr_line,
  2527. _(".word %s-%s+%s didn't fit"),
  2528. S_GET_NAME (lie->add),
  2529. S_GET_NAME (lie->sub),
  2530. buf);
  2531. }
  2532. if (fragP->fr_subtype == 0)
  2533. {
  2534. fragP->fr_subtype++;
  2535. growth += md_short_jump_size;
  2536. }
  2537. /* Redirect *all* words of this table with the same
  2538. target, lest we have to handle the case where the
  2539. same target but with a offset that fits on this
  2540. round overflows at the next relaxation round. */
  2541. for (untruth = (struct broken_word *) (fragP->fr_symbol);
  2542. untruth && untruth->dispfrag == lie->dispfrag;
  2543. untruth = untruth->next_broken_word)
  2544. if ((symbol_get_frag (untruth->add)
  2545. == symbol_get_frag (lie->add))
  2546. && (S_GET_VALUE (untruth->add)
  2547. == S_GET_VALUE (lie->add)))
  2548. {
  2549. untruth->added = 2;
  2550. untruth->use_jump = lie;
  2551. }
  2552. lie->added = 1;
  2553. growth += md_long_jump_size;
  2554. }
  2555. }
  2556. break;
  2557. } /* case rs_broken_word */
  2558. #endif
  2559. case rs_align:
  2560. case rs_align_code:
  2561. case rs_align_test:
  2562. {
  2563. addressT oldoff, newoff;
  2564. oldoff = relax_align (was_address + fragP->fr_fix,
  2565. (int) offset);
  2566. newoff = relax_align (address + fragP->fr_fix,
  2567. (int) offset);
  2568. if (fragP->fr_subtype != 0)
  2569. {
  2570. if (oldoff > fragP->fr_subtype)
  2571. oldoff = 0;
  2572. if (newoff > fragP->fr_subtype)
  2573. newoff = 0;
  2574. }
  2575. growth = newoff - oldoff;
  2576. /* If this align happens to follow a leb128 and
  2577. we have determined that the leb128 is bouncing
  2578. in size, then break the cycle by inserting an
  2579. extra alignment. */
  2580. if (growth < 0
  2581. && (rs_leb128_fudge & 16) != 0
  2582. && (rs_leb128_fudge & 15) >= 2)
  2583. {
  2584. segment_info_type *seginfo = seg_info (segment);
  2585. struct obstack *ob = &seginfo->frchainP->frch_obstack;
  2586. struct frag *newf;
  2587. newf = frag_alloc (ob);
  2588. obstack_blank_fast (ob, fragP->fr_var);
  2589. obstack_finish (ob);
  2590. memcpy (newf, fragP, SIZEOF_STRUCT_FRAG);
  2591. memcpy (newf->fr_literal,
  2592. fragP->fr_literal + fragP->fr_fix,
  2593. fragP->fr_var);
  2594. newf->fr_type = rs_fill;
  2595. newf->fr_address = address + fragP->fr_fix + newoff;
  2596. newf->fr_fix = 0;
  2597. newf->fr_offset = (((offsetT) 1 << fragP->fr_offset)
  2598. / fragP->fr_var);
  2599. if (newf->fr_offset * newf->fr_var
  2600. != (offsetT) 1 << fragP->fr_offset)
  2601. {
  2602. newf->fr_offset = (offsetT) 1 << fragP->fr_offset;
  2603. newf->fr_var = 1;
  2604. }
  2605. /* Include size of new frag in GROWTH. */
  2606. growth += newf->fr_offset * newf->fr_var;
  2607. /* Adjust the new frag address for the amount
  2608. we'll add when we process the new frag. */
  2609. newf->fr_address -= stretch + growth;
  2610. newf->relax_marker ^= 1;
  2611. fragP->fr_next = newf;
  2612. #ifdef DEBUG
  2613. as_warn (_("padding added"));
  2614. #endif
  2615. }
  2616. }
  2617. break;
  2618. case rs_org:
  2619. {
  2620. offsetT target = offset;
  2621. addressT after;
  2622. if (symbolP)
  2623. {
  2624. /* Convert from an actual address to an octet offset
  2625. into the section. Here it is assumed that the
  2626. section's VMA is zero, and can omit subtracting it
  2627. from the symbol's value to get the address offset. */
  2628. know (S_GET_SEGMENT (symbolP)->vma == 0);
  2629. target += S_GET_VALUE (symbolP) * OCTETS_PER_BYTE;
  2630. }
  2631. know (fragP->fr_next);
  2632. after = fragP->fr_next->fr_address + stretch;
  2633. growth = target - after;
  2634. /* Growth may be negative, but variable part of frag
  2635. cannot have fewer than 0 chars. That is, we can't
  2636. .org backwards. */
  2637. if ((offsetT) (address + fragP->fr_fix) > target)
  2638. {
  2639. growth = 0;
  2640. /* Don't error on first few frag relax passes.
  2641. The symbol might be an expression involving
  2642. symbol values from other sections. If those
  2643. sections have not yet been processed their
  2644. frags will all have zero addresses, so we
  2645. will calculate incorrect values for them. The
  2646. number of passes we allow before giving an
  2647. error is somewhat arbitrary. It should be at
  2648. least one, with larger values requiring
  2649. increasingly contrived dependencies between
  2650. frags to trigger a false error. */
  2651. if (pass < 2)
  2652. {
  2653. /* Force another pass. */
  2654. ret = 1;
  2655. break;
  2656. }
  2657. as_bad_where (fragP->fr_file, fragP->fr_line,
  2658. _("attempt to move .org backwards"));
  2659. /* We've issued an error message. Change the
  2660. frag to avoid cascading errors. */
  2661. fragP->fr_type = rs_align;
  2662. fragP->fr_subtype = 0;
  2663. fragP->fr_offset = 0;
  2664. fragP->fr_fix = after - address;
  2665. }
  2666. }
  2667. break;
  2668. case rs_space:
  2669. case rs_space_nop:
  2670. growth = 0;
  2671. if (symbolP)
  2672. {
  2673. offsetT amount;
  2674. amount = S_GET_VALUE (symbolP);
  2675. if (S_GET_SEGMENT (symbolP) != absolute_section
  2676. || S_IS_COMMON (symbolP)
  2677. || ! S_IS_DEFINED (symbolP))
  2678. {
  2679. as_bad_where (fragP->fr_file, fragP->fr_line,
  2680. _(".space, .nops or .fill specifies non-absolute value"));
  2681. /* Prevent repeat of this error message. */
  2682. fragP->fr_symbol = 0;
  2683. }
  2684. else if (amount < 0)
  2685. {
  2686. /* Don't error on first few frag relax passes.
  2687. See rs_org comment for a longer explanation. */
  2688. if (pass < 2)
  2689. {
  2690. ret = 1;
  2691. break;
  2692. }
  2693. as_warn_where (fragP->fr_file, fragP->fr_line,
  2694. _(".space, .nops or .fill with negative value, ignored"));
  2695. fragP->fr_symbol = 0;
  2696. }
  2697. else
  2698. growth = (was_address + fragP->fr_fix + amount
  2699. - fragP->fr_next->fr_address);
  2700. }
  2701. break;
  2702. case rs_machine_dependent:
  2703. #ifdef md_relax_frag
  2704. growth = md_relax_frag (segment, fragP, stretch);
  2705. #else
  2706. #ifdef TC_GENERIC_RELAX_TABLE
  2707. /* The default way to relax a frag is to look through
  2708. TC_GENERIC_RELAX_TABLE. */
  2709. growth = md_generic_table_relax_frag (segment, fragP,
  2710. stretch);
  2711. #endif /* TC_GENERIC_RELAX_TABLE */
  2712. #endif
  2713. break;
  2714. case rs_leb128:
  2715. {
  2716. valueT value;
  2717. offsetT size;
  2718. value = resolve_symbol_value (fragP->fr_symbol);
  2719. size = sizeof_leb128 (value, fragP->fr_subtype);
  2720. growth = size - fragP->fr_offset;
  2721. fragP->fr_offset = size;
  2722. }
  2723. break;
  2724. case rs_cfa:
  2725. growth = eh_frame_relax_frag (fragP);
  2726. break;
  2727. case rs_dwarf2dbg:
  2728. growth = dwarf2dbg_relax_frag (fragP);
  2729. break;
  2730. default:
  2731. BAD_CASE (fragP->fr_type);
  2732. break;
  2733. }
  2734. if (growth)
  2735. {
  2736. stretch += growth;
  2737. stretched = 1;
  2738. if (fragP->fr_type == rs_leb128)
  2739. rs_leb128_fudge += 16;
  2740. else if (fragP->fr_type == rs_align
  2741. && (rs_leb128_fudge & 16) != 0
  2742. && stretch == 0)
  2743. rs_leb128_fudge += 16;
  2744. else
  2745. rs_leb128_fudge = 0;
  2746. }
  2747. }
  2748. if (stretch == 0
  2749. && (rs_leb128_fudge & 16) == 0
  2750. && (rs_leb128_fudge & -16) != 0)
  2751. rs_leb128_fudge += 1;
  2752. else
  2753. rs_leb128_fudge = 0;
  2754. }
  2755. /* Until nothing further to relax. */
  2756. while (stretched && -- max_iterations);
  2757. if (stretched)
  2758. as_fatal (_("Infinite loop encountered whilst attempting to compute the addresses of symbols in section %s"),
  2759. segment_name (segment));
  2760. }
  2761. for (fragP = segment_frag_root; fragP; fragP = fragP->fr_next)
  2762. if (fragP->last_fr_address != fragP->fr_address)
  2763. {
  2764. fragP->last_fr_address = fragP->fr_address;
  2765. ret = 1;
  2766. }
  2767. return ret;
  2768. }
  2769. void
  2770. number_to_chars_bigendian (char *buf, valueT val, int n)
  2771. {
  2772. if (n <= 0)
  2773. abort ();
  2774. while (n--)
  2775. {
  2776. buf[n] = val & 0xff;
  2777. val >>= 8;
  2778. }
  2779. }
  2780. void
  2781. number_to_chars_littleendian (char *buf, valueT val, int n)
  2782. {
  2783. if (n <= 0)
  2784. abort ();
  2785. while (n--)
  2786. {
  2787. *buf++ = val & 0xff;
  2788. val >>= 8;
  2789. }
  2790. }
  2791. void
  2792. write_print_statistics (FILE *file)
  2793. {
  2794. fprintf (file, "fixups: %d\n", n_fixups);
  2795. }
  2796. /* For debugging. */
  2797. extern int indent_level;
  2798. void
  2799. print_fixup (fixS *fixp)
  2800. {
  2801. indent_level = 1;
  2802. fprintf (stderr, "fix ");
  2803. fprintf_vma (stderr, (bfd_vma)((bfd_hostptr_t) fixp));
  2804. fprintf (stderr, " %s:%d",fixp->fx_file, fixp->fx_line);
  2805. if (fixp->fx_pcrel)
  2806. fprintf (stderr, " pcrel");
  2807. if (fixp->fx_pcrel_adjust)
  2808. fprintf (stderr, " pcrel_adjust=%d", fixp->fx_pcrel_adjust);
  2809. if (fixp->fx_tcbit)
  2810. fprintf (stderr, " tcbit");
  2811. if (fixp->fx_done)
  2812. fprintf (stderr, " done");
  2813. fprintf (stderr, "\n size=%d frag=", fixp->fx_size);
  2814. fprintf_vma (stderr, (bfd_vma) ((bfd_hostptr_t) fixp->fx_frag));
  2815. fprintf (stderr, " where=%ld offset=%lx addnumber=%lx",
  2816. (long) fixp->fx_where,
  2817. (unsigned long) fixp->fx_offset,
  2818. (unsigned long) fixp->fx_addnumber);
  2819. fprintf (stderr, "\n %s (%d)", bfd_get_reloc_code_name (fixp->fx_r_type),
  2820. fixp->fx_r_type);
  2821. if (fixp->fx_addsy)
  2822. {
  2823. fprintf (stderr, "\n +<");
  2824. print_symbol_value_1 (stderr, fixp->fx_addsy);
  2825. fprintf (stderr, ">");
  2826. }
  2827. if (fixp->fx_subsy)
  2828. {
  2829. fprintf (stderr, "\n -<");
  2830. print_symbol_value_1 (stderr, fixp->fx_subsy);
  2831. fprintf (stderr, ">");
  2832. }
  2833. fprintf (stderr, "\n");
  2834. #ifdef TC_FIX_DATA_PRINT
  2835. TC_FIX_DATA_PRINT (stderr, fixp);
  2836. #endif
  2837. }