elf64-mmix.c 91 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917
  1. /* MMIX-specific support for 64-bit ELF.
  2. Copyright (C) 2001-2022 Free Software Foundation, Inc.
  3. Contributed by Hans-Peter Nilsson <hp@bitrange.com>
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. /* No specific ABI or "processor-specific supplement" defined. */
  18. /* TODO:
  19. - "Traditional" linker relaxation (shrinking whole sections).
  20. - Merge reloc stubs jumping to same location.
  21. - GETA stub relaxation (call a stub for out of range new
  22. R_MMIX_GETA_STUBBABLE). */
  23. #include "sysdep.h"
  24. #include "bfd.h"
  25. #include "libbfd.h"
  26. #include "elf-bfd.h"
  27. #include "elf/mmix.h"
  28. #include "opcode/mmix.h"
  29. #define MINUS_ONE (((bfd_vma) 0) - 1)
  30. #define MAX_PUSHJ_STUB_SIZE (5 * 4)
  31. /* Put these everywhere in new code. */
  32. #define FATAL_DEBUG \
  33. _bfd_abort (__FILE__, __LINE__, \
  34. "Internal: Non-debugged code (test-case missing)")
  35. #define BAD_CASE(x) \
  36. _bfd_abort (__FILE__, __LINE__, \
  37. "bad case for " #x)
  38. struct _mmix_elf_section_data
  39. {
  40. struct bfd_elf_section_data elf;
  41. union
  42. {
  43. struct bpo_reloc_section_info *reloc;
  44. struct bpo_greg_section_info *greg;
  45. } bpo;
  46. struct pushj_stub_info
  47. {
  48. /* Maximum number of stubs needed for this section. */
  49. bfd_size_type n_pushj_relocs;
  50. /* Size of stubs after a mmix_elf_relax_section round. */
  51. bfd_size_type stubs_size_sum;
  52. /* Per-reloc stubs_size_sum information. The stubs_size_sum member is the sum
  53. of these. Allocated in mmix_elf_check_common_relocs. */
  54. bfd_size_type *stub_size;
  55. /* Offset of next stub during relocation. Somewhat redundant with the
  56. above: error coverage is easier and we don't have to reset the
  57. stubs_size_sum for relocation. */
  58. bfd_size_type stub_offset;
  59. } pjs;
  60. /* Whether there has been a warning that this section could not be
  61. linked due to a specific cause. FIXME: a way to access the
  62. linker info or output section, then stuff the limiter guard
  63. there. */
  64. bool has_warned_bpo;
  65. bool has_warned_pushj;
  66. };
  67. #define mmix_elf_section_data(sec) \
  68. ((struct _mmix_elf_section_data *) elf_section_data (sec))
  69. /* For each section containing a base-plus-offset (BPO) reloc, we attach
  70. this struct as mmix_elf_section_data (section)->bpo, which is otherwise
  71. NULL. */
  72. struct bpo_reloc_section_info
  73. {
  74. /* The base is 1; this is the first number in this section. */
  75. size_t first_base_plus_offset_reloc;
  76. /* Number of BPO-relocs in this section. */
  77. size_t n_bpo_relocs_this_section;
  78. /* Running index, used at relocation time. */
  79. size_t bpo_index;
  80. /* We don't have access to the bfd_link_info struct in
  81. mmix_final_link_relocate. What we really want to get at is the
  82. global single struct greg_relocation, so we stash it here. */
  83. asection *bpo_greg_section;
  84. };
  85. /* Helper struct (in global context) for the one below.
  86. There's one of these created for every BPO reloc. */
  87. struct bpo_reloc_request
  88. {
  89. bfd_vma value;
  90. /* Valid after relaxation. The base is 0; the first register number
  91. must be added. The offset is in range 0..255. */
  92. size_t regindex;
  93. size_t offset;
  94. /* The order number for this BPO reloc, corresponding to the order in
  95. which BPO relocs were found. Used to create an index after reloc
  96. requests are sorted. */
  97. size_t bpo_reloc_no;
  98. /* Set when the value is computed. Better than coding "guard values"
  99. into the other members. Is FALSE only for BPO relocs in a GC:ed
  100. section. */
  101. bool valid;
  102. };
  103. /* We attach this as mmix_elf_section_data (sec)->bpo in the linker-allocated
  104. greg contents section (MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME),
  105. which is linked into the register contents section
  106. (MMIX_REG_CONTENTS_SECTION_NAME). This section is created by the
  107. linker; using the same hook as for usual with BPO relocs does not
  108. collide. */
  109. struct bpo_greg_section_info
  110. {
  111. /* After GC, this reflects the number of remaining, non-excluded
  112. BPO-relocs. */
  113. size_t n_bpo_relocs;
  114. /* This is the number of allocated bpo_reloc_requests; the size of
  115. sorted_indexes. Valid after the check.*relocs functions are called
  116. for all incoming sections. It includes the number of BPO relocs in
  117. sections that were GC:ed. */
  118. size_t n_max_bpo_relocs;
  119. /* A counter used to find out when to fold the BPO gregs, since we
  120. don't have a single "after-relaxation" hook. */
  121. size_t n_remaining_bpo_relocs_this_relaxation_round;
  122. /* The number of linker-allocated GREGs resulting from BPO relocs.
  123. This is an approximation after _bfd_mmix_before_linker_allocation
  124. and supposedly accurate after mmix_elf_relax_section is called for
  125. all incoming non-collected sections. */
  126. size_t n_allocated_bpo_gregs;
  127. /* Index into reloc_request[], sorted on increasing "value", secondary
  128. by increasing index for strict sorting order. */
  129. size_t *bpo_reloc_indexes;
  130. /* An array of all relocations, with the "value" member filled in by
  131. the relaxation function. */
  132. struct bpo_reloc_request *reloc_request;
  133. };
  134. extern bool mmix_elf_final_link (bfd *, struct bfd_link_info *);
  135. extern void mmix_elf_symbol_processing (bfd *, asymbol *);
  136. /* Only intended to be called from a debugger. */
  137. extern void mmix_dump_bpo_gregs
  138. (struct bfd_link_info *, void (*) (const char *, ...));
  139. static void
  140. mmix_set_relaxable_size (bfd *, asection *, void *);
  141. static bfd_reloc_status_type
  142. mmix_elf_reloc (bfd *, arelent *, asymbol *, void *,
  143. asection *, bfd *, char **);
  144. static bfd_reloc_status_type
  145. mmix_final_link_relocate (reloc_howto_type *, asection *, bfd_byte *, bfd_vma,
  146. bfd_signed_vma, bfd_vma, const char *, asection *,
  147. char **);
  148. /* Watch out: this currently needs to have elements with the same index as
  149. their R_MMIX_ number. */
  150. static reloc_howto_type elf_mmix_howto_table[] =
  151. {
  152. /* This reloc does nothing. */
  153. HOWTO (R_MMIX_NONE, /* type */
  154. 0, /* rightshift */
  155. 3, /* size (0 = byte, 1 = short, 2 = long) */
  156. 0, /* bitsize */
  157. false, /* pc_relative */
  158. 0, /* bitpos */
  159. complain_overflow_dont, /* complain_on_overflow */
  160. bfd_elf_generic_reloc, /* special_function */
  161. "R_MMIX_NONE", /* name */
  162. false, /* partial_inplace */
  163. 0, /* src_mask */
  164. 0, /* dst_mask */
  165. false), /* pcrel_offset */
  166. /* An 8 bit absolute relocation. */
  167. HOWTO (R_MMIX_8, /* type */
  168. 0, /* rightshift */
  169. 0, /* size (0 = byte, 1 = short, 2 = long) */
  170. 8, /* bitsize */
  171. false, /* pc_relative */
  172. 0, /* bitpos */
  173. complain_overflow_bitfield, /* complain_on_overflow */
  174. bfd_elf_generic_reloc, /* special_function */
  175. "R_MMIX_8", /* name */
  176. false, /* partial_inplace */
  177. 0, /* src_mask */
  178. 0xff, /* dst_mask */
  179. false), /* pcrel_offset */
  180. /* An 16 bit absolute relocation. */
  181. HOWTO (R_MMIX_16, /* type */
  182. 0, /* rightshift */
  183. 1, /* size (0 = byte, 1 = short, 2 = long) */
  184. 16, /* bitsize */
  185. false, /* pc_relative */
  186. 0, /* bitpos */
  187. complain_overflow_bitfield, /* complain_on_overflow */
  188. bfd_elf_generic_reloc, /* special_function */
  189. "R_MMIX_16", /* name */
  190. false, /* partial_inplace */
  191. 0, /* src_mask */
  192. 0xffff, /* dst_mask */
  193. false), /* pcrel_offset */
  194. /* An 24 bit absolute relocation. */
  195. HOWTO (R_MMIX_24, /* type */
  196. 0, /* rightshift */
  197. 2, /* size (0 = byte, 1 = short, 2 = long) */
  198. 24, /* bitsize */
  199. false, /* pc_relative */
  200. 0, /* bitpos */
  201. complain_overflow_bitfield, /* complain_on_overflow */
  202. bfd_elf_generic_reloc, /* special_function */
  203. "R_MMIX_24", /* name */
  204. false, /* partial_inplace */
  205. ~0xffffff, /* src_mask */
  206. 0xffffff, /* dst_mask */
  207. false), /* pcrel_offset */
  208. /* A 32 bit absolute relocation. */
  209. HOWTO (R_MMIX_32, /* type */
  210. 0, /* rightshift */
  211. 2, /* size (0 = byte, 1 = short, 2 = long) */
  212. 32, /* bitsize */
  213. false, /* pc_relative */
  214. 0, /* bitpos */
  215. complain_overflow_bitfield, /* complain_on_overflow */
  216. bfd_elf_generic_reloc, /* special_function */
  217. "R_MMIX_32", /* name */
  218. false, /* partial_inplace */
  219. 0, /* src_mask */
  220. 0xffffffff, /* dst_mask */
  221. false), /* pcrel_offset */
  222. /* 64 bit relocation. */
  223. HOWTO (R_MMIX_64, /* type */
  224. 0, /* rightshift */
  225. 4, /* size (0 = byte, 1 = short, 2 = long) */
  226. 64, /* bitsize */
  227. false, /* pc_relative */
  228. 0, /* bitpos */
  229. complain_overflow_bitfield, /* complain_on_overflow */
  230. bfd_elf_generic_reloc, /* special_function */
  231. "R_MMIX_64", /* name */
  232. false, /* partial_inplace */
  233. 0, /* src_mask */
  234. MINUS_ONE, /* dst_mask */
  235. false), /* pcrel_offset */
  236. /* An 8 bit PC-relative relocation. */
  237. HOWTO (R_MMIX_PC_8, /* type */
  238. 0, /* rightshift */
  239. 0, /* size (0 = byte, 1 = short, 2 = long) */
  240. 8, /* bitsize */
  241. true, /* pc_relative */
  242. 0, /* bitpos */
  243. complain_overflow_bitfield, /* complain_on_overflow */
  244. bfd_elf_generic_reloc, /* special_function */
  245. "R_MMIX_PC_8", /* name */
  246. false, /* partial_inplace */
  247. 0, /* src_mask */
  248. 0xff, /* dst_mask */
  249. true), /* pcrel_offset */
  250. /* An 16 bit PC-relative relocation. */
  251. HOWTO (R_MMIX_PC_16, /* type */
  252. 0, /* rightshift */
  253. 1, /* size (0 = byte, 1 = short, 2 = long) */
  254. 16, /* bitsize */
  255. true, /* pc_relative */
  256. 0, /* bitpos */
  257. complain_overflow_bitfield, /* complain_on_overflow */
  258. bfd_elf_generic_reloc, /* special_function */
  259. "R_MMIX_PC_16", /* name */
  260. false, /* partial_inplace */
  261. 0, /* src_mask */
  262. 0xffff, /* dst_mask */
  263. true), /* pcrel_offset */
  264. /* An 24 bit PC-relative relocation. */
  265. HOWTO (R_MMIX_PC_24, /* type */
  266. 0, /* rightshift */
  267. 2, /* size (0 = byte, 1 = short, 2 = long) */
  268. 24, /* bitsize */
  269. true, /* pc_relative */
  270. 0, /* bitpos */
  271. complain_overflow_bitfield, /* complain_on_overflow */
  272. bfd_elf_generic_reloc, /* special_function */
  273. "R_MMIX_PC_24", /* name */
  274. false, /* partial_inplace */
  275. ~0xffffff, /* src_mask */
  276. 0xffffff, /* dst_mask */
  277. true), /* pcrel_offset */
  278. /* A 32 bit absolute PC-relative relocation. */
  279. HOWTO (R_MMIX_PC_32, /* type */
  280. 0, /* rightshift */
  281. 2, /* size (0 = byte, 1 = short, 2 = long) */
  282. 32, /* bitsize */
  283. true, /* pc_relative */
  284. 0, /* bitpos */
  285. complain_overflow_bitfield, /* complain_on_overflow */
  286. bfd_elf_generic_reloc, /* special_function */
  287. "R_MMIX_PC_32", /* name */
  288. false, /* partial_inplace */
  289. 0, /* src_mask */
  290. 0xffffffff, /* dst_mask */
  291. true), /* pcrel_offset */
  292. /* 64 bit PC-relative relocation. */
  293. HOWTO (R_MMIX_PC_64, /* type */
  294. 0, /* rightshift */
  295. 4, /* size (0 = byte, 1 = short, 2 = long) */
  296. 64, /* bitsize */
  297. true, /* pc_relative */
  298. 0, /* bitpos */
  299. complain_overflow_bitfield, /* complain_on_overflow */
  300. bfd_elf_generic_reloc, /* special_function */
  301. "R_MMIX_PC_64", /* name */
  302. false, /* partial_inplace */
  303. 0, /* src_mask */
  304. MINUS_ONE, /* dst_mask */
  305. true), /* pcrel_offset */
  306. /* GNU extension to record C++ vtable hierarchy. */
  307. HOWTO (R_MMIX_GNU_VTINHERIT, /* type */
  308. 0, /* rightshift */
  309. 0, /* size (0 = byte, 1 = short, 2 = long) */
  310. 0, /* bitsize */
  311. false, /* pc_relative */
  312. 0, /* bitpos */
  313. complain_overflow_dont, /* complain_on_overflow */
  314. NULL, /* special_function */
  315. "R_MMIX_GNU_VTINHERIT", /* name */
  316. false, /* partial_inplace */
  317. 0, /* src_mask */
  318. 0, /* dst_mask */
  319. true), /* pcrel_offset */
  320. /* GNU extension to record C++ vtable member usage. */
  321. HOWTO (R_MMIX_GNU_VTENTRY, /* type */
  322. 0, /* rightshift */
  323. 0, /* size (0 = byte, 1 = short, 2 = long) */
  324. 0, /* bitsize */
  325. false, /* pc_relative */
  326. 0, /* bitpos */
  327. complain_overflow_dont, /* complain_on_overflow */
  328. _bfd_elf_rel_vtable_reloc_fn, /* special_function */
  329. "R_MMIX_GNU_VTENTRY", /* name */
  330. false, /* partial_inplace */
  331. 0, /* src_mask */
  332. 0, /* dst_mask */
  333. false), /* pcrel_offset */
  334. /* The GETA relocation is supposed to get any address that could
  335. possibly be reached by the GETA instruction. It can silently expand
  336. to get a 64-bit operand, but will complain if any of the two least
  337. significant bits are set. The howto members reflect a simple GETA. */
  338. HOWTO (R_MMIX_GETA, /* type */
  339. 2, /* rightshift */
  340. 2, /* size (0 = byte, 1 = short, 2 = long) */
  341. 19, /* bitsize */
  342. true, /* pc_relative */
  343. 0, /* bitpos */
  344. complain_overflow_signed, /* complain_on_overflow */
  345. mmix_elf_reloc, /* special_function */
  346. "R_MMIX_GETA", /* name */
  347. false, /* partial_inplace */
  348. ~0x0100ffff, /* src_mask */
  349. 0x0100ffff, /* dst_mask */
  350. true), /* pcrel_offset */
  351. HOWTO (R_MMIX_GETA_1, /* type */
  352. 2, /* rightshift */
  353. 2, /* size (0 = byte, 1 = short, 2 = long) */
  354. 19, /* bitsize */
  355. true, /* pc_relative */
  356. 0, /* bitpos */
  357. complain_overflow_signed, /* complain_on_overflow */
  358. mmix_elf_reloc, /* special_function */
  359. "R_MMIX_GETA_1", /* name */
  360. false, /* partial_inplace */
  361. ~0x0100ffff, /* src_mask */
  362. 0x0100ffff, /* dst_mask */
  363. true), /* pcrel_offset */
  364. HOWTO (R_MMIX_GETA_2, /* type */
  365. 2, /* rightshift */
  366. 2, /* size (0 = byte, 1 = short, 2 = long) */
  367. 19, /* bitsize */
  368. true, /* pc_relative */
  369. 0, /* bitpos */
  370. complain_overflow_signed, /* complain_on_overflow */
  371. mmix_elf_reloc, /* special_function */
  372. "R_MMIX_GETA_2", /* name */
  373. false, /* partial_inplace */
  374. ~0x0100ffff, /* src_mask */
  375. 0x0100ffff, /* dst_mask */
  376. true), /* pcrel_offset */
  377. HOWTO (R_MMIX_GETA_3, /* type */
  378. 2, /* rightshift */
  379. 2, /* size (0 = byte, 1 = short, 2 = long) */
  380. 19, /* bitsize */
  381. true, /* pc_relative */
  382. 0, /* bitpos */
  383. complain_overflow_signed, /* complain_on_overflow */
  384. mmix_elf_reloc, /* special_function */
  385. "R_MMIX_GETA_3", /* name */
  386. false, /* partial_inplace */
  387. ~0x0100ffff, /* src_mask */
  388. 0x0100ffff, /* dst_mask */
  389. true), /* pcrel_offset */
  390. /* The conditional branches are supposed to reach any (code) address.
  391. It can silently expand to a 64-bit operand, but will emit an error if
  392. any of the two least significant bits are set. The howto members
  393. reflect a simple branch. */
  394. HOWTO (R_MMIX_CBRANCH, /* type */
  395. 2, /* rightshift */
  396. 2, /* size (0 = byte, 1 = short, 2 = long) */
  397. 19, /* bitsize */
  398. true, /* pc_relative */
  399. 0, /* bitpos */
  400. complain_overflow_signed, /* complain_on_overflow */
  401. mmix_elf_reloc, /* special_function */
  402. "R_MMIX_CBRANCH", /* name */
  403. false, /* partial_inplace */
  404. ~0x0100ffff, /* src_mask */
  405. 0x0100ffff, /* dst_mask */
  406. true), /* pcrel_offset */
  407. HOWTO (R_MMIX_CBRANCH_J, /* type */
  408. 2, /* rightshift */
  409. 2, /* size (0 = byte, 1 = short, 2 = long) */
  410. 19, /* bitsize */
  411. true, /* pc_relative */
  412. 0, /* bitpos */
  413. complain_overflow_signed, /* complain_on_overflow */
  414. mmix_elf_reloc, /* special_function */
  415. "R_MMIX_CBRANCH_J", /* name */
  416. false, /* partial_inplace */
  417. ~0x0100ffff, /* src_mask */
  418. 0x0100ffff, /* dst_mask */
  419. true), /* pcrel_offset */
  420. HOWTO (R_MMIX_CBRANCH_1, /* type */
  421. 2, /* rightshift */
  422. 2, /* size (0 = byte, 1 = short, 2 = long) */
  423. 19, /* bitsize */
  424. true, /* pc_relative */
  425. 0, /* bitpos */
  426. complain_overflow_signed, /* complain_on_overflow */
  427. mmix_elf_reloc, /* special_function */
  428. "R_MMIX_CBRANCH_1", /* name */
  429. false, /* partial_inplace */
  430. ~0x0100ffff, /* src_mask */
  431. 0x0100ffff, /* dst_mask */
  432. true), /* pcrel_offset */
  433. HOWTO (R_MMIX_CBRANCH_2, /* type */
  434. 2, /* rightshift */
  435. 2, /* size (0 = byte, 1 = short, 2 = long) */
  436. 19, /* bitsize */
  437. true, /* pc_relative */
  438. 0, /* bitpos */
  439. complain_overflow_signed, /* complain_on_overflow */
  440. mmix_elf_reloc, /* special_function */
  441. "R_MMIX_CBRANCH_2", /* name */
  442. false, /* partial_inplace */
  443. ~0x0100ffff, /* src_mask */
  444. 0x0100ffff, /* dst_mask */
  445. true), /* pcrel_offset */
  446. HOWTO (R_MMIX_CBRANCH_3, /* type */
  447. 2, /* rightshift */
  448. 2, /* size (0 = byte, 1 = short, 2 = long) */
  449. 19, /* bitsize */
  450. true, /* pc_relative */
  451. 0, /* bitpos */
  452. complain_overflow_signed, /* complain_on_overflow */
  453. mmix_elf_reloc, /* special_function */
  454. "R_MMIX_CBRANCH_3", /* name */
  455. false, /* partial_inplace */
  456. ~0x0100ffff, /* src_mask */
  457. 0x0100ffff, /* dst_mask */
  458. true), /* pcrel_offset */
  459. /* The PUSHJ instruction can reach any (code) address, as long as it's
  460. the beginning of a function (no usable restriction). It can silently
  461. expand to a 64-bit operand, but will emit an error if any of the two
  462. least significant bits are set. It can also expand into a call to a
  463. stub; see R_MMIX_PUSHJ_STUBBABLE. The howto members reflect a simple
  464. PUSHJ. */
  465. HOWTO (R_MMIX_PUSHJ, /* type */
  466. 2, /* rightshift */
  467. 2, /* size (0 = byte, 1 = short, 2 = long) */
  468. 19, /* bitsize */
  469. true, /* pc_relative */
  470. 0, /* bitpos */
  471. complain_overflow_signed, /* complain_on_overflow */
  472. mmix_elf_reloc, /* special_function */
  473. "R_MMIX_PUSHJ", /* name */
  474. false, /* partial_inplace */
  475. ~0x0100ffff, /* src_mask */
  476. 0x0100ffff, /* dst_mask */
  477. true), /* pcrel_offset */
  478. HOWTO (R_MMIX_PUSHJ_1, /* type */
  479. 2, /* rightshift */
  480. 2, /* size (0 = byte, 1 = short, 2 = long) */
  481. 19, /* bitsize */
  482. true, /* pc_relative */
  483. 0, /* bitpos */
  484. complain_overflow_signed, /* complain_on_overflow */
  485. mmix_elf_reloc, /* special_function */
  486. "R_MMIX_PUSHJ_1", /* name */
  487. false, /* partial_inplace */
  488. ~0x0100ffff, /* src_mask */
  489. 0x0100ffff, /* dst_mask */
  490. true), /* pcrel_offset */
  491. HOWTO (R_MMIX_PUSHJ_2, /* type */
  492. 2, /* rightshift */
  493. 2, /* size (0 = byte, 1 = short, 2 = long) */
  494. 19, /* bitsize */
  495. true, /* pc_relative */
  496. 0, /* bitpos */
  497. complain_overflow_signed, /* complain_on_overflow */
  498. mmix_elf_reloc, /* special_function */
  499. "R_MMIX_PUSHJ_2", /* name */
  500. false, /* partial_inplace */
  501. ~0x0100ffff, /* src_mask */
  502. 0x0100ffff, /* dst_mask */
  503. true), /* pcrel_offset */
  504. HOWTO (R_MMIX_PUSHJ_3, /* type */
  505. 2, /* rightshift */
  506. 2, /* size (0 = byte, 1 = short, 2 = long) */
  507. 19, /* bitsize */
  508. true, /* pc_relative */
  509. 0, /* bitpos */
  510. complain_overflow_signed, /* complain_on_overflow */
  511. mmix_elf_reloc, /* special_function */
  512. "R_MMIX_PUSHJ_3", /* name */
  513. false, /* partial_inplace */
  514. ~0x0100ffff, /* src_mask */
  515. 0x0100ffff, /* dst_mask */
  516. true), /* pcrel_offset */
  517. /* A JMP is supposed to reach any (code) address. By itself, it can
  518. reach +-64M; the expansion can reach all 64 bits. Note that the 64M
  519. limit is soon reached if you link the program in wildly different
  520. memory segments. The howto members reflect a trivial JMP. */
  521. HOWTO (R_MMIX_JMP, /* type */
  522. 2, /* rightshift */
  523. 2, /* size (0 = byte, 1 = short, 2 = long) */
  524. 27, /* bitsize */
  525. true, /* pc_relative */
  526. 0, /* bitpos */
  527. complain_overflow_signed, /* complain_on_overflow */
  528. mmix_elf_reloc, /* special_function */
  529. "R_MMIX_JMP", /* name */
  530. false, /* partial_inplace */
  531. ~0x1ffffff, /* src_mask */
  532. 0x1ffffff, /* dst_mask */
  533. true), /* pcrel_offset */
  534. HOWTO (R_MMIX_JMP_1, /* type */
  535. 2, /* rightshift */
  536. 2, /* size (0 = byte, 1 = short, 2 = long) */
  537. 27, /* bitsize */
  538. true, /* pc_relative */
  539. 0, /* bitpos */
  540. complain_overflow_signed, /* complain_on_overflow */
  541. mmix_elf_reloc, /* special_function */
  542. "R_MMIX_JMP_1", /* name */
  543. false, /* partial_inplace */
  544. ~0x1ffffff, /* src_mask */
  545. 0x1ffffff, /* dst_mask */
  546. true), /* pcrel_offset */
  547. HOWTO (R_MMIX_JMP_2, /* type */
  548. 2, /* rightshift */
  549. 2, /* size (0 = byte, 1 = short, 2 = long) */
  550. 27, /* bitsize */
  551. true, /* pc_relative */
  552. 0, /* bitpos */
  553. complain_overflow_signed, /* complain_on_overflow */
  554. mmix_elf_reloc, /* special_function */
  555. "R_MMIX_JMP_2", /* name */
  556. false, /* partial_inplace */
  557. ~0x1ffffff, /* src_mask */
  558. 0x1ffffff, /* dst_mask */
  559. true), /* pcrel_offset */
  560. HOWTO (R_MMIX_JMP_3, /* type */
  561. 2, /* rightshift */
  562. 2, /* size (0 = byte, 1 = short, 2 = long) */
  563. 27, /* bitsize */
  564. true, /* pc_relative */
  565. 0, /* bitpos */
  566. complain_overflow_signed, /* complain_on_overflow */
  567. mmix_elf_reloc, /* special_function */
  568. "R_MMIX_JMP_3", /* name */
  569. false, /* partial_inplace */
  570. ~0x1ffffff, /* src_mask */
  571. 0x1ffffff, /* dst_mask */
  572. true), /* pcrel_offset */
  573. /* When we don't emit link-time-relaxable code from the assembler, or
  574. when relaxation has done all it can do, these relocs are used. For
  575. GETA/PUSHJ/branches. */
  576. HOWTO (R_MMIX_ADDR19, /* type */
  577. 2, /* rightshift */
  578. 2, /* size (0 = byte, 1 = short, 2 = long) */
  579. 19, /* bitsize */
  580. true, /* pc_relative */
  581. 0, /* bitpos */
  582. complain_overflow_signed, /* complain_on_overflow */
  583. mmix_elf_reloc, /* special_function */
  584. "R_MMIX_ADDR19", /* name */
  585. false, /* partial_inplace */
  586. ~0x0100ffff, /* src_mask */
  587. 0x0100ffff, /* dst_mask */
  588. true), /* pcrel_offset */
  589. /* For JMP. */
  590. HOWTO (R_MMIX_ADDR27, /* type */
  591. 2, /* rightshift */
  592. 2, /* size (0 = byte, 1 = short, 2 = long) */
  593. 27, /* bitsize */
  594. true, /* pc_relative */
  595. 0, /* bitpos */
  596. complain_overflow_signed, /* complain_on_overflow */
  597. mmix_elf_reloc, /* special_function */
  598. "R_MMIX_ADDR27", /* name */
  599. false, /* partial_inplace */
  600. ~0x1ffffff, /* src_mask */
  601. 0x1ffffff, /* dst_mask */
  602. true), /* pcrel_offset */
  603. /* A general register or the value 0..255. If a value, then the
  604. instruction (offset -3) needs adjusting. */
  605. HOWTO (R_MMIX_REG_OR_BYTE, /* type */
  606. 0, /* rightshift */
  607. 1, /* size (0 = byte, 1 = short, 2 = long) */
  608. 8, /* bitsize */
  609. false, /* pc_relative */
  610. 0, /* bitpos */
  611. complain_overflow_bitfield, /* complain_on_overflow */
  612. mmix_elf_reloc, /* special_function */
  613. "R_MMIX_REG_OR_BYTE", /* name */
  614. false, /* partial_inplace */
  615. 0, /* src_mask */
  616. 0xff, /* dst_mask */
  617. false), /* pcrel_offset */
  618. /* A general register. */
  619. HOWTO (R_MMIX_REG, /* type */
  620. 0, /* rightshift */
  621. 1, /* size (0 = byte, 1 = short, 2 = long) */
  622. 8, /* bitsize */
  623. false, /* pc_relative */
  624. 0, /* bitpos */
  625. complain_overflow_bitfield, /* complain_on_overflow */
  626. mmix_elf_reloc, /* special_function */
  627. "R_MMIX_REG", /* name */
  628. false, /* partial_inplace */
  629. 0, /* src_mask */
  630. 0xff, /* dst_mask */
  631. false), /* pcrel_offset */
  632. /* A register plus an index, corresponding to the relocation expression.
  633. The sizes must correspond to the valid range of the expression, while
  634. the bitmasks correspond to what we store in the image. */
  635. HOWTO (R_MMIX_BASE_PLUS_OFFSET, /* type */
  636. 0, /* rightshift */
  637. 4, /* size (0 = byte, 1 = short, 2 = long) */
  638. 64, /* bitsize */
  639. false, /* pc_relative */
  640. 0, /* bitpos */
  641. complain_overflow_bitfield, /* complain_on_overflow */
  642. mmix_elf_reloc, /* special_function */
  643. "R_MMIX_BASE_PLUS_OFFSET", /* name */
  644. false, /* partial_inplace */
  645. 0, /* src_mask */
  646. 0xffff, /* dst_mask */
  647. false), /* pcrel_offset */
  648. /* A "magic" relocation for a LOCAL expression, asserting that the
  649. expression is less than the number of global registers. No actual
  650. modification of the contents is done. Implementing this as a
  651. relocation was less intrusive than e.g. putting such expressions in a
  652. section to discard *after* relocation. */
  653. HOWTO (R_MMIX_LOCAL, /* type */
  654. 0, /* rightshift */
  655. 0, /* size (0 = byte, 1 = short, 2 = long) */
  656. 0, /* bitsize */
  657. false, /* pc_relative */
  658. 0, /* bitpos */
  659. complain_overflow_dont, /* complain_on_overflow */
  660. mmix_elf_reloc, /* special_function */
  661. "R_MMIX_LOCAL", /* name */
  662. false, /* partial_inplace */
  663. 0, /* src_mask */
  664. 0, /* dst_mask */
  665. false), /* pcrel_offset */
  666. HOWTO (R_MMIX_PUSHJ_STUBBABLE, /* type */
  667. 2, /* rightshift */
  668. 2, /* size (0 = byte, 1 = short, 2 = long) */
  669. 19, /* bitsize */
  670. true, /* pc_relative */
  671. 0, /* bitpos */
  672. complain_overflow_signed, /* complain_on_overflow */
  673. mmix_elf_reloc, /* special_function */
  674. "R_MMIX_PUSHJ_STUBBABLE", /* name */
  675. false, /* partial_inplace */
  676. ~0x0100ffff, /* src_mask */
  677. 0x0100ffff, /* dst_mask */
  678. true) /* pcrel_offset */
  679. };
  680. /* Map BFD reloc types to MMIX ELF reloc types. */
  681. struct mmix_reloc_map
  682. {
  683. bfd_reloc_code_real_type bfd_reloc_val;
  684. enum elf_mmix_reloc_type elf_reloc_val;
  685. };
  686. static const struct mmix_reloc_map mmix_reloc_map[] =
  687. {
  688. {BFD_RELOC_NONE, R_MMIX_NONE},
  689. {BFD_RELOC_8, R_MMIX_8},
  690. {BFD_RELOC_16, R_MMIX_16},
  691. {BFD_RELOC_24, R_MMIX_24},
  692. {BFD_RELOC_32, R_MMIX_32},
  693. {BFD_RELOC_64, R_MMIX_64},
  694. {BFD_RELOC_8_PCREL, R_MMIX_PC_8},
  695. {BFD_RELOC_16_PCREL, R_MMIX_PC_16},
  696. {BFD_RELOC_24_PCREL, R_MMIX_PC_24},
  697. {BFD_RELOC_32_PCREL, R_MMIX_PC_32},
  698. {BFD_RELOC_64_PCREL, R_MMIX_PC_64},
  699. {BFD_RELOC_VTABLE_INHERIT, R_MMIX_GNU_VTINHERIT},
  700. {BFD_RELOC_VTABLE_ENTRY, R_MMIX_GNU_VTENTRY},
  701. {BFD_RELOC_MMIX_GETA, R_MMIX_GETA},
  702. {BFD_RELOC_MMIX_CBRANCH, R_MMIX_CBRANCH},
  703. {BFD_RELOC_MMIX_PUSHJ, R_MMIX_PUSHJ},
  704. {BFD_RELOC_MMIX_JMP, R_MMIX_JMP},
  705. {BFD_RELOC_MMIX_ADDR19, R_MMIX_ADDR19},
  706. {BFD_RELOC_MMIX_ADDR27, R_MMIX_ADDR27},
  707. {BFD_RELOC_MMIX_REG_OR_BYTE, R_MMIX_REG_OR_BYTE},
  708. {BFD_RELOC_MMIX_REG, R_MMIX_REG},
  709. {BFD_RELOC_MMIX_BASE_PLUS_OFFSET, R_MMIX_BASE_PLUS_OFFSET},
  710. {BFD_RELOC_MMIX_LOCAL, R_MMIX_LOCAL},
  711. {BFD_RELOC_MMIX_PUSHJ_STUBBABLE, R_MMIX_PUSHJ_STUBBABLE}
  712. };
  713. static reloc_howto_type *
  714. bfd_elf64_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  715. bfd_reloc_code_real_type code)
  716. {
  717. unsigned int i;
  718. for (i = 0;
  719. i < sizeof (mmix_reloc_map) / sizeof (mmix_reloc_map[0]);
  720. i++)
  721. {
  722. if (mmix_reloc_map[i].bfd_reloc_val == code)
  723. return &elf_mmix_howto_table[mmix_reloc_map[i].elf_reloc_val];
  724. }
  725. return NULL;
  726. }
  727. static reloc_howto_type *
  728. bfd_elf64_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  729. const char *r_name)
  730. {
  731. unsigned int i;
  732. for (i = 0;
  733. i < sizeof (elf_mmix_howto_table) / sizeof (elf_mmix_howto_table[0]);
  734. i++)
  735. if (elf_mmix_howto_table[i].name != NULL
  736. && strcasecmp (elf_mmix_howto_table[i].name, r_name) == 0)
  737. return &elf_mmix_howto_table[i];
  738. return NULL;
  739. }
  740. static bool
  741. mmix_elf_new_section_hook (bfd *abfd, asection *sec)
  742. {
  743. if (!sec->used_by_bfd)
  744. {
  745. struct _mmix_elf_section_data *sdata;
  746. size_t amt = sizeof (*sdata);
  747. sdata = bfd_zalloc (abfd, amt);
  748. if (sdata == NULL)
  749. return false;
  750. sec->used_by_bfd = sdata;
  751. }
  752. return _bfd_elf_new_section_hook (abfd, sec);
  753. }
  754. /* This function performs the actual bitfiddling and sanity check for a
  755. final relocation. Each relocation gets its *worst*-case expansion
  756. in size when it arrives here; any reduction in size should have been
  757. caught in linker relaxation earlier. When we get here, the relocation
  758. looks like the smallest instruction with SWYM:s (nop:s) appended to the
  759. max size. We fill in those nop:s.
  760. R_MMIX_GETA: (FIXME: Relaxation should break this up in 1, 2, 3 tetra)
  761. GETA $N,foo
  762. ->
  763. SETL $N,foo & 0xffff
  764. INCML $N,(foo >> 16) & 0xffff
  765. INCMH $N,(foo >> 32) & 0xffff
  766. INCH $N,(foo >> 48) & 0xffff
  767. R_MMIX_CBRANCH: (FIXME: Relaxation should break this up, but
  768. condbranches needing relaxation might be rare enough to not be
  769. worthwhile.)
  770. [P]Bcc $N,foo
  771. ->
  772. [~P]B~cc $N,.+20
  773. SETL $255,foo & ...
  774. INCML ...
  775. INCMH ...
  776. INCH ...
  777. GO $255,$255,0
  778. R_MMIX_PUSHJ: (FIXME: Relaxation...)
  779. PUSHJ $N,foo
  780. ->
  781. SETL $255,foo & ...
  782. INCML ...
  783. INCMH ...
  784. INCH ...
  785. PUSHGO $N,$255,0
  786. R_MMIX_JMP: (FIXME: Relaxation...)
  787. JMP foo
  788. ->
  789. SETL $255,foo & ...
  790. INCML ...
  791. INCMH ...
  792. INCH ...
  793. GO $255,$255,0
  794. R_MMIX_ADDR19 and R_MMIX_ADDR27 are just filled in. */
  795. static bfd_reloc_status_type
  796. mmix_elf_perform_relocation (asection *isec, reloc_howto_type *howto,
  797. void *datap, bfd_vma addr, bfd_vma value,
  798. char **error_message)
  799. {
  800. bfd *abfd = isec->owner;
  801. bfd_reloc_status_type flag = bfd_reloc_ok;
  802. bfd_reloc_status_type r;
  803. int offs = 0;
  804. int reg = 255;
  805. /* The worst case bits are all similar SETL/INCML/INCMH/INCH sequences.
  806. We handle the differences here and the common sequence later. */
  807. switch (howto->type)
  808. {
  809. case R_MMIX_GETA:
  810. offs = 0;
  811. reg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
  812. /* We change to an absolute value. */
  813. value += addr;
  814. break;
  815. case R_MMIX_CBRANCH:
  816. {
  817. int in1 = bfd_get_16 (abfd, (bfd_byte *) datap) << 16;
  818. /* Invert the condition and prediction bit, and set the offset
  819. to five instructions ahead.
  820. We *can* do better if we want to. If the branch is found to be
  821. within limits, we could leave the branch as is; there'll just
  822. be a bunch of NOP:s after it. But we shouldn't see this
  823. sequence often enough that it's worth doing it. */
  824. bfd_put_32 (abfd,
  825. (((in1 ^ ((PRED_INV_BIT | COND_INV_BIT) << 24)) & ~0xffff)
  826. | (24/4)),
  827. (bfd_byte *) datap);
  828. /* Put a "GO $255,$255,0" after the common sequence. */
  829. bfd_put_32 (abfd,
  830. ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24) | 0xffff00,
  831. (bfd_byte *) datap + 20);
  832. /* Common sequence starts at offset 4. */
  833. offs = 4;
  834. /* We change to an absolute value. */
  835. value += addr;
  836. }
  837. break;
  838. case R_MMIX_PUSHJ_STUBBABLE:
  839. /* If the address fits, we're fine. */
  840. if ((value & 3) == 0
  841. /* Note rightshift 0; see R_MMIX_JMP case below. */
  842. && (r = bfd_check_overflow (complain_overflow_signed,
  843. howto->bitsize,
  844. 0,
  845. bfd_arch_bits_per_address (abfd),
  846. value)) == bfd_reloc_ok)
  847. goto pcrel_mmix_reloc_fits;
  848. else
  849. {
  850. bfd_size_type size = isec->rawsize ? isec->rawsize : isec->size;
  851. /* We have the bytes at the PUSHJ insn and need to get the
  852. position for the stub. There's supposed to be room allocated
  853. for the stub. */
  854. bfd_byte *stubcontents
  855. = ((bfd_byte *) datap
  856. - (addr - (isec->output_section->vma + isec->output_offset))
  857. + size
  858. + mmix_elf_section_data (isec)->pjs.stub_offset);
  859. bfd_vma stubaddr;
  860. if (mmix_elf_section_data (isec)->pjs.n_pushj_relocs == 0)
  861. {
  862. /* This shouldn't happen when linking to ELF or mmo, so
  863. this is an attempt to link to "binary", right? We
  864. can't access the output bfd, so we can't verify that
  865. assumption. We only know that the critical
  866. mmix_elf_check_common_relocs has not been called,
  867. which happens when the output format is different
  868. from the input format (and is not mmo). */
  869. if (! mmix_elf_section_data (isec)->has_warned_pushj)
  870. {
  871. /* For the first such error per input section, produce
  872. a verbose message. */
  873. *error_message
  874. = _("invalid input relocation when producing"
  875. " non-ELF, non-mmo format output;"
  876. " please use the objcopy program to convert from"
  877. " ELF or mmo,"
  878. " or assemble using"
  879. " \"-no-expand\" (for gcc, \"-Wa,-no-expand\"");
  880. mmix_elf_section_data (isec)->has_warned_pushj = true;
  881. return bfd_reloc_dangerous;
  882. }
  883. /* For subsequent errors, return this one, which is
  884. rate-limited but looks a little bit different,
  885. hopefully without affecting user-friendliness. */
  886. return bfd_reloc_overflow;
  887. }
  888. /* The address doesn't fit, so redirect the PUSHJ to the
  889. location of the stub. */
  890. r = mmix_elf_perform_relocation (isec,
  891. &elf_mmix_howto_table
  892. [R_MMIX_ADDR19],
  893. datap,
  894. addr,
  895. isec->output_section->vma
  896. + isec->output_offset
  897. + size
  898. + (mmix_elf_section_data (isec)
  899. ->pjs.stub_offset)
  900. - addr,
  901. error_message);
  902. if (r != bfd_reloc_ok)
  903. return r;
  904. stubaddr
  905. = (isec->output_section->vma
  906. + isec->output_offset
  907. + size
  908. + mmix_elf_section_data (isec)->pjs.stub_offset);
  909. /* We generate a simple JMP if that suffices, else the whole 5
  910. insn stub. */
  911. if (bfd_check_overflow (complain_overflow_signed,
  912. elf_mmix_howto_table[R_MMIX_ADDR27].bitsize,
  913. 0,
  914. bfd_arch_bits_per_address (abfd),
  915. addr + value - stubaddr) == bfd_reloc_ok)
  916. {
  917. bfd_put_32 (abfd, JMP_INSN_BYTE << 24, stubcontents);
  918. r = mmix_elf_perform_relocation (isec,
  919. &elf_mmix_howto_table
  920. [R_MMIX_ADDR27],
  921. stubcontents,
  922. stubaddr,
  923. value + addr - stubaddr,
  924. error_message);
  925. mmix_elf_section_data (isec)->pjs.stub_offset += 4;
  926. if (size + mmix_elf_section_data (isec)->pjs.stub_offset
  927. > isec->size)
  928. abort ();
  929. return r;
  930. }
  931. else
  932. {
  933. /* Put a "GO $255,0" after the common sequence. */
  934. bfd_put_32 (abfd,
  935. ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
  936. | 0xff00, (bfd_byte *) stubcontents + 16);
  937. /* Prepare for the general code to set the first part of the
  938. linker stub, and */
  939. value += addr;
  940. datap = stubcontents;
  941. mmix_elf_section_data (isec)->pjs.stub_offset
  942. += MAX_PUSHJ_STUB_SIZE;
  943. }
  944. }
  945. break;
  946. case R_MMIX_PUSHJ:
  947. {
  948. int inreg = bfd_get_8 (abfd, (bfd_byte *) datap + 1);
  949. /* Put a "PUSHGO $N,$255,0" after the common sequence. */
  950. bfd_put_32 (abfd,
  951. ((PUSHGO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
  952. | (inreg << 16)
  953. | 0xff00,
  954. (bfd_byte *) datap + 16);
  955. /* We change to an absolute value. */
  956. value += addr;
  957. }
  958. break;
  959. case R_MMIX_JMP:
  960. /* This one is a little special. If we get here on a non-relaxing
  961. link, and the destination is actually in range, we don't need to
  962. execute the nops.
  963. If so, we fall through to the bit-fiddling relocs.
  964. FIXME: bfd_check_overflow seems broken; the relocation is
  965. rightshifted before testing, so supply a zero rightshift. */
  966. if (! ((value & 3) == 0
  967. && (r = bfd_check_overflow (complain_overflow_signed,
  968. howto->bitsize,
  969. 0,
  970. bfd_arch_bits_per_address (abfd),
  971. value)) == bfd_reloc_ok))
  972. {
  973. /* If the relocation doesn't fit in a JMP, we let the NOP:s be
  974. modified below, and put a "GO $255,$255,0" after the
  975. address-loading sequence. */
  976. bfd_put_32 (abfd,
  977. ((GO_INSN_BYTE | IMM_OFFSET_BIT) << 24)
  978. | 0xffff00,
  979. (bfd_byte *) datap + 16);
  980. /* We change to an absolute value. */
  981. value += addr;
  982. break;
  983. }
  984. /* FALLTHROUGH. */
  985. case R_MMIX_ADDR19:
  986. case R_MMIX_ADDR27:
  987. pcrel_mmix_reloc_fits:
  988. /* These must be in range, or else we emit an error. */
  989. if ((value & 3) == 0
  990. /* Note rightshift 0; see above. */
  991. && (r = bfd_check_overflow (complain_overflow_signed,
  992. howto->bitsize,
  993. 0,
  994. bfd_arch_bits_per_address (abfd),
  995. value)) == bfd_reloc_ok)
  996. {
  997. bfd_vma in1
  998. = bfd_get_32 (abfd, (bfd_byte *) datap);
  999. bfd_vma highbit;
  1000. if ((bfd_signed_vma) value < 0)
  1001. {
  1002. highbit = 1 << 24;
  1003. value += (1 << (howto->bitsize - 1));
  1004. }
  1005. else
  1006. highbit = 0;
  1007. value >>= 2;
  1008. bfd_put_32 (abfd,
  1009. (in1 & howto->src_mask)
  1010. | highbit
  1011. | (value & howto->dst_mask),
  1012. (bfd_byte *) datap);
  1013. return bfd_reloc_ok;
  1014. }
  1015. else
  1016. return bfd_reloc_overflow;
  1017. case R_MMIX_BASE_PLUS_OFFSET:
  1018. {
  1019. struct bpo_reloc_section_info *bpodata
  1020. = mmix_elf_section_data (isec)->bpo.reloc;
  1021. asection *bpo_greg_section;
  1022. struct bpo_greg_section_info *gregdata;
  1023. size_t bpo_index;
  1024. if (bpodata == NULL)
  1025. {
  1026. /* This shouldn't happen when linking to ELF or mmo, so
  1027. this is an attempt to link to "binary", right? We
  1028. can't access the output bfd, so we can't verify that
  1029. assumption. We only know that the critical
  1030. mmix_elf_check_common_relocs has not been called, which
  1031. happens when the output format is different from the
  1032. input format (and is not mmo). */
  1033. if (! mmix_elf_section_data (isec)->has_warned_bpo)
  1034. {
  1035. /* For the first such error per input section, produce
  1036. a verbose message. */
  1037. *error_message
  1038. = _("invalid input relocation when producing"
  1039. " non-ELF, non-mmo format output;"
  1040. " please use the objcopy program to convert from"
  1041. " ELF or mmo,"
  1042. " or compile using the gcc-option"
  1043. " \"-mno-base-addresses\".");
  1044. mmix_elf_section_data (isec)->has_warned_bpo = true;
  1045. return bfd_reloc_dangerous;
  1046. }
  1047. /* For subsequent errors, return this one, which is
  1048. rate-limited but looks a little bit different,
  1049. hopefully without affecting user-friendliness. */
  1050. return bfd_reloc_overflow;
  1051. }
  1052. bpo_greg_section = bpodata->bpo_greg_section;
  1053. gregdata = mmix_elf_section_data (bpo_greg_section)->bpo.greg;
  1054. bpo_index = gregdata->bpo_reloc_indexes[bpodata->bpo_index++];
  1055. /* A consistency check: The value we now have in "relocation" must
  1056. be the same as the value we stored for that relocation. It
  1057. doesn't cost much, so can be left in at all times. */
  1058. if (value != gregdata->reloc_request[bpo_index].value)
  1059. {
  1060. _bfd_error_handler
  1061. /* xgettext:c-format */
  1062. (_("%pB: Internal inconsistency error for value for\n\
  1063. linker-allocated global register: linked: %#" PRIx64 " != relaxed: %#" PRIx64 ""),
  1064. isec->owner,
  1065. (uint64_t) value,
  1066. (uint64_t) gregdata->reloc_request[bpo_index].value);
  1067. bfd_set_error (bfd_error_bad_value);
  1068. return bfd_reloc_overflow;
  1069. }
  1070. /* Then store the register number and offset for that register
  1071. into datap and datap + 1 respectively. */
  1072. bfd_put_8 (abfd,
  1073. gregdata->reloc_request[bpo_index].regindex
  1074. + bpo_greg_section->output_section->vma / 8,
  1075. datap);
  1076. bfd_put_8 (abfd,
  1077. gregdata->reloc_request[bpo_index].offset,
  1078. ((unsigned char *) datap) + 1);
  1079. return bfd_reloc_ok;
  1080. }
  1081. case R_MMIX_REG_OR_BYTE:
  1082. case R_MMIX_REG:
  1083. if (value > 255)
  1084. return bfd_reloc_overflow;
  1085. bfd_put_8 (abfd, value, datap);
  1086. return bfd_reloc_ok;
  1087. default:
  1088. BAD_CASE (howto->type);
  1089. }
  1090. /* This code adds the common SETL/INCML/INCMH/INCH worst-case
  1091. sequence. */
  1092. /* Lowest two bits must be 0. We return bfd_reloc_overflow for
  1093. everything that looks strange. */
  1094. if (value & 3)
  1095. flag = bfd_reloc_overflow;
  1096. bfd_put_32 (abfd,
  1097. (SETL_INSN_BYTE << 24) | (value & 0xffff) | (reg << 16),
  1098. (bfd_byte *) datap + offs);
  1099. bfd_put_32 (abfd,
  1100. (INCML_INSN_BYTE << 24) | ((value >> 16) & 0xffff) | (reg << 16),
  1101. (bfd_byte *) datap + offs + 4);
  1102. bfd_put_32 (abfd,
  1103. (INCMH_INSN_BYTE << 24) | ((value >> 32) & 0xffff) | (reg << 16),
  1104. (bfd_byte *) datap + offs + 8);
  1105. bfd_put_32 (abfd,
  1106. (INCH_INSN_BYTE << 24) | ((value >> 48) & 0xffff) | (reg << 16),
  1107. (bfd_byte *) datap + offs + 12);
  1108. return flag;
  1109. }
  1110. /* Set the howto pointer for an MMIX ELF reloc (type RELA). */
  1111. static bool
  1112. mmix_info_to_howto_rela (bfd *abfd,
  1113. arelent *cache_ptr,
  1114. Elf_Internal_Rela *dst)
  1115. {
  1116. unsigned int r_type;
  1117. r_type = ELF64_R_TYPE (dst->r_info);
  1118. if (r_type >= (unsigned int) R_MMIX_max)
  1119. {
  1120. /* xgettext:c-format */
  1121. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  1122. abfd, r_type);
  1123. bfd_set_error (bfd_error_bad_value);
  1124. return false;
  1125. }
  1126. cache_ptr->howto = &elf_mmix_howto_table[r_type];
  1127. return true;
  1128. }
  1129. /* Any MMIX-specific relocation gets here at assembly time or when linking
  1130. to other formats (such as mmo); this is the relocation function from
  1131. the reloc_table. We don't get here for final pure ELF linking. */
  1132. static bfd_reloc_status_type
  1133. mmix_elf_reloc (bfd *abfd,
  1134. arelent *reloc_entry,
  1135. asymbol *symbol,
  1136. void * data,
  1137. asection *input_section,
  1138. bfd *output_bfd,
  1139. char **error_message)
  1140. {
  1141. bfd_vma relocation;
  1142. bfd_reloc_status_type r;
  1143. asection *reloc_target_output_section;
  1144. bfd_reloc_status_type flag = bfd_reloc_ok;
  1145. bfd_vma output_base = 0;
  1146. r = bfd_elf_generic_reloc (abfd, reloc_entry, symbol, data,
  1147. input_section, output_bfd, error_message);
  1148. /* If that was all that was needed (i.e. this isn't a final link, only
  1149. some segment adjustments), we're done. */
  1150. if (r != bfd_reloc_continue)
  1151. return r;
  1152. if (bfd_is_und_section (symbol->section)
  1153. && (symbol->flags & BSF_WEAK) == 0
  1154. && output_bfd == (bfd *) NULL)
  1155. return bfd_reloc_undefined;
  1156. /* Is the address of the relocation really within the section? */
  1157. if (reloc_entry->address > bfd_get_section_limit (abfd, input_section))
  1158. return bfd_reloc_outofrange;
  1159. /* Work out which section the relocation is targeted at and the
  1160. initial relocation command value. */
  1161. /* Get symbol value. (Common symbols are special.) */
  1162. if (bfd_is_com_section (symbol->section))
  1163. relocation = 0;
  1164. else
  1165. relocation = symbol->value;
  1166. reloc_target_output_section = bfd_asymbol_section (symbol)->output_section;
  1167. /* Here the variable relocation holds the final address of the symbol we
  1168. are relocating against, plus any addend. */
  1169. if (output_bfd)
  1170. output_base = 0;
  1171. else
  1172. output_base = reloc_target_output_section->vma;
  1173. relocation += output_base + symbol->section->output_offset;
  1174. if (output_bfd != (bfd *) NULL)
  1175. {
  1176. /* Add in supplied addend. */
  1177. relocation += reloc_entry->addend;
  1178. /* This is a partial relocation, and we want to apply the
  1179. relocation to the reloc entry rather than the raw data.
  1180. Modify the reloc inplace to reflect what we now know. */
  1181. reloc_entry->addend = relocation;
  1182. reloc_entry->address += input_section->output_offset;
  1183. return flag;
  1184. }
  1185. return mmix_final_link_relocate (reloc_entry->howto, input_section,
  1186. data, reloc_entry->address,
  1187. reloc_entry->addend, relocation,
  1188. bfd_asymbol_name (symbol),
  1189. reloc_target_output_section,
  1190. error_message);
  1191. }
  1192. /* Relocate an MMIX ELF section. Modified from elf32-fr30.c; look to it
  1193. for guidance if you're thinking of copying this. */
  1194. static int
  1195. mmix_elf_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
  1196. struct bfd_link_info *info,
  1197. bfd *input_bfd,
  1198. asection *input_section,
  1199. bfd_byte *contents,
  1200. Elf_Internal_Rela *relocs,
  1201. Elf_Internal_Sym *local_syms,
  1202. asection **local_sections)
  1203. {
  1204. Elf_Internal_Shdr *symtab_hdr;
  1205. struct elf_link_hash_entry **sym_hashes;
  1206. Elf_Internal_Rela *rel;
  1207. Elf_Internal_Rela *relend;
  1208. bfd_size_type size;
  1209. size_t pjsno = 0;
  1210. size = input_section->rawsize ? input_section->rawsize : input_section->size;
  1211. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  1212. sym_hashes = elf_sym_hashes (input_bfd);
  1213. relend = relocs + input_section->reloc_count;
  1214. /* Zero the stub area before we start. */
  1215. if (input_section->rawsize != 0
  1216. && input_section->size > input_section->rawsize)
  1217. memset (contents + input_section->rawsize, 0,
  1218. input_section->size - input_section->rawsize);
  1219. for (rel = relocs; rel < relend; rel ++)
  1220. {
  1221. reloc_howto_type *howto;
  1222. unsigned long r_symndx;
  1223. Elf_Internal_Sym *sym;
  1224. asection *sec;
  1225. struct elf_link_hash_entry *h;
  1226. bfd_vma relocation;
  1227. bfd_reloc_status_type r;
  1228. const char *name = NULL;
  1229. int r_type;
  1230. bool undefined_signalled = false;
  1231. r_type = ELF64_R_TYPE (rel->r_info);
  1232. if (r_type == R_MMIX_GNU_VTINHERIT
  1233. || r_type == R_MMIX_GNU_VTENTRY)
  1234. continue;
  1235. r_symndx = ELF64_R_SYM (rel->r_info);
  1236. howto = elf_mmix_howto_table + ELF64_R_TYPE (rel->r_info);
  1237. h = NULL;
  1238. sym = NULL;
  1239. sec = NULL;
  1240. if (r_symndx < symtab_hdr->sh_info)
  1241. {
  1242. sym = local_syms + r_symndx;
  1243. sec = local_sections [r_symndx];
  1244. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  1245. name = bfd_elf_string_from_elf_section (input_bfd,
  1246. symtab_hdr->sh_link,
  1247. sym->st_name);
  1248. if (name == NULL)
  1249. name = bfd_section_name (sec);
  1250. }
  1251. else
  1252. {
  1253. bool unresolved_reloc, ignored;
  1254. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  1255. r_symndx, symtab_hdr, sym_hashes,
  1256. h, sec, relocation,
  1257. unresolved_reloc, undefined_signalled,
  1258. ignored);
  1259. name = h->root.root.string;
  1260. }
  1261. if (sec != NULL && discarded_section (sec))
  1262. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  1263. rel, 1, relend, howto, 0, contents);
  1264. if (bfd_link_relocatable (info))
  1265. {
  1266. /* This is a relocatable link. For most relocs we don't have to
  1267. change anything, unless the reloc is against a section
  1268. symbol, in which case we have to adjust according to where
  1269. the section symbol winds up in the output section. */
  1270. if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  1271. rel->r_addend += sec->output_offset;
  1272. /* For PUSHJ stub relocs however, we may need to change the
  1273. reloc and the section contents, if the reloc doesn't reach
  1274. beyond the end of the output section and previous stubs.
  1275. Then we change the section contents to be a PUSHJ to the end
  1276. of the input section plus stubs (we can do that without using
  1277. a reloc), and then we change the reloc to be a R_MMIX_PUSHJ
  1278. at the stub location. */
  1279. if (r_type == R_MMIX_PUSHJ_STUBBABLE)
  1280. {
  1281. /* We've already checked whether we need a stub; use that
  1282. knowledge. */
  1283. if (mmix_elf_section_data (input_section)->pjs.stub_size[pjsno]
  1284. != 0)
  1285. {
  1286. Elf_Internal_Rela relcpy;
  1287. if (mmix_elf_section_data (input_section)
  1288. ->pjs.stub_size[pjsno] != MAX_PUSHJ_STUB_SIZE)
  1289. abort ();
  1290. /* There's already a PUSHJ insn there, so just fill in
  1291. the offset bits to the stub. */
  1292. if (mmix_final_link_relocate (elf_mmix_howto_table
  1293. + R_MMIX_ADDR19,
  1294. input_section,
  1295. contents,
  1296. rel->r_offset,
  1297. 0,
  1298. input_section
  1299. ->output_section->vma
  1300. + input_section->output_offset
  1301. + size
  1302. + mmix_elf_section_data (input_section)
  1303. ->pjs.stub_offset,
  1304. NULL, NULL, NULL) != bfd_reloc_ok)
  1305. return false;
  1306. /* Put a JMP insn at the stub; it goes with the
  1307. R_MMIX_JMP reloc. */
  1308. bfd_put_32 (output_bfd, JMP_INSN_BYTE << 24,
  1309. contents
  1310. + size
  1311. + mmix_elf_section_data (input_section)
  1312. ->pjs.stub_offset);
  1313. /* Change the reloc to be at the stub, and to a full
  1314. R_MMIX_JMP reloc. */
  1315. rel->r_info = ELF64_R_INFO (r_symndx, R_MMIX_JMP);
  1316. rel->r_offset
  1317. = (size
  1318. + mmix_elf_section_data (input_section)
  1319. ->pjs.stub_offset);
  1320. mmix_elf_section_data (input_section)->pjs.stub_offset
  1321. += MAX_PUSHJ_STUB_SIZE;
  1322. /* Shift this reloc to the end of the relocs to maintain
  1323. the r_offset sorted reloc order. */
  1324. relcpy = *rel;
  1325. memmove (rel, rel + 1, (char *) relend - (char *) rel);
  1326. relend[-1] = relcpy;
  1327. /* Back up one reloc, or else we'd skip the next reloc
  1328. in turn. */
  1329. rel--;
  1330. }
  1331. pjsno++;
  1332. }
  1333. continue;
  1334. }
  1335. r = mmix_final_link_relocate (howto, input_section,
  1336. contents, rel->r_offset,
  1337. rel->r_addend, relocation, name, sec, NULL);
  1338. if (r != bfd_reloc_ok)
  1339. {
  1340. const char * msg = (const char *) NULL;
  1341. switch (r)
  1342. {
  1343. case bfd_reloc_overflow:
  1344. info->callbacks->reloc_overflow
  1345. (info, (h ? &h->root : NULL), name, howto->name,
  1346. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  1347. break;
  1348. case bfd_reloc_undefined:
  1349. /* We may have sent this message above. */
  1350. if (! undefined_signalled)
  1351. info->callbacks->undefined_symbol
  1352. (info, name, input_bfd, input_section, rel->r_offset, true);
  1353. undefined_signalled = true;
  1354. break;
  1355. case bfd_reloc_outofrange:
  1356. msg = _("internal error: out of range error");
  1357. break;
  1358. case bfd_reloc_notsupported:
  1359. msg = _("internal error: unsupported relocation error");
  1360. break;
  1361. case bfd_reloc_dangerous:
  1362. msg = _("internal error: dangerous relocation");
  1363. break;
  1364. default:
  1365. msg = _("internal error: unknown error");
  1366. break;
  1367. }
  1368. if (msg)
  1369. (*info->callbacks->warning) (info, msg, name, input_bfd,
  1370. input_section, rel->r_offset);
  1371. }
  1372. }
  1373. return true;
  1374. }
  1375. /* Perform a single relocation. By default we use the standard BFD
  1376. routines. A few relocs we have to do ourselves. */
  1377. static bfd_reloc_status_type
  1378. mmix_final_link_relocate (reloc_howto_type *howto, asection *input_section,
  1379. bfd_byte *contents, bfd_vma r_offset,
  1380. bfd_signed_vma r_addend, bfd_vma relocation,
  1381. const char *symname, asection *symsec,
  1382. char **error_message)
  1383. {
  1384. bfd_reloc_status_type r = bfd_reloc_ok;
  1385. bfd_vma addr
  1386. = (input_section->output_section->vma
  1387. + input_section->output_offset
  1388. + r_offset);
  1389. bfd_signed_vma srel
  1390. = (bfd_signed_vma) relocation + r_addend;
  1391. switch (howto->type)
  1392. {
  1393. /* All these are PC-relative. */
  1394. case R_MMIX_PUSHJ_STUBBABLE:
  1395. case R_MMIX_PUSHJ:
  1396. case R_MMIX_CBRANCH:
  1397. case R_MMIX_ADDR19:
  1398. case R_MMIX_GETA:
  1399. case R_MMIX_ADDR27:
  1400. case R_MMIX_JMP:
  1401. contents += r_offset;
  1402. srel -= (input_section->output_section->vma
  1403. + input_section->output_offset
  1404. + r_offset);
  1405. r = mmix_elf_perform_relocation (input_section, howto, contents,
  1406. addr, srel, error_message);
  1407. break;
  1408. case R_MMIX_BASE_PLUS_OFFSET:
  1409. if (symsec == NULL)
  1410. return bfd_reloc_undefined;
  1411. /* Check that we're not relocating against a register symbol. */
  1412. if (strcmp (bfd_section_name (symsec),
  1413. MMIX_REG_CONTENTS_SECTION_NAME) == 0
  1414. || strcmp (bfd_section_name (symsec),
  1415. MMIX_REG_SECTION_NAME) == 0)
  1416. {
  1417. /* Note: This is separated out into two messages in order
  1418. to ease the translation into other languages. */
  1419. if (symname == NULL || *symname == 0)
  1420. _bfd_error_handler
  1421. /* xgettext:c-format */
  1422. (_("%pB: base-plus-offset relocation against register symbol:"
  1423. " (unknown) in %pA"),
  1424. input_section->owner, symsec);
  1425. else
  1426. _bfd_error_handler
  1427. /* xgettext:c-format */
  1428. (_("%pB: base-plus-offset relocation against register symbol:"
  1429. " %s in %pA"),
  1430. input_section->owner, symname, symsec);
  1431. return bfd_reloc_overflow;
  1432. }
  1433. goto do_mmix_reloc;
  1434. case R_MMIX_REG_OR_BYTE:
  1435. case R_MMIX_REG:
  1436. /* For now, we handle these alike. They must refer to an register
  1437. symbol, which is either relative to the register section and in
  1438. the range 0..255, or is in the register contents section with vma
  1439. regno * 8. */
  1440. /* FIXME: A better way to check for reg contents section?
  1441. FIXME: Postpone section->scaling to mmix_elf_perform_relocation? */
  1442. if (symsec == NULL)
  1443. return bfd_reloc_undefined;
  1444. if (strcmp (bfd_section_name (symsec),
  1445. MMIX_REG_CONTENTS_SECTION_NAME) == 0)
  1446. {
  1447. if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
  1448. {
  1449. /* The bfd_reloc_outofrange return value, though intuitively
  1450. a better value, will not get us an error. */
  1451. return bfd_reloc_overflow;
  1452. }
  1453. srel /= 8;
  1454. }
  1455. else if (strcmp (bfd_section_name (symsec),
  1456. MMIX_REG_SECTION_NAME) == 0)
  1457. {
  1458. if (srel < 0 || srel > 255)
  1459. /* The bfd_reloc_outofrange return value, though intuitively a
  1460. better value, will not get us an error. */
  1461. return bfd_reloc_overflow;
  1462. }
  1463. else
  1464. {
  1465. /* Note: This is separated out into two messages in order
  1466. to ease the translation into other languages. */
  1467. if (symname == NULL || *symname == 0)
  1468. _bfd_error_handler
  1469. /* xgettext:c-format */
  1470. (_("%pB: register relocation against non-register symbol:"
  1471. " (unknown) in %pA"),
  1472. input_section->owner, symsec);
  1473. else
  1474. _bfd_error_handler
  1475. /* xgettext:c-format */
  1476. (_("%pB: register relocation against non-register symbol:"
  1477. " %s in %pA"),
  1478. input_section->owner, symname, symsec);
  1479. /* The bfd_reloc_outofrange return value, though intuitively a
  1480. better value, will not get us an error. */
  1481. return bfd_reloc_overflow;
  1482. }
  1483. do_mmix_reloc:
  1484. contents += r_offset;
  1485. r = mmix_elf_perform_relocation (input_section, howto, contents,
  1486. addr, srel, error_message);
  1487. break;
  1488. case R_MMIX_LOCAL:
  1489. /* This isn't a real relocation, it's just an assertion that the
  1490. final relocation value corresponds to a local register. We
  1491. ignore the actual relocation; nothing is changed. */
  1492. {
  1493. asection *regsec
  1494. = bfd_get_section_by_name (input_section->output_section->owner,
  1495. MMIX_REG_CONTENTS_SECTION_NAME);
  1496. bfd_vma first_global;
  1497. /* Check that this is an absolute value, or a reference to the
  1498. register contents section or the register (symbol) section.
  1499. Absolute numbers can get here as undefined section. Undefined
  1500. symbols are signalled elsewhere, so there's no conflict in us
  1501. accidentally handling it. */
  1502. if (!bfd_is_abs_section (symsec)
  1503. && !bfd_is_und_section (symsec)
  1504. && strcmp (bfd_section_name (symsec),
  1505. MMIX_REG_CONTENTS_SECTION_NAME) != 0
  1506. && strcmp (bfd_section_name (symsec),
  1507. MMIX_REG_SECTION_NAME) != 0)
  1508. {
  1509. _bfd_error_handler
  1510. (_("%pB: directive LOCAL valid only with a register or absolute value"),
  1511. input_section->owner);
  1512. return bfd_reloc_overflow;
  1513. }
  1514. /* If we don't have a register contents section, then $255 is the
  1515. first global register. */
  1516. if (regsec == NULL)
  1517. first_global = 255;
  1518. else
  1519. {
  1520. first_global = bfd_section_vma (regsec) / 8;
  1521. if (strcmp (bfd_section_name (symsec),
  1522. MMIX_REG_CONTENTS_SECTION_NAME) == 0)
  1523. {
  1524. if ((srel & 7) != 0 || srel < 32*8 || srel > 255*8)
  1525. /* The bfd_reloc_outofrange return value, though
  1526. intuitively a better value, will not get us an error. */
  1527. return bfd_reloc_overflow;
  1528. srel /= 8;
  1529. }
  1530. }
  1531. if ((bfd_vma) srel >= first_global)
  1532. {
  1533. /* FIXME: Better error message. */
  1534. _bfd_error_handler
  1535. /* xgettext:c-format */
  1536. (_("%pB: LOCAL directive: "
  1537. "register $%" PRId64 " is not a local register;"
  1538. " first global register is $%" PRId64),
  1539. input_section->owner, (int64_t) srel, (int64_t) first_global);
  1540. return bfd_reloc_overflow;
  1541. }
  1542. }
  1543. r = bfd_reloc_ok;
  1544. break;
  1545. default:
  1546. r = _bfd_final_link_relocate (howto, input_section->owner, input_section,
  1547. contents, r_offset,
  1548. relocation, r_addend);
  1549. }
  1550. return r;
  1551. }
  1552. /* Return the section that should be marked against GC for a given
  1553. relocation. */
  1554. static asection *
  1555. mmix_elf_gc_mark_hook (asection *sec,
  1556. struct bfd_link_info *info,
  1557. Elf_Internal_Rela *rel,
  1558. struct elf_link_hash_entry *h,
  1559. Elf_Internal_Sym *sym)
  1560. {
  1561. if (h != NULL)
  1562. switch (ELF64_R_TYPE (rel->r_info))
  1563. {
  1564. case R_MMIX_GNU_VTINHERIT:
  1565. case R_MMIX_GNU_VTENTRY:
  1566. return NULL;
  1567. }
  1568. return _bfd_elf_gc_mark_hook (sec, info, rel, h, sym);
  1569. }
  1570. /* Sort register relocs to come before expanding relocs. */
  1571. static int
  1572. mmix_elf_sort_relocs (const void * p1, const void * p2)
  1573. {
  1574. const Elf_Internal_Rela *r1 = (const Elf_Internal_Rela *) p1;
  1575. const Elf_Internal_Rela *r2 = (const Elf_Internal_Rela *) p2;
  1576. int r1_is_reg, r2_is_reg;
  1577. /* Sort primarily on r_offset & ~3, so relocs are done to consecutive
  1578. insns. */
  1579. if ((r1->r_offset & ~(bfd_vma) 3) > (r2->r_offset & ~(bfd_vma) 3))
  1580. return 1;
  1581. else if ((r1->r_offset & ~(bfd_vma) 3) < (r2->r_offset & ~(bfd_vma) 3))
  1582. return -1;
  1583. r1_is_reg
  1584. = (ELF64_R_TYPE (r1->r_info) == R_MMIX_REG_OR_BYTE
  1585. || ELF64_R_TYPE (r1->r_info) == R_MMIX_REG);
  1586. r2_is_reg
  1587. = (ELF64_R_TYPE (r2->r_info) == R_MMIX_REG_OR_BYTE
  1588. || ELF64_R_TYPE (r2->r_info) == R_MMIX_REG);
  1589. if (r1_is_reg != r2_is_reg)
  1590. return r2_is_reg - r1_is_reg;
  1591. /* Neither or both are register relocs. Then sort on full offset. */
  1592. if (r1->r_offset > r2->r_offset)
  1593. return 1;
  1594. else if (r1->r_offset < r2->r_offset)
  1595. return -1;
  1596. return 0;
  1597. }
  1598. /* Subset of mmix_elf_check_relocs, common to ELF and mmo linking. */
  1599. static bool
  1600. mmix_elf_check_common_relocs (bfd *abfd,
  1601. struct bfd_link_info *info,
  1602. asection *sec,
  1603. const Elf_Internal_Rela *relocs)
  1604. {
  1605. bfd *bpo_greg_owner = NULL;
  1606. asection *allocated_gregs_section = NULL;
  1607. struct bpo_greg_section_info *gregdata = NULL;
  1608. struct bpo_reloc_section_info *bpodata = NULL;
  1609. const Elf_Internal_Rela *rel;
  1610. const Elf_Internal_Rela *rel_end;
  1611. /* We currently have to abuse this COFF-specific member, since there's
  1612. no target-machine-dedicated member. There's no alternative outside
  1613. the bfd_link_info struct; we can't specialize a hash-table since
  1614. they're different between ELF and mmo. */
  1615. bpo_greg_owner = (bfd *) info->base_file;
  1616. rel_end = relocs + sec->reloc_count;
  1617. for (rel = relocs; rel < rel_end; rel++)
  1618. {
  1619. switch (ELF64_R_TYPE (rel->r_info))
  1620. {
  1621. /* This relocation causes a GREG allocation. We need to count
  1622. them, and we need to create a section for them, so we need an
  1623. object to fake as the owner of that section. We can't use
  1624. the ELF dynobj for this, since the ELF bits assume lots of
  1625. DSO-related stuff if that member is non-NULL. */
  1626. case R_MMIX_BASE_PLUS_OFFSET:
  1627. /* We don't do anything with this reloc for a relocatable link. */
  1628. if (bfd_link_relocatable (info))
  1629. break;
  1630. if (bpo_greg_owner == NULL)
  1631. {
  1632. bpo_greg_owner = abfd;
  1633. info->base_file = bpo_greg_owner;
  1634. }
  1635. if (allocated_gregs_section == NULL)
  1636. allocated_gregs_section
  1637. = bfd_get_section_by_name (bpo_greg_owner,
  1638. MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
  1639. if (allocated_gregs_section == NULL)
  1640. {
  1641. allocated_gregs_section
  1642. = bfd_make_section_with_flags (bpo_greg_owner,
  1643. MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME,
  1644. (SEC_HAS_CONTENTS
  1645. | SEC_IN_MEMORY
  1646. | SEC_LINKER_CREATED));
  1647. /* Setting both SEC_ALLOC and SEC_LOAD means the section is
  1648. treated like any other section, and we'd get errors for
  1649. address overlap with the text section. Let's set none of
  1650. those flags, as that is what currently happens for usual
  1651. GREG allocations, and that works. */
  1652. if (allocated_gregs_section == NULL
  1653. || !bfd_set_section_alignment (allocated_gregs_section, 3))
  1654. return false;
  1655. gregdata = (struct bpo_greg_section_info *)
  1656. bfd_zalloc (bpo_greg_owner, sizeof (struct bpo_greg_section_info));
  1657. if (gregdata == NULL)
  1658. return false;
  1659. mmix_elf_section_data (allocated_gregs_section)->bpo.greg
  1660. = gregdata;
  1661. }
  1662. else if (gregdata == NULL)
  1663. gregdata
  1664. = mmix_elf_section_data (allocated_gregs_section)->bpo.greg;
  1665. /* Get ourselves some auxiliary info for the BPO-relocs. */
  1666. if (bpodata == NULL)
  1667. {
  1668. /* No use doing a separate iteration pass to find the upper
  1669. limit - just use the number of relocs. */
  1670. bpodata = (struct bpo_reloc_section_info *)
  1671. bfd_alloc (bpo_greg_owner,
  1672. sizeof (struct bpo_reloc_section_info)
  1673. * (sec->reloc_count + 1));
  1674. if (bpodata == NULL)
  1675. return false;
  1676. mmix_elf_section_data (sec)->bpo.reloc = bpodata;
  1677. bpodata->first_base_plus_offset_reloc
  1678. = bpodata->bpo_index
  1679. = gregdata->n_max_bpo_relocs;
  1680. bpodata->bpo_greg_section
  1681. = allocated_gregs_section;
  1682. bpodata->n_bpo_relocs_this_section = 0;
  1683. }
  1684. bpodata->n_bpo_relocs_this_section++;
  1685. gregdata->n_max_bpo_relocs++;
  1686. /* We don't get another chance to set this before GC; we've not
  1687. set up any hook that runs before GC. */
  1688. gregdata->n_bpo_relocs
  1689. = gregdata->n_max_bpo_relocs;
  1690. break;
  1691. case R_MMIX_PUSHJ_STUBBABLE:
  1692. mmix_elf_section_data (sec)->pjs.n_pushj_relocs++;
  1693. break;
  1694. }
  1695. }
  1696. /* Allocate per-reloc stub storage and initialize it to the max stub
  1697. size. */
  1698. if (mmix_elf_section_data (sec)->pjs.n_pushj_relocs != 0)
  1699. {
  1700. size_t i;
  1701. mmix_elf_section_data (sec)->pjs.stub_size
  1702. = bfd_alloc (abfd, mmix_elf_section_data (sec)->pjs.n_pushj_relocs
  1703. * sizeof (mmix_elf_section_data (sec)
  1704. ->pjs.stub_size[0]));
  1705. if (mmix_elf_section_data (sec)->pjs.stub_size == NULL)
  1706. return false;
  1707. for (i = 0; i < mmix_elf_section_data (sec)->pjs.n_pushj_relocs; i++)
  1708. mmix_elf_section_data (sec)->pjs.stub_size[i] = MAX_PUSHJ_STUB_SIZE;
  1709. }
  1710. return true;
  1711. }
  1712. /* Look through the relocs for a section during the first phase. */
  1713. static bool
  1714. mmix_elf_check_relocs (bfd *abfd,
  1715. struct bfd_link_info *info,
  1716. asection *sec,
  1717. const Elf_Internal_Rela *relocs)
  1718. {
  1719. Elf_Internal_Shdr *symtab_hdr;
  1720. struct elf_link_hash_entry **sym_hashes;
  1721. const Elf_Internal_Rela *rel;
  1722. const Elf_Internal_Rela *rel_end;
  1723. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1724. sym_hashes = elf_sym_hashes (abfd);
  1725. /* First we sort the relocs so that any register relocs come before
  1726. expansion-relocs to the same insn. FIXME: Not done for mmo. */
  1727. qsort ((void *) relocs, sec->reloc_count, sizeof (Elf_Internal_Rela),
  1728. mmix_elf_sort_relocs);
  1729. /* Do the common part. */
  1730. if (!mmix_elf_check_common_relocs (abfd, info, sec, relocs))
  1731. return false;
  1732. if (bfd_link_relocatable (info))
  1733. return true;
  1734. rel_end = relocs + sec->reloc_count;
  1735. for (rel = relocs; rel < rel_end; rel++)
  1736. {
  1737. struct elf_link_hash_entry *h;
  1738. unsigned long r_symndx;
  1739. r_symndx = ELF64_R_SYM (rel->r_info);
  1740. if (r_symndx < symtab_hdr->sh_info)
  1741. h = NULL;
  1742. else
  1743. {
  1744. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1745. while (h->root.type == bfd_link_hash_indirect
  1746. || h->root.type == bfd_link_hash_warning)
  1747. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1748. }
  1749. switch (ELF64_R_TYPE (rel->r_info))
  1750. {
  1751. /* This relocation describes the C++ object vtable hierarchy.
  1752. Reconstruct it for later use during GC. */
  1753. case R_MMIX_GNU_VTINHERIT:
  1754. if (!bfd_elf_gc_record_vtinherit (abfd, sec, h, rel->r_offset))
  1755. return false;
  1756. break;
  1757. /* This relocation describes which C++ vtable entries are actually
  1758. used. Record for later use during GC. */
  1759. case R_MMIX_GNU_VTENTRY:
  1760. if (!bfd_elf_gc_record_vtentry (abfd, sec, h, rel->r_addend))
  1761. return false;
  1762. break;
  1763. }
  1764. }
  1765. return true;
  1766. }
  1767. /* Wrapper for mmix_elf_check_common_relocs, called when linking to mmo.
  1768. Copied from elf_link_add_object_symbols. */
  1769. bool
  1770. _bfd_mmix_check_all_relocs (bfd *abfd, struct bfd_link_info *info)
  1771. {
  1772. asection *o;
  1773. for (o = abfd->sections; o != NULL; o = o->next)
  1774. {
  1775. Elf_Internal_Rela *internal_relocs;
  1776. bool ok;
  1777. if ((o->flags & SEC_RELOC) == 0
  1778. || o->reloc_count == 0
  1779. || ((info->strip == strip_all || info->strip == strip_debugger)
  1780. && (o->flags & SEC_DEBUGGING) != 0)
  1781. || bfd_is_abs_section (o->output_section))
  1782. continue;
  1783. internal_relocs
  1784. = _bfd_elf_link_read_relocs (abfd, o, NULL,
  1785. (Elf_Internal_Rela *) NULL,
  1786. info->keep_memory);
  1787. if (internal_relocs == NULL)
  1788. return false;
  1789. ok = mmix_elf_check_common_relocs (abfd, info, o, internal_relocs);
  1790. if (! info->keep_memory)
  1791. free (internal_relocs);
  1792. if (! ok)
  1793. return false;
  1794. }
  1795. return true;
  1796. }
  1797. /* Change symbols relative to the reg contents section to instead be to
  1798. the register section, and scale them down to correspond to the register
  1799. number. */
  1800. static int
  1801. mmix_elf_link_output_symbol_hook (struct bfd_link_info *info ATTRIBUTE_UNUSED,
  1802. const char *name ATTRIBUTE_UNUSED,
  1803. Elf_Internal_Sym *sym,
  1804. asection *input_sec,
  1805. struct elf_link_hash_entry *h ATTRIBUTE_UNUSED)
  1806. {
  1807. if (input_sec != NULL
  1808. && input_sec->name != NULL
  1809. && ELF_ST_TYPE (sym->st_info) != STT_SECTION
  1810. && strcmp (input_sec->name, MMIX_REG_CONTENTS_SECTION_NAME) == 0)
  1811. {
  1812. sym->st_value /= 8;
  1813. sym->st_shndx = SHN_REGISTER;
  1814. }
  1815. return 1;
  1816. }
  1817. /* We fake a register section that holds values that are register numbers.
  1818. Having a SHN_REGISTER and register section translates better to other
  1819. formats (e.g. mmo) than for example a STT_REGISTER attribute.
  1820. This section faking is based on a construct in elf32-mips.c. */
  1821. static asection mmix_elf_reg_section;
  1822. static const asymbol mmix_elf_reg_section_symbol =
  1823. GLOBAL_SYM_INIT (MMIX_REG_SECTION_NAME, &mmix_elf_reg_section);
  1824. static asection mmix_elf_reg_section =
  1825. BFD_FAKE_SECTION (mmix_elf_reg_section, &mmix_elf_reg_section_symbol,
  1826. MMIX_REG_SECTION_NAME, 0, SEC_NO_FLAGS);
  1827. /* Handle the special section numbers that a symbol may use. */
  1828. void
  1829. mmix_elf_symbol_processing (bfd *abfd ATTRIBUTE_UNUSED, asymbol *asym)
  1830. {
  1831. elf_symbol_type *elfsym;
  1832. elfsym = (elf_symbol_type *) asym;
  1833. switch (elfsym->internal_elf_sym.st_shndx)
  1834. {
  1835. case SHN_REGISTER:
  1836. asym->section = &mmix_elf_reg_section;
  1837. break;
  1838. default:
  1839. break;
  1840. }
  1841. }
  1842. /* Given a BFD section, try to locate the corresponding ELF section
  1843. index. */
  1844. static bool
  1845. mmix_elf_section_from_bfd_section (bfd * abfd ATTRIBUTE_UNUSED,
  1846. asection * sec,
  1847. int * retval)
  1848. {
  1849. if (strcmp (bfd_section_name (sec), MMIX_REG_SECTION_NAME) == 0)
  1850. *retval = SHN_REGISTER;
  1851. else
  1852. return false;
  1853. return true;
  1854. }
  1855. /* Hook called by the linker routine which adds symbols from an object
  1856. file. We must handle the special SHN_REGISTER section number here.
  1857. We also check that we only have *one* each of the section-start
  1858. symbols, since otherwise having two with the same value would cause
  1859. them to be "merged", but with the contents serialized. */
  1860. static bool
  1861. mmix_elf_add_symbol_hook (bfd *abfd,
  1862. struct bfd_link_info *info ATTRIBUTE_UNUSED,
  1863. Elf_Internal_Sym *sym,
  1864. const char **namep ATTRIBUTE_UNUSED,
  1865. flagword *flagsp ATTRIBUTE_UNUSED,
  1866. asection **secp,
  1867. bfd_vma *valp ATTRIBUTE_UNUSED)
  1868. {
  1869. if (sym->st_shndx == SHN_REGISTER)
  1870. {
  1871. *secp = bfd_make_section_old_way (abfd, MMIX_REG_SECTION_NAME);
  1872. (*secp)->flags |= SEC_LINKER_CREATED;
  1873. }
  1874. else if ((*namep)[0] == '_' && (*namep)[1] == '_' && (*namep)[2] == '.'
  1875. && startswith (*namep, MMIX_LOC_SECTION_START_SYMBOL_PREFIX))
  1876. {
  1877. /* See if we have another one. */
  1878. struct bfd_link_hash_entry *h = bfd_link_hash_lookup (info->hash,
  1879. *namep,
  1880. false,
  1881. false,
  1882. false);
  1883. if (h != NULL && h->type != bfd_link_hash_undefined)
  1884. {
  1885. /* How do we get the asymbol (or really: the filename) from h?
  1886. h->u.def.section->owner is NULL. */
  1887. _bfd_error_handler
  1888. /* xgettext:c-format */
  1889. (_("%pB: error: multiple definition of `%s'; start of %s "
  1890. "is set in a earlier linked file"),
  1891. abfd, *namep,
  1892. *namep + strlen (MMIX_LOC_SECTION_START_SYMBOL_PREFIX));
  1893. bfd_set_error (bfd_error_bad_value);
  1894. return false;
  1895. }
  1896. }
  1897. return true;
  1898. }
  1899. /* We consider symbols matching "L.*:[0-9]+" to be local symbols. */
  1900. static bool
  1901. mmix_elf_is_local_label_name (bfd *abfd, const char *name)
  1902. {
  1903. const char *colpos;
  1904. int digits;
  1905. /* Also include the default local-label definition. */
  1906. if (_bfd_elf_is_local_label_name (abfd, name))
  1907. return true;
  1908. if (*name != 'L')
  1909. return false;
  1910. /* If there's no ":", or more than one, it's not a local symbol. */
  1911. colpos = strchr (name, ':');
  1912. if (colpos == NULL || strchr (colpos + 1, ':') != NULL)
  1913. return false;
  1914. /* Check that there are remaining characters and that they are digits. */
  1915. if (colpos[1] == 0)
  1916. return false;
  1917. digits = strspn (colpos + 1, "0123456789");
  1918. return digits != 0 && colpos[1 + digits] == 0;
  1919. }
  1920. /* We get rid of the register section here. */
  1921. bool
  1922. mmix_elf_final_link (bfd *abfd, struct bfd_link_info *info)
  1923. {
  1924. /* We never output a register section, though we create one for
  1925. temporary measures. Check that nobody entered contents into it. */
  1926. asection *reg_section;
  1927. reg_section = bfd_get_section_by_name (abfd, MMIX_REG_SECTION_NAME);
  1928. if (reg_section != NULL)
  1929. {
  1930. /* FIXME: Pass error state gracefully. */
  1931. if (bfd_section_flags (reg_section) & SEC_HAS_CONTENTS)
  1932. _bfd_abort (__FILE__, __LINE__, _("register section has contents\n"));
  1933. /* Really remove the section, if it hasn't already been done. */
  1934. if (!bfd_section_removed_from_list (abfd, reg_section))
  1935. {
  1936. bfd_section_list_remove (abfd, reg_section);
  1937. --abfd->section_count;
  1938. }
  1939. }
  1940. if (! bfd_elf_final_link (abfd, info))
  1941. return false;
  1942. /* Since this section is marked SEC_LINKER_CREATED, it isn't output by
  1943. the regular linker machinery. We do it here, like other targets with
  1944. special sections. */
  1945. if (info->base_file != NULL)
  1946. {
  1947. asection *greg_section
  1948. = bfd_get_section_by_name ((bfd *) info->base_file,
  1949. MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
  1950. if (!bfd_set_section_contents (abfd,
  1951. greg_section->output_section,
  1952. greg_section->contents,
  1953. (file_ptr) greg_section->output_offset,
  1954. greg_section->size))
  1955. return false;
  1956. }
  1957. return true;
  1958. }
  1959. /* We need to include the maximum size of PUSHJ-stubs in the initial
  1960. section size. This is expected to shrink during linker relaxation. */
  1961. static void
  1962. mmix_set_relaxable_size (bfd *abfd ATTRIBUTE_UNUSED,
  1963. asection *sec,
  1964. void *ptr)
  1965. {
  1966. struct bfd_link_info *info = ptr;
  1967. /* Make sure we only do this for section where we know we want this,
  1968. otherwise we might end up resetting the size of COMMONs. */
  1969. if (mmix_elf_section_data (sec)->pjs.n_pushj_relocs == 0)
  1970. return;
  1971. sec->rawsize = sec->size;
  1972. sec->size += (mmix_elf_section_data (sec)->pjs.n_pushj_relocs
  1973. * MAX_PUSHJ_STUB_SIZE);
  1974. /* For use in relocatable link, we start with a max stubs size. See
  1975. mmix_elf_relax_section. */
  1976. if (bfd_link_relocatable (info) && sec->output_section)
  1977. mmix_elf_section_data (sec->output_section)->pjs.stubs_size_sum
  1978. += (mmix_elf_section_data (sec)->pjs.n_pushj_relocs
  1979. * MAX_PUSHJ_STUB_SIZE);
  1980. }
  1981. /* Initialize stuff for the linker-generated GREGs to match
  1982. R_MMIX_BASE_PLUS_OFFSET relocs seen by the linker. */
  1983. bool
  1984. _bfd_mmix_before_linker_allocation (bfd *abfd ATTRIBUTE_UNUSED,
  1985. struct bfd_link_info *info)
  1986. {
  1987. asection *bpo_gregs_section;
  1988. bfd *bpo_greg_owner;
  1989. struct bpo_greg_section_info *gregdata;
  1990. size_t n_gregs;
  1991. bfd_vma gregs_size;
  1992. size_t i;
  1993. size_t *bpo_reloc_indexes;
  1994. bfd *ibfd;
  1995. /* Set the initial size of sections. */
  1996. for (ibfd = info->input_bfds; ibfd != NULL; ibfd = ibfd->link.next)
  1997. bfd_map_over_sections (ibfd, mmix_set_relaxable_size, info);
  1998. /* The bpo_greg_owner bfd is supposed to have been set by
  1999. mmix_elf_check_relocs when the first R_MMIX_BASE_PLUS_OFFSET is seen.
  2000. If there is no such object, there was no R_MMIX_BASE_PLUS_OFFSET. */
  2001. bpo_greg_owner = (bfd *) info->base_file;
  2002. if (bpo_greg_owner == NULL)
  2003. return true;
  2004. bpo_gregs_section
  2005. = bfd_get_section_by_name (bpo_greg_owner,
  2006. MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
  2007. if (bpo_gregs_section == NULL)
  2008. return true;
  2009. /* We use the target-data handle in the ELF section data. */
  2010. gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
  2011. if (gregdata == NULL)
  2012. return false;
  2013. n_gregs = gregdata->n_bpo_relocs;
  2014. gregdata->n_allocated_bpo_gregs = n_gregs;
  2015. /* When this reaches zero during relaxation, all entries have been
  2016. filled in and the size of the linker gregs can be calculated. */
  2017. gregdata->n_remaining_bpo_relocs_this_relaxation_round = n_gregs;
  2018. /* Set the zeroth-order estimate for the GREGs size. */
  2019. gregs_size = n_gregs * 8;
  2020. if (!bfd_set_section_size (bpo_gregs_section, gregs_size))
  2021. return false;
  2022. /* Allocate and set up the GREG arrays. They're filled in at relaxation
  2023. time. Note that we must use the max number ever noted for the array,
  2024. since the index numbers were created before GC. */
  2025. gregdata->reloc_request
  2026. = bfd_zalloc (bpo_greg_owner,
  2027. sizeof (struct bpo_reloc_request)
  2028. * gregdata->n_max_bpo_relocs);
  2029. gregdata->bpo_reloc_indexes
  2030. = bpo_reloc_indexes
  2031. = bfd_alloc (bpo_greg_owner,
  2032. gregdata->n_max_bpo_relocs
  2033. * sizeof (size_t));
  2034. if (bpo_reloc_indexes == NULL)
  2035. return false;
  2036. /* The default order is an identity mapping. */
  2037. for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
  2038. {
  2039. bpo_reloc_indexes[i] = i;
  2040. gregdata->reloc_request[i].bpo_reloc_no = i;
  2041. }
  2042. return true;
  2043. }
  2044. /* Fill in contents in the linker allocated gregs. Everything is
  2045. calculated at this point; we just move the contents into place here. */
  2046. bool
  2047. _bfd_mmix_after_linker_allocation (bfd *abfd ATTRIBUTE_UNUSED,
  2048. struct bfd_link_info *link_info)
  2049. {
  2050. asection *bpo_gregs_section;
  2051. bfd *bpo_greg_owner;
  2052. struct bpo_greg_section_info *gregdata;
  2053. size_t n_gregs;
  2054. size_t i, j;
  2055. size_t lastreg;
  2056. bfd_byte *contents;
  2057. /* The bpo_greg_owner bfd is supposed to have been set by mmix_elf_check_relocs
  2058. when the first R_MMIX_BASE_PLUS_OFFSET is seen. If there is no such
  2059. object, there was no R_MMIX_BASE_PLUS_OFFSET. */
  2060. bpo_greg_owner = (bfd *) link_info->base_file;
  2061. if (bpo_greg_owner == NULL)
  2062. return true;
  2063. bpo_gregs_section
  2064. = bfd_get_section_by_name (bpo_greg_owner,
  2065. MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
  2066. /* This can't happen without DSO handling. When DSOs are handled
  2067. without any R_MMIX_BASE_PLUS_OFFSET seen, there will be no such
  2068. section. */
  2069. if (bpo_gregs_section == NULL)
  2070. return true;
  2071. /* We use the target-data handle in the ELF section data. */
  2072. gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
  2073. if (gregdata == NULL)
  2074. return false;
  2075. n_gregs = gregdata->n_allocated_bpo_gregs;
  2076. bpo_gregs_section->contents
  2077. = contents = bfd_alloc (bpo_greg_owner, bpo_gregs_section->size);
  2078. if (contents == NULL)
  2079. return false;
  2080. /* Sanity check: If these numbers mismatch, some relocation has not been
  2081. accounted for and the rest of gregdata is probably inconsistent.
  2082. It's a bug, but it's more helpful to identify it than segfaulting
  2083. below. */
  2084. if (gregdata->n_remaining_bpo_relocs_this_relaxation_round
  2085. != gregdata->n_bpo_relocs)
  2086. {
  2087. _bfd_error_handler
  2088. /* xgettext:c-format */
  2089. (_("internal inconsistency: remaining %lu != max %lu;"
  2090. " please report this bug"),
  2091. (unsigned long) gregdata->n_remaining_bpo_relocs_this_relaxation_round,
  2092. (unsigned long) gregdata->n_bpo_relocs);
  2093. return false;
  2094. }
  2095. for (lastreg = 255, i = 0, j = 0; j < n_gregs; i++)
  2096. if (gregdata->reloc_request[i].regindex != lastreg)
  2097. {
  2098. bfd_put_64 (bpo_greg_owner, gregdata->reloc_request[i].value,
  2099. contents + j * 8);
  2100. lastreg = gregdata->reloc_request[i].regindex;
  2101. j++;
  2102. }
  2103. return true;
  2104. }
  2105. /* Sort valid relocs to come before non-valid relocs, then on increasing
  2106. value. */
  2107. static int
  2108. bpo_reloc_request_sort_fn (const void * p1, const void * p2)
  2109. {
  2110. const struct bpo_reloc_request *r1 = (const struct bpo_reloc_request *) p1;
  2111. const struct bpo_reloc_request *r2 = (const struct bpo_reloc_request *) p2;
  2112. /* Primary function is validity; non-valid relocs sorted after valid
  2113. ones. */
  2114. if (r1->valid != r2->valid)
  2115. return r2->valid - r1->valid;
  2116. /* Then sort on value. Don't simplify and return just the difference of
  2117. the values: the upper bits of the 64-bit value would be truncated on
  2118. a host with 32-bit ints. */
  2119. if (r1->value != r2->value)
  2120. return r1->value > r2->value ? 1 : -1;
  2121. /* As a last re-sort, use the relocation number, so we get a stable
  2122. sort. The *addresses* aren't stable since items are swapped during
  2123. sorting. It depends on the qsort implementation if this actually
  2124. happens. */
  2125. return r1->bpo_reloc_no > r2->bpo_reloc_no
  2126. ? 1 : (r1->bpo_reloc_no < r2->bpo_reloc_no ? -1 : 0);
  2127. }
  2128. /* For debug use only. Dumps the global register allocations resulting
  2129. from base-plus-offset relocs. */
  2130. void
  2131. mmix_dump_bpo_gregs (struct bfd_link_info *link_info,
  2132. void (*pf) (const char *fmt, ...))
  2133. {
  2134. bfd *bpo_greg_owner;
  2135. asection *bpo_gregs_section;
  2136. struct bpo_greg_section_info *gregdata;
  2137. unsigned int i;
  2138. if (link_info == NULL || link_info->base_file == NULL)
  2139. return;
  2140. bpo_greg_owner = (bfd *) link_info->base_file;
  2141. bpo_gregs_section
  2142. = bfd_get_section_by_name (bpo_greg_owner,
  2143. MMIX_LD_ALLOCATED_REG_CONTENTS_SECTION_NAME);
  2144. if (bpo_gregs_section == NULL)
  2145. return;
  2146. gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
  2147. if (gregdata == NULL)
  2148. return;
  2149. if (pf == NULL)
  2150. pf = _bfd_error_handler;
  2151. /* These format strings are not translated. They are for debug purposes
  2152. only and never displayed to an end user. Should they escape, we
  2153. surely want them in original. */
  2154. (*pf) (" n_bpo_relocs: %u\n n_max_bpo_relocs: %u\n n_remain...round: %u\n\
  2155. n_allocated_bpo_gregs: %u\n", gregdata->n_bpo_relocs,
  2156. gregdata->n_max_bpo_relocs,
  2157. gregdata->n_remaining_bpo_relocs_this_relaxation_round,
  2158. gregdata->n_allocated_bpo_gregs);
  2159. if (gregdata->reloc_request)
  2160. for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
  2161. (*pf) ("%4u (%4u)/%4u#%u: 0x%08lx%08lx r: %3u o: %3u\n",
  2162. i,
  2163. (gregdata->bpo_reloc_indexes != NULL
  2164. ? gregdata->bpo_reloc_indexes[i] : (size_t) -1),
  2165. gregdata->reloc_request[i].bpo_reloc_no,
  2166. gregdata->reloc_request[i].valid,
  2167. (unsigned long) (gregdata->reloc_request[i].value >> 32),
  2168. (unsigned long) gregdata->reloc_request[i].value,
  2169. gregdata->reloc_request[i].regindex,
  2170. gregdata->reloc_request[i].offset);
  2171. }
  2172. /* This links all R_MMIX_BASE_PLUS_OFFSET relocs into a special array, and
  2173. when the last such reloc is done, an index-array is sorted according to
  2174. the values and iterated over to produce register numbers (indexed by 0
  2175. from the first allocated register number) and offsets for use in real
  2176. relocation. (N.B.: Relocatable runs are handled, not just punted.)
  2177. PUSHJ stub accounting is also done here.
  2178. Symbol- and reloc-reading infrastructure copied from elf-m10200.c. */
  2179. static bool
  2180. mmix_elf_relax_section (bfd *abfd,
  2181. asection *sec,
  2182. struct bfd_link_info *link_info,
  2183. bool *again)
  2184. {
  2185. Elf_Internal_Shdr *symtab_hdr;
  2186. Elf_Internal_Rela *internal_relocs;
  2187. Elf_Internal_Rela *irel, *irelend;
  2188. asection *bpo_gregs_section = NULL;
  2189. struct bpo_greg_section_info *gregdata;
  2190. struct bpo_reloc_section_info *bpodata
  2191. = mmix_elf_section_data (sec)->bpo.reloc;
  2192. /* The initialization is to quiet compiler warnings. The value is to
  2193. spot a missing actual initialization. */
  2194. size_t bpono = (size_t) -1;
  2195. size_t pjsno = 0;
  2196. size_t pjsno_undefs = 0;
  2197. Elf_Internal_Sym *isymbuf = NULL;
  2198. bfd_size_type size = sec->rawsize ? sec->rawsize : sec->size;
  2199. mmix_elf_section_data (sec)->pjs.stubs_size_sum = 0;
  2200. /* Assume nothing changes. */
  2201. *again = false;
  2202. /* We don't have to do anything if this section does not have relocs, or
  2203. if this is not a code section. */
  2204. if ((sec->flags & SEC_RELOC) == 0
  2205. || sec->reloc_count == 0
  2206. || (sec->flags & SEC_CODE) == 0
  2207. || (sec->flags & SEC_LINKER_CREATED) != 0
  2208. /* If no R_MMIX_BASE_PLUS_OFFSET relocs and no PUSHJ-stub relocs,
  2209. then nothing to do. */
  2210. || (bpodata == NULL
  2211. && mmix_elf_section_data (sec)->pjs.n_pushj_relocs == 0))
  2212. return true;
  2213. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2214. if (bpodata != NULL)
  2215. {
  2216. bpo_gregs_section = bpodata->bpo_greg_section;
  2217. gregdata = mmix_elf_section_data (bpo_gregs_section)->bpo.greg;
  2218. bpono = bpodata->first_base_plus_offset_reloc;
  2219. }
  2220. else
  2221. gregdata = NULL;
  2222. /* Get a copy of the native relocations. */
  2223. internal_relocs
  2224. = _bfd_elf_link_read_relocs (abfd, sec, NULL,
  2225. (Elf_Internal_Rela *) NULL,
  2226. link_info->keep_memory);
  2227. if (internal_relocs == NULL)
  2228. goto error_return;
  2229. /* Walk through them looking for relaxing opportunities. */
  2230. irelend = internal_relocs + sec->reloc_count;
  2231. for (irel = internal_relocs; irel < irelend; irel++)
  2232. {
  2233. bfd_vma symval;
  2234. struct elf_link_hash_entry *h = NULL;
  2235. /* We only process two relocs. */
  2236. if (ELF64_R_TYPE (irel->r_info) != (int) R_MMIX_BASE_PLUS_OFFSET
  2237. && ELF64_R_TYPE (irel->r_info) != (int) R_MMIX_PUSHJ_STUBBABLE)
  2238. continue;
  2239. /* We process relocs in a distinctly different way when this is a
  2240. relocatable link (for one, we don't look at symbols), so we avoid
  2241. mixing its code with that for the "normal" relaxation. */
  2242. if (bfd_link_relocatable (link_info))
  2243. {
  2244. /* The only transformation in a relocatable link is to generate
  2245. a full stub at the location of the stub calculated for the
  2246. input section, if the relocated stub location, the end of the
  2247. output section plus earlier stubs, cannot be reached. Thus
  2248. relocatable linking can only lead to worse code, but it still
  2249. works. */
  2250. if (ELF64_R_TYPE (irel->r_info) == R_MMIX_PUSHJ_STUBBABLE)
  2251. {
  2252. /* If we can reach the end of the output-section and beyond
  2253. any current stubs, then we don't need a stub for this
  2254. reloc. The relaxed order of output stub allocation may
  2255. not exactly match the straightforward order, so we always
  2256. assume presence of output stubs, which will allow
  2257. relaxation only on relocations indifferent to the
  2258. presence of output stub allocations for other relocations
  2259. and thus the order of output stub allocation. */
  2260. if (bfd_check_overflow (complain_overflow_signed,
  2261. 19,
  2262. 0,
  2263. bfd_arch_bits_per_address (abfd),
  2264. /* Output-stub location. */
  2265. sec->output_section->rawsize
  2266. + (mmix_elf_section_data (sec
  2267. ->output_section)
  2268. ->pjs.stubs_size_sum)
  2269. /* Location of this PUSHJ reloc. */
  2270. - (sec->output_offset + irel->r_offset)
  2271. /* Don't count *this* stub twice. */
  2272. - (mmix_elf_section_data (sec)
  2273. ->pjs.stub_size[pjsno]
  2274. + MAX_PUSHJ_STUB_SIZE))
  2275. == bfd_reloc_ok)
  2276. mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 0;
  2277. mmix_elf_section_data (sec)->pjs.stubs_size_sum
  2278. += mmix_elf_section_data (sec)->pjs.stub_size[pjsno];
  2279. pjsno++;
  2280. }
  2281. continue;
  2282. }
  2283. /* Get the value of the symbol referred to by the reloc. */
  2284. if (ELF64_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  2285. {
  2286. /* A local symbol. */
  2287. Elf_Internal_Sym *isym;
  2288. asection *sym_sec;
  2289. /* Read this BFD's local symbols if we haven't already. */
  2290. if (isymbuf == NULL)
  2291. {
  2292. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  2293. if (isymbuf == NULL)
  2294. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  2295. symtab_hdr->sh_info, 0,
  2296. NULL, NULL, NULL);
  2297. if (isymbuf == 0)
  2298. goto error_return;
  2299. }
  2300. isym = isymbuf + ELF64_R_SYM (irel->r_info);
  2301. if (isym->st_shndx == SHN_UNDEF)
  2302. sym_sec = bfd_und_section_ptr;
  2303. else if (isym->st_shndx == SHN_ABS)
  2304. sym_sec = bfd_abs_section_ptr;
  2305. else if (isym->st_shndx == SHN_COMMON)
  2306. sym_sec = bfd_com_section_ptr;
  2307. else
  2308. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  2309. symval = (isym->st_value
  2310. + sym_sec->output_section->vma
  2311. + sym_sec->output_offset);
  2312. }
  2313. else
  2314. {
  2315. unsigned long indx;
  2316. /* An external symbol. */
  2317. indx = ELF64_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  2318. h = elf_sym_hashes (abfd)[indx];
  2319. BFD_ASSERT (h != NULL);
  2320. if (h->root.type == bfd_link_hash_undefweak)
  2321. /* FIXME: for R_MMIX_PUSHJ_STUBBABLE, there are alternatives to
  2322. the canonical value 0 for an unresolved weak symbol to
  2323. consider: as the debug-friendly approach, resolve to "abort"
  2324. (or a port-specific function), or as the space-friendly
  2325. approach resolve to the next instruction (like some other
  2326. ports, notably ARM and AArch64). These alternatives require
  2327. matching code in mmix_elf_perform_relocation or its caller. */
  2328. symval = 0;
  2329. else if (h->root.type == bfd_link_hash_defined
  2330. || h->root.type == bfd_link_hash_defweak)
  2331. symval = (h->root.u.def.value
  2332. + h->root.u.def.section->output_section->vma
  2333. + h->root.u.def.section->output_offset);
  2334. else
  2335. {
  2336. /* This appears to be a reference to an undefined symbol. Just
  2337. ignore it--it will be caught by the regular reloc processing.
  2338. We need to keep BPO reloc accounting consistent, though
  2339. else we'll abort instead of emitting an error message. */
  2340. if (ELF64_R_TYPE (irel->r_info) == R_MMIX_BASE_PLUS_OFFSET
  2341. && gregdata != NULL)
  2342. {
  2343. gregdata->n_remaining_bpo_relocs_this_relaxation_round--;
  2344. bpono++;
  2345. }
  2346. /* Similarly, keep accounting consistent for PUSHJ
  2347. referring to an undefined symbol. */
  2348. if (ELF64_R_TYPE (irel->r_info) == R_MMIX_PUSHJ_STUBBABLE)
  2349. pjsno_undefs++;
  2350. continue;
  2351. }
  2352. }
  2353. if (ELF64_R_TYPE (irel->r_info) == (int) R_MMIX_PUSHJ_STUBBABLE)
  2354. {
  2355. bfd_vma value = symval + irel->r_addend;
  2356. bfd_vma dot
  2357. = (sec->output_section->vma
  2358. + sec->output_offset
  2359. + irel->r_offset);
  2360. bfd_vma stubaddr
  2361. = (sec->output_section->vma
  2362. + sec->output_offset
  2363. + size
  2364. + mmix_elf_section_data (sec)->pjs.stubs_size_sum);
  2365. if ((value & 3) == 0
  2366. && bfd_check_overflow (complain_overflow_signed,
  2367. 19,
  2368. 0,
  2369. bfd_arch_bits_per_address (abfd),
  2370. value - dot
  2371. - (value > dot
  2372. ? mmix_elf_section_data (sec)
  2373. ->pjs.stub_size[pjsno]
  2374. : 0))
  2375. == bfd_reloc_ok)
  2376. /* If the reloc fits, no stub is needed. */
  2377. mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 0;
  2378. else
  2379. /* Maybe we can get away with just a JMP insn? */
  2380. if ((value & 3) == 0
  2381. && bfd_check_overflow (complain_overflow_signed,
  2382. 27,
  2383. 0,
  2384. bfd_arch_bits_per_address (abfd),
  2385. value - stubaddr
  2386. - (value > dot
  2387. ? mmix_elf_section_data (sec)
  2388. ->pjs.stub_size[pjsno] - 4
  2389. : 0))
  2390. == bfd_reloc_ok)
  2391. /* Yep, account for a stub consisting of a single JMP insn. */
  2392. mmix_elf_section_data (sec)->pjs.stub_size[pjsno] = 4;
  2393. else
  2394. /* Nope, go for the full insn stub. It doesn't seem useful to
  2395. emit the intermediate sizes; those will only be useful for
  2396. a >64M program assuming contiguous code. */
  2397. mmix_elf_section_data (sec)->pjs.stub_size[pjsno]
  2398. = MAX_PUSHJ_STUB_SIZE;
  2399. mmix_elf_section_data (sec)->pjs.stubs_size_sum
  2400. += mmix_elf_section_data (sec)->pjs.stub_size[pjsno];
  2401. pjsno++;
  2402. continue;
  2403. }
  2404. /* We're looking at a R_MMIX_BASE_PLUS_OFFSET reloc. */
  2405. gregdata->reloc_request[gregdata->bpo_reloc_indexes[bpono]].value
  2406. = symval + irel->r_addend;
  2407. gregdata->reloc_request[gregdata->bpo_reloc_indexes[bpono++]].valid = true;
  2408. gregdata->n_remaining_bpo_relocs_this_relaxation_round--;
  2409. }
  2410. /* Check if that was the last BPO-reloc. If so, sort the values and
  2411. calculate how many registers we need to cover them. Set the size of
  2412. the linker gregs, and if the number of registers changed, indicate
  2413. that we need to relax some more because we have more work to do. */
  2414. if (gregdata != NULL
  2415. && gregdata->n_remaining_bpo_relocs_this_relaxation_round == 0)
  2416. {
  2417. size_t i;
  2418. bfd_vma prev_base;
  2419. size_t regindex;
  2420. /* First, reset the remaining relocs for the next round. */
  2421. gregdata->n_remaining_bpo_relocs_this_relaxation_round
  2422. = gregdata->n_bpo_relocs;
  2423. qsort (gregdata->reloc_request,
  2424. gregdata->n_max_bpo_relocs,
  2425. sizeof (struct bpo_reloc_request),
  2426. bpo_reloc_request_sort_fn);
  2427. /* Recalculate indexes. When we find a change (however unlikely
  2428. after the initial iteration), we know we need to relax again,
  2429. since items in the GREG-array are sorted by increasing value and
  2430. stored in the relaxation phase. */
  2431. for (i = 0; i < gregdata->n_max_bpo_relocs; i++)
  2432. if (gregdata->bpo_reloc_indexes[gregdata->reloc_request[i].bpo_reloc_no]
  2433. != i)
  2434. {
  2435. gregdata->bpo_reloc_indexes[gregdata->reloc_request[i].bpo_reloc_no]
  2436. = i;
  2437. *again = true;
  2438. }
  2439. /* Allocate register numbers (indexing from 0). Stop at the first
  2440. non-valid reloc. */
  2441. for (i = 0, regindex = 0, prev_base = gregdata->reloc_request[0].value;
  2442. i < gregdata->n_bpo_relocs;
  2443. i++)
  2444. {
  2445. if (gregdata->reloc_request[i].value > prev_base + 255)
  2446. {
  2447. regindex++;
  2448. prev_base = gregdata->reloc_request[i].value;
  2449. }
  2450. gregdata->reloc_request[i].regindex = regindex;
  2451. gregdata->reloc_request[i].offset
  2452. = gregdata->reloc_request[i].value - prev_base;
  2453. }
  2454. /* If it's not the same as the last time, we need to relax again,
  2455. because the size of the section has changed. I'm not sure we
  2456. actually need to do any adjustments since the shrinking happens
  2457. at the start of this section, but better safe than sorry. */
  2458. if (gregdata->n_allocated_bpo_gregs != regindex + 1)
  2459. {
  2460. gregdata->n_allocated_bpo_gregs = regindex + 1;
  2461. *again = true;
  2462. }
  2463. bpo_gregs_section->size = (regindex + 1) * 8;
  2464. }
  2465. if (isymbuf != NULL && (unsigned char *) isymbuf != symtab_hdr->contents)
  2466. {
  2467. if (! link_info->keep_memory)
  2468. free (isymbuf);
  2469. else
  2470. {
  2471. /* Cache the symbols for elf_link_input_bfd. */
  2472. symtab_hdr->contents = (unsigned char *) isymbuf;
  2473. }
  2474. }
  2475. BFD_ASSERT(pjsno + pjsno_undefs
  2476. == mmix_elf_section_data (sec)->pjs.n_pushj_relocs);
  2477. if (elf_section_data (sec)->relocs != internal_relocs)
  2478. free (internal_relocs);
  2479. if (sec->size < size + mmix_elf_section_data (sec)->pjs.stubs_size_sum)
  2480. abort ();
  2481. if (sec->size > size + mmix_elf_section_data (sec)->pjs.stubs_size_sum)
  2482. {
  2483. sec->size = size + mmix_elf_section_data (sec)->pjs.stubs_size_sum;
  2484. *again = true;
  2485. }
  2486. return true;
  2487. error_return:
  2488. if ((unsigned char *) isymbuf != symtab_hdr->contents)
  2489. free (isymbuf);
  2490. if (elf_section_data (sec)->relocs != internal_relocs)
  2491. free (internal_relocs);
  2492. return false;
  2493. }
  2494. #define ELF_ARCH bfd_arch_mmix
  2495. #define ELF_MACHINE_CODE EM_MMIX
  2496. /* According to mmix-doc page 36 (paragraph 45), this should be (1LL << 48LL).
  2497. However, that's too much for something somewhere in the linker part of
  2498. BFD; perhaps the start-address has to be a non-zero multiple of this
  2499. number, or larger than this number. The symptom is that the linker
  2500. complains: "warning: allocated section `.text' not in segment". We
  2501. settle for 64k; the page-size used in examples is 8k.
  2502. #define ELF_MAXPAGESIZE 0x10000
  2503. Unfortunately, this causes excessive padding in the supposedly small
  2504. for-education programs that are the expected usage (where people would
  2505. inspect output). We stick to 256 bytes just to have *some* default
  2506. alignment. */
  2507. #define ELF_MAXPAGESIZE 0x100
  2508. #define TARGET_BIG_SYM mmix_elf64_vec
  2509. #define TARGET_BIG_NAME "elf64-mmix"
  2510. #define elf_info_to_howto_rel NULL
  2511. #define elf_info_to_howto mmix_info_to_howto_rela
  2512. #define elf_backend_relocate_section mmix_elf_relocate_section
  2513. #define elf_backend_gc_mark_hook mmix_elf_gc_mark_hook
  2514. #define elf_backend_link_output_symbol_hook \
  2515. mmix_elf_link_output_symbol_hook
  2516. #define elf_backend_add_symbol_hook mmix_elf_add_symbol_hook
  2517. #define elf_backend_check_relocs mmix_elf_check_relocs
  2518. #define elf_backend_symbol_processing mmix_elf_symbol_processing
  2519. #define elf_backend_omit_section_dynsym _bfd_elf_omit_section_dynsym_all
  2520. #define bfd_elf64_bfd_copy_link_hash_symbol_type \
  2521. _bfd_generic_copy_link_hash_symbol_type
  2522. #define bfd_elf64_bfd_is_local_label_name \
  2523. mmix_elf_is_local_label_name
  2524. #define elf_backend_may_use_rel_p 0
  2525. #define elf_backend_may_use_rela_p 1
  2526. #define elf_backend_default_use_rela_p 1
  2527. #define elf_backend_can_gc_sections 1
  2528. #define elf_backend_section_from_bfd_section \
  2529. mmix_elf_section_from_bfd_section
  2530. #define bfd_elf64_new_section_hook mmix_elf_new_section_hook
  2531. #define bfd_elf64_bfd_final_link mmix_elf_final_link
  2532. #define bfd_elf64_bfd_relax_section mmix_elf_relax_section
  2533. #include "elf64-target.h"