write.c 77 KB

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