elf32-rl78.c 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617
  1. /* Renesas RL78 specific support for 32-bit ELF.
  2. Copyright (C) 2011-2022 Free Software Foundation, Inc.
  3. This file is part of BFD, the Binary File Descriptor library.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  15. MA 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include "bfd.h"
  18. #include "libbfd.h"
  19. #include "elf-bfd.h"
  20. #include "elf/rl78.h"
  21. #include "libiberty.h"
  22. #define valid_16bit_address(v) ((v) <= 0x0ffff || (v) >= 0xf0000)
  23. #define RL78REL(n,sz,bit,mask,shift,complain,pcrel) \
  24. HOWTO (R_RL78_##n, shift, sz, bit, pcrel, 0, complain_overflow_ ## complain, \
  25. bfd_elf_generic_reloc, "R_RL78_" #n, false, 0, mask, false)
  26. static bfd_reloc_status_type rl78_special_reloc (bfd *, arelent *, asymbol *, void *,
  27. asection *, bfd *, char **);
  28. #define RL78_OP_REL(n,sz,bit,mask,shift,complain,pcrel) \
  29. HOWTO (R_RL78_##n, shift, sz, bit, pcrel, 0, complain_overflow_ ## complain, \
  30. rl78_special_reloc, "R_RL78_" #n, false, 0, mask, false)
  31. /* Note that the relocations around 0x7f are internal to this file;
  32. feel free to move them as needed to avoid conflicts with published
  33. relocation numbers. */
  34. static reloc_howto_type rl78_elf_howto_table [] =
  35. {
  36. RL78REL (NONE, 3, 0, 0, 0, dont, false),
  37. RL78REL (DIR32, 2, 32, 0xffffffff, 0, dont, false),
  38. RL78REL (DIR24S, 2, 24, 0xffffff, 0, signed, false),
  39. RL78REL (DIR16, 1, 16, 0xffff, 0, bitfield, false),
  40. RL78REL (DIR16U, 1, 16, 0xffff, 0, unsigned, false),
  41. RL78REL (DIR16S, 1, 16, 0xffff, 0, bitfield, false),
  42. RL78REL (DIR8, 0, 8, 0xff, 0, dont, false),
  43. RL78REL (DIR8U, 0, 8, 0xff, 0, unsigned, false),
  44. RL78REL (DIR8S, 0, 8, 0xff, 0, bitfield, false),
  45. RL78REL (DIR24S_PCREL, 2, 24, 0xffffff, 0, signed, true),
  46. RL78REL (DIR16S_PCREL, 1, 16, 0xffff, 0, signed, true),
  47. RL78REL (DIR8S_PCREL, 0, 8, 0xff, 0, signed, true),
  48. RL78REL (DIR16UL, 1, 16, 0xffff, 2, unsigned, false),
  49. RL78REL (DIR16UW, 1, 16, 0xffff, 1, unsigned, false),
  50. RL78REL (DIR8UL, 0, 8, 0xff, 2, unsigned, false),
  51. RL78REL (DIR8UW, 0, 8, 0xff, 1, unsigned, false),
  52. RL78REL (DIR32_REV, 2, 32, 0xffffffff, 0, dont, false),
  53. RL78REL (DIR16_REV, 1, 16, 0xffff, 0, bitfield, false),
  54. RL78REL (DIR3U_PCREL, 0, 3, 0x7, 0, unsigned, true),
  55. EMPTY_HOWTO (0x13),
  56. EMPTY_HOWTO (0x14),
  57. EMPTY_HOWTO (0x15),
  58. EMPTY_HOWTO (0x16),
  59. EMPTY_HOWTO (0x17),
  60. EMPTY_HOWTO (0x18),
  61. EMPTY_HOWTO (0x19),
  62. EMPTY_HOWTO (0x1a),
  63. EMPTY_HOWTO (0x1b),
  64. EMPTY_HOWTO (0x1c),
  65. EMPTY_HOWTO (0x1d),
  66. EMPTY_HOWTO (0x1e),
  67. EMPTY_HOWTO (0x1f),
  68. EMPTY_HOWTO (0x20),
  69. EMPTY_HOWTO (0x21),
  70. EMPTY_HOWTO (0x22),
  71. EMPTY_HOWTO (0x23),
  72. EMPTY_HOWTO (0x24),
  73. EMPTY_HOWTO (0x25),
  74. EMPTY_HOWTO (0x26),
  75. EMPTY_HOWTO (0x27),
  76. EMPTY_HOWTO (0x28),
  77. EMPTY_HOWTO (0x29),
  78. EMPTY_HOWTO (0x2a),
  79. EMPTY_HOWTO (0x2b),
  80. EMPTY_HOWTO (0x2c),
  81. RL78REL (RH_RELAX, 3, 0, 0, 0, dont, false),
  82. RL78REL (RH_SFR, 0, 8, 0xff, 0, unsigned, false),
  83. RL78REL (RH_SADDR, 0, 8, 0xff, 0, unsigned, false),
  84. EMPTY_HOWTO (0x30),
  85. EMPTY_HOWTO (0x31),
  86. EMPTY_HOWTO (0x32),
  87. EMPTY_HOWTO (0x33),
  88. EMPTY_HOWTO (0x34),
  89. EMPTY_HOWTO (0x35),
  90. EMPTY_HOWTO (0x36),
  91. EMPTY_HOWTO (0x37),
  92. EMPTY_HOWTO (0x38),
  93. EMPTY_HOWTO (0x39),
  94. EMPTY_HOWTO (0x3a),
  95. EMPTY_HOWTO (0x3b),
  96. EMPTY_HOWTO (0x3c),
  97. EMPTY_HOWTO (0x3d),
  98. EMPTY_HOWTO (0x3e),
  99. EMPTY_HOWTO (0x3f),
  100. EMPTY_HOWTO (0x40),
  101. RL78_OP_REL (ABS32, 2, 32, 0xffffffff, 0, dont, false),
  102. RL78_OP_REL (ABS24S, 2, 24, 0xffffff, 0, signed, false),
  103. RL78_OP_REL (ABS16, 1, 16, 0xffff, 0, bitfield, false),
  104. RL78_OP_REL (ABS16U, 1, 16, 0xffff, 0, unsigned, false),
  105. RL78_OP_REL (ABS16S, 1, 16, 0xffff, 0, signed, false),
  106. RL78_OP_REL (ABS8, 0, 8, 0xff, 0, bitfield, false),
  107. RL78_OP_REL (ABS8U, 0, 8, 0xff, 0, unsigned, false),
  108. RL78_OP_REL (ABS8S, 0, 8, 0xff, 0, signed, false),
  109. RL78_OP_REL (ABS24S_PCREL, 2, 24, 0xffffff, 0, signed, true),
  110. RL78_OP_REL (ABS16S_PCREL, 1, 16, 0xffff, 0, signed, true),
  111. RL78_OP_REL (ABS8S_PCREL, 0, 8, 0xff, 0, signed, true),
  112. RL78_OP_REL (ABS16UL, 1, 16, 0xffff, 0, unsigned, false),
  113. RL78_OP_REL (ABS16UW, 1, 16, 0xffff, 0, unsigned, false),
  114. RL78_OP_REL (ABS8UL, 0, 8, 0xff, 0, unsigned, false),
  115. RL78_OP_REL (ABS8UW, 0, 8, 0xff, 0, unsigned, false),
  116. RL78_OP_REL (ABS32_REV, 2, 32, 0xffffffff, 0, dont, false),
  117. RL78_OP_REL (ABS16_REV, 1, 16, 0xffff, 0, bitfield, false),
  118. #define STACK_REL_P(x) ((x) <= R_RL78_ABS16_REV && (x) >= R_RL78_ABS32)
  119. EMPTY_HOWTO (0x52),
  120. EMPTY_HOWTO (0x53),
  121. EMPTY_HOWTO (0x54),
  122. EMPTY_HOWTO (0x55),
  123. EMPTY_HOWTO (0x56),
  124. EMPTY_HOWTO (0x57),
  125. EMPTY_HOWTO (0x58),
  126. EMPTY_HOWTO (0x59),
  127. EMPTY_HOWTO (0x5a),
  128. EMPTY_HOWTO (0x5b),
  129. EMPTY_HOWTO (0x5c),
  130. EMPTY_HOWTO (0x5d),
  131. EMPTY_HOWTO (0x5e),
  132. EMPTY_HOWTO (0x5f),
  133. EMPTY_HOWTO (0x60),
  134. EMPTY_HOWTO (0x61),
  135. EMPTY_HOWTO (0x62),
  136. EMPTY_HOWTO (0x63),
  137. EMPTY_HOWTO (0x64),
  138. EMPTY_HOWTO (0x65),
  139. EMPTY_HOWTO (0x66),
  140. EMPTY_HOWTO (0x67),
  141. EMPTY_HOWTO (0x68),
  142. EMPTY_HOWTO (0x69),
  143. EMPTY_HOWTO (0x6a),
  144. EMPTY_HOWTO (0x6b),
  145. EMPTY_HOWTO (0x6c),
  146. EMPTY_HOWTO (0x6d),
  147. EMPTY_HOWTO (0x6e),
  148. EMPTY_HOWTO (0x6f),
  149. EMPTY_HOWTO (0x70),
  150. EMPTY_HOWTO (0x71),
  151. EMPTY_HOWTO (0x72),
  152. EMPTY_HOWTO (0x73),
  153. EMPTY_HOWTO (0x74),
  154. EMPTY_HOWTO (0x75),
  155. EMPTY_HOWTO (0x76),
  156. EMPTY_HOWTO (0x77),
  157. EMPTY_HOWTO (0x78),
  158. EMPTY_HOWTO (0x79),
  159. EMPTY_HOWTO (0x7a),
  160. EMPTY_HOWTO (0x7b),
  161. EMPTY_HOWTO (0x7c),
  162. EMPTY_HOWTO (0x7d),
  163. EMPTY_HOWTO (0x7e),
  164. EMPTY_HOWTO (0x7f),
  165. RL78_OP_REL (SYM, 3, 0, 0, 0, dont, false),
  166. RL78_OP_REL (OPneg, 3, 0, 0, 0, dont, false),
  167. RL78_OP_REL (OPadd, 3, 0, 0, 0, dont, false),
  168. RL78_OP_REL (OPsub, 3, 0, 0, 0, dont, false),
  169. RL78_OP_REL (OPmul, 3, 0, 0, 0, dont, false),
  170. RL78_OP_REL (OPdiv, 3, 0, 0, 0, dont, false),
  171. RL78_OP_REL (OPshla, 3, 0, 0, 0, dont, false),
  172. RL78_OP_REL (OPshra, 3, 0, 0, 0, dont, false),
  173. RL78_OP_REL (OPsctsize, 3, 0, 0, 0, dont, false),
  174. EMPTY_HOWTO (0x89),
  175. EMPTY_HOWTO (0x8a),
  176. EMPTY_HOWTO (0x8b),
  177. EMPTY_HOWTO (0x8c),
  178. RL78_OP_REL (OPscttop, 3, 0, 0, 0, dont, false),
  179. EMPTY_HOWTO (0x8e),
  180. EMPTY_HOWTO (0x8f),
  181. RL78_OP_REL (OPand, 3, 0, 0, 0, dont, false),
  182. RL78_OP_REL (OPor, 3, 0, 0, 0, dont, false),
  183. RL78_OP_REL (OPxor, 3, 0, 0, 0, dont, false),
  184. RL78_OP_REL (OPnot, 3, 0, 0, 0, dont, false),
  185. RL78_OP_REL (OPmod, 3, 0, 0, 0, dont, false),
  186. RL78_OP_REL (OPromtop, 3, 0, 0, 0, dont, false),
  187. RL78_OP_REL (OPramtop, 3, 0, 0, 0, dont, false)
  188. };
  189. /* Map BFD reloc types to RL78 ELF reloc types. */
  190. struct rl78_reloc_map
  191. {
  192. bfd_reloc_code_real_type bfd_reloc_val;
  193. unsigned int rl78_reloc_val;
  194. };
  195. static const struct rl78_reloc_map rl78_reloc_map [] =
  196. {
  197. { BFD_RELOC_NONE, R_RL78_NONE },
  198. { BFD_RELOC_8, R_RL78_DIR8S },
  199. { BFD_RELOC_16, R_RL78_DIR16S },
  200. { BFD_RELOC_24, R_RL78_DIR24S },
  201. { BFD_RELOC_32, R_RL78_DIR32 },
  202. { BFD_RELOC_RL78_16_OP, R_RL78_DIR16 },
  203. { BFD_RELOC_RL78_DIR3U_PCREL, R_RL78_DIR3U_PCREL },
  204. { BFD_RELOC_8_PCREL, R_RL78_DIR8S_PCREL },
  205. { BFD_RELOC_16_PCREL, R_RL78_DIR16S_PCREL },
  206. { BFD_RELOC_24_PCREL, R_RL78_DIR24S_PCREL },
  207. { BFD_RELOC_RL78_8U, R_RL78_DIR8U },
  208. { BFD_RELOC_RL78_16U, R_RL78_DIR16U },
  209. { BFD_RELOC_RL78_SYM, R_RL78_SYM },
  210. { BFD_RELOC_RL78_OP_SUBTRACT, R_RL78_OPsub },
  211. { BFD_RELOC_RL78_OP_NEG, R_RL78_OPneg },
  212. { BFD_RELOC_RL78_OP_AND, R_RL78_OPand },
  213. { BFD_RELOC_RL78_OP_SHRA, R_RL78_OPshra },
  214. { BFD_RELOC_RL78_ABS8, R_RL78_ABS8 },
  215. { BFD_RELOC_RL78_ABS16, R_RL78_ABS16 },
  216. { BFD_RELOC_RL78_ABS16_REV, R_RL78_ABS16_REV },
  217. { BFD_RELOC_RL78_ABS32, R_RL78_ABS32 },
  218. { BFD_RELOC_RL78_ABS32_REV, R_RL78_ABS32_REV },
  219. { BFD_RELOC_RL78_ABS16UL, R_RL78_ABS16UL },
  220. { BFD_RELOC_RL78_ABS16UW, R_RL78_ABS16UW },
  221. { BFD_RELOC_RL78_ABS16U, R_RL78_ABS16U },
  222. { BFD_RELOC_RL78_SADDR, R_RL78_RH_SADDR },
  223. { BFD_RELOC_RL78_RELAX, R_RL78_RH_RELAX }
  224. };
  225. static reloc_howto_type *
  226. rl78_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
  227. bfd_reloc_code_real_type code)
  228. {
  229. unsigned int i;
  230. if (code == BFD_RELOC_RL78_32_OP)
  231. return rl78_elf_howto_table + R_RL78_DIR32;
  232. for (i = ARRAY_SIZE (rl78_reloc_map); i--;)
  233. if (rl78_reloc_map [i].bfd_reloc_val == code)
  234. return rl78_elf_howto_table + rl78_reloc_map[i].rl78_reloc_val;
  235. return NULL;
  236. }
  237. static reloc_howto_type *
  238. rl78_reloc_name_lookup (bfd * abfd ATTRIBUTE_UNUSED, const char * r_name)
  239. {
  240. unsigned int i;
  241. for (i = 0; i < ARRAY_SIZE (rl78_elf_howto_table); i++)
  242. if (rl78_elf_howto_table[i].name != NULL
  243. && strcasecmp (rl78_elf_howto_table[i].name, r_name) == 0)
  244. return rl78_elf_howto_table + i;
  245. return NULL;
  246. }
  247. /* Set the howto pointer for an RL78 ELF reloc. */
  248. static bool
  249. rl78_info_to_howto_rela (bfd * abfd,
  250. arelent * cache_ptr,
  251. Elf_Internal_Rela * dst)
  252. {
  253. unsigned int r_type;
  254. r_type = ELF32_R_TYPE (dst->r_info);
  255. if (r_type >= (unsigned int) R_RL78_max)
  256. {
  257. /* xgettext:c-format */
  258. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  259. abfd, r_type);
  260. bfd_set_error (bfd_error_bad_value);
  261. return false;
  262. }
  263. cache_ptr->howto = rl78_elf_howto_table + r_type;
  264. return true;
  265. }
  266. static bfd_vma
  267. get_symbol_value (const char * name,
  268. struct bfd_link_info * info,
  269. bfd * input_bfd,
  270. asection * input_section,
  271. int offset)
  272. {
  273. struct bfd_link_hash_entry * h;
  274. if (info == NULL)
  275. return 0;
  276. h = bfd_link_hash_lookup (info->hash, name, false, false, true);
  277. if (h == NULL
  278. || (h->type != bfd_link_hash_defined
  279. && h->type != bfd_link_hash_defweak))
  280. {
  281. (*info->callbacks->undefined_symbol)
  282. (info, name, input_bfd, input_section, offset, true);
  283. return 0;
  284. }
  285. return (h->u.def.value
  286. + h->u.def.section->output_section->vma
  287. + h->u.def.section->output_offset);
  288. }
  289. static bfd_vma
  290. get_romstart (struct bfd_link_info * info,
  291. bfd * abfd,
  292. asection * sec,
  293. int offset)
  294. {
  295. static bool cached = false;
  296. static bfd_vma cached_value = 0;
  297. if (!cached)
  298. {
  299. cached_value = get_symbol_value ("_start", info, abfd, sec, offset);
  300. cached = true;
  301. }
  302. return cached_value;
  303. }
  304. static bfd_vma
  305. get_ramstart (struct bfd_link_info * info,
  306. bfd * abfd,
  307. asection * sec,
  308. int offset)
  309. {
  310. static bool cached = false;
  311. static bfd_vma cached_value = 0;
  312. if (!cached)
  313. {
  314. cached_value = get_symbol_value ("__datastart", info, abfd, sec, offset);
  315. cached = true;
  316. }
  317. return cached_value;
  318. }
  319. #define NUM_STACK_ENTRIES 16
  320. static int32_t rl78_stack [ NUM_STACK_ENTRIES ];
  321. static unsigned int rl78_stack_top;
  322. static inline void
  323. rl78_stack_push (bfd_vma val, bfd_reloc_status_type *r)
  324. {
  325. if (rl78_stack_top < NUM_STACK_ENTRIES)
  326. rl78_stack[rl78_stack_top++] = val;
  327. else
  328. *r = bfd_reloc_dangerous;
  329. }
  330. static inline bfd_vma
  331. rl78_stack_pop (bfd_reloc_status_type *r)
  332. {
  333. if (rl78_stack_top > 0)
  334. return rl78_stack[-- rl78_stack_top];
  335. else
  336. *r = bfd_reloc_dangerous;
  337. return 0;
  338. }
  339. /* Special handling for RL78 complex relocs. Returns the
  340. value of the reloc, or 0 for relocs which do not generate
  341. a result. SYMVAL is the value of the symbol for relocs
  342. which use a symbolic argument. */
  343. static bfd_vma
  344. rl78_compute_complex_reloc (unsigned long r_type,
  345. bfd_vma symval,
  346. asection *input_section,
  347. bfd_reloc_status_type *r,
  348. char **error_message)
  349. {
  350. int32_t tmp1, tmp2;
  351. bfd_vma relocation = 0;
  352. bfd_reloc_status_type stat = bfd_reloc_ok;
  353. switch (r_type)
  354. {
  355. default:
  356. stat = bfd_reloc_notsupported;
  357. break;
  358. case R_RL78_ABS24S_PCREL:
  359. case R_RL78_ABS16S_PCREL:
  360. case R_RL78_ABS8S_PCREL:
  361. relocation = rl78_stack_pop (&stat);
  362. relocation -= input_section->output_section->vma + input_section->output_offset;
  363. break;
  364. case R_RL78_ABS32:
  365. case R_RL78_ABS32_REV:
  366. case R_RL78_ABS16:
  367. case R_RL78_ABS16_REV:
  368. case R_RL78_ABS16S:
  369. case R_RL78_ABS16U:
  370. case R_RL78_ABS8:
  371. case R_RL78_ABS8U:
  372. case R_RL78_ABS8S:
  373. relocation = rl78_stack_pop (&stat);
  374. break;
  375. case R_RL78_ABS16UL:
  376. case R_RL78_ABS8UL:
  377. relocation = rl78_stack_pop (&stat) >> 2;
  378. break;;
  379. case R_RL78_ABS16UW:
  380. case R_RL78_ABS8UW:
  381. relocation = rl78_stack_pop (&stat) >> 1;
  382. break;
  383. /* The rest of the relocs compute values and then push them onto the stack. */
  384. case R_RL78_OPramtop:
  385. case R_RL78_OPromtop:
  386. case R_RL78_SYM:
  387. rl78_stack_push (symval, &stat);
  388. break;
  389. case R_RL78_OPneg:
  390. tmp1 = rl78_stack_pop (&stat);
  391. tmp1 = - tmp1;
  392. rl78_stack_push (tmp1, &stat);
  393. break;
  394. case R_RL78_OPadd:
  395. tmp2 = rl78_stack_pop (&stat);
  396. tmp1 = rl78_stack_pop (&stat);
  397. tmp1 += tmp2;
  398. rl78_stack_push (tmp1, &stat);
  399. break;
  400. case R_RL78_OPsub:
  401. /* For the expression "A - B", the assembler pushes A,
  402. then B, then OPSUB. So the first op we pop is B, not A. */
  403. tmp2 = rl78_stack_pop (&stat); /* B */
  404. tmp1 = rl78_stack_pop (&stat); /* A */
  405. tmp1 -= tmp2; /* A - B */
  406. rl78_stack_push (tmp1, &stat);
  407. break;
  408. case R_RL78_OPmul:
  409. tmp2 = rl78_stack_pop (&stat);
  410. tmp1 = rl78_stack_pop (&stat);
  411. tmp1 *= tmp2;
  412. rl78_stack_push (tmp1, &stat);
  413. break;
  414. case R_RL78_OPdiv:
  415. tmp2 = rl78_stack_pop (&stat);
  416. tmp1 = rl78_stack_pop (&stat);
  417. if (tmp2 != 0)
  418. tmp1 /= tmp2;
  419. else
  420. {
  421. tmp1 = 0;
  422. stat = bfd_reloc_overflow;
  423. }
  424. rl78_stack_push (tmp1, &stat);
  425. break;
  426. case R_RL78_OPshla:
  427. tmp2 = rl78_stack_pop (&stat);
  428. tmp1 = rl78_stack_pop (&stat);
  429. tmp1 <<= tmp2;
  430. rl78_stack_push (tmp1, &stat);
  431. break;
  432. case R_RL78_OPshra:
  433. tmp2 = rl78_stack_pop (&stat);
  434. tmp1 = rl78_stack_pop (&stat);
  435. tmp1 >>= tmp2;
  436. rl78_stack_push (tmp1, &stat);
  437. break;
  438. case R_RL78_OPsctsize:
  439. rl78_stack_push (input_section->size, &stat);
  440. break;
  441. case R_RL78_OPscttop:
  442. rl78_stack_push (input_section->output_section->vma, &stat);
  443. break;
  444. case R_RL78_OPand:
  445. tmp2 = rl78_stack_pop (&stat);
  446. tmp1 = rl78_stack_pop (&stat);
  447. tmp1 &= tmp2;
  448. rl78_stack_push (tmp1, &stat);
  449. break;
  450. case R_RL78_OPor:
  451. tmp2 = rl78_stack_pop (&stat);
  452. tmp1 = rl78_stack_pop (&stat);
  453. tmp1 |= tmp2;
  454. rl78_stack_push (tmp1, &stat);
  455. break;
  456. case R_RL78_OPxor:
  457. tmp2 = rl78_stack_pop (&stat);
  458. tmp1 = rl78_stack_pop (&stat);
  459. tmp1 ^= tmp2;
  460. rl78_stack_push (tmp1, &stat);
  461. break;
  462. case R_RL78_OPnot:
  463. tmp1 = rl78_stack_pop (&stat);
  464. tmp1 = ~ tmp1;
  465. rl78_stack_push (tmp1, &stat);
  466. break;
  467. case R_RL78_OPmod:
  468. tmp2 = rl78_stack_pop (&stat);
  469. tmp1 = rl78_stack_pop (&stat);
  470. if (tmp2 != 0)
  471. tmp1 %= tmp2;
  472. else
  473. {
  474. tmp1 = 0;
  475. stat = bfd_reloc_overflow;
  476. }
  477. rl78_stack_push (tmp1, &stat);
  478. break;
  479. }
  480. if (r)
  481. {
  482. if (stat == bfd_reloc_dangerous)
  483. *error_message = (_("RL78 reloc stack overflow/underflow"));
  484. else if (stat == bfd_reloc_overflow)
  485. {
  486. stat = bfd_reloc_dangerous;
  487. *error_message = (_("RL78 reloc divide by zero"));
  488. }
  489. *r = stat;
  490. }
  491. return relocation;
  492. }
  493. /* Check whether RELOCATION overflows a relocation field described by
  494. HOWTO. */
  495. static bfd_reloc_status_type
  496. check_overflow (reloc_howto_type *howto, bfd_vma relocation)
  497. {
  498. switch (howto->complain_on_overflow)
  499. {
  500. case complain_overflow_dont:
  501. break;
  502. case complain_overflow_bitfield:
  503. if ((bfd_signed_vma) relocation < -(1LL << (howto->bitsize - 1))
  504. || (bfd_signed_vma) relocation >= 1LL << howto->bitsize)
  505. return bfd_reloc_overflow;
  506. break;
  507. case complain_overflow_signed:
  508. if ((bfd_signed_vma) relocation < -(1LL << (howto->bitsize - 1))
  509. || (bfd_signed_vma) relocation >= 1LL << (howto->bitsize - 1))
  510. return bfd_reloc_overflow;
  511. break;
  512. case complain_overflow_unsigned:
  513. if (relocation >= 1ULL << howto->bitsize)
  514. return bfd_reloc_overflow;
  515. break;
  516. }
  517. return bfd_reloc_ok;
  518. }
  519. static bfd_reloc_status_type
  520. rl78_special_reloc (bfd * input_bfd,
  521. arelent * reloc,
  522. asymbol * symbol,
  523. void * data,
  524. asection * input_section,
  525. bfd * output_bfd ATTRIBUTE_UNUSED,
  526. char ** error_message)
  527. {
  528. bfd_reloc_status_type r = bfd_reloc_ok;
  529. bfd_vma relocation = 0;
  530. unsigned long r_type = reloc->howto->type;
  531. bfd_byte * contents = data;
  532. /* If necessary, compute the symbolic value of the relocation. */
  533. switch (r_type)
  534. {
  535. case R_RL78_SYM:
  536. relocation = (symbol->value
  537. + symbol->section->output_section->vma
  538. + symbol->section->output_offset
  539. + reloc->addend);
  540. break;
  541. case R_RL78_OPromtop:
  542. relocation = get_romstart (NULL, input_bfd, input_section,
  543. reloc->address);
  544. break;
  545. case R_RL78_OPramtop:
  546. relocation = get_ramstart (NULL, input_bfd, input_section,
  547. reloc->address);
  548. break;
  549. }
  550. /* Get the value of the relocation. */
  551. relocation = rl78_compute_complex_reloc (r_type, relocation, input_section,
  552. &r, error_message);
  553. if (STACK_REL_P (r_type))
  554. {
  555. bfd_size_type limit;
  556. unsigned int nbytes;
  557. if (r == bfd_reloc_ok)
  558. r = check_overflow (reloc->howto, relocation);
  559. if (r_type == R_RL78_ABS16_REV)
  560. relocation = ((relocation & 0xff) << 8) | ((relocation >> 8) & 0xff);
  561. else if (r_type == R_RL78_ABS32_REV)
  562. relocation = (((relocation & 0xff) << 24)
  563. | ((relocation & 0xff00) << 8)
  564. | ((relocation >> 8) & 0xff00)
  565. | ((relocation >> 24) & 0xff));
  566. limit = bfd_get_section_limit_octets (input_bfd, input_section);
  567. nbytes = reloc->howto->bitsize / 8;
  568. if (reloc->address < limit
  569. && nbytes <= limit - reloc->address)
  570. {
  571. unsigned int i;
  572. for (i = 0; i < nbytes; i++)
  573. {
  574. contents[reloc->address + i] = relocation;
  575. relocation >>= 8;
  576. }
  577. }
  578. else
  579. r = bfd_reloc_outofrange;
  580. }
  581. return r;
  582. }
  583. #define OP(i) (contents[rel->r_offset + (i)])
  584. /* Relocate an RL78 ELF section.
  585. There is some attempt to make this function usable for many architectures,
  586. both USE_REL and USE_RELA ['twould be nice if such a critter existed],
  587. if only to serve as a learning tool.
  588. The RELOCATE_SECTION function is called by the new ELF backend linker
  589. to handle the relocations for a section.
  590. The relocs are always passed as Rela structures; if the section
  591. actually uses Rel structures, the r_addend field will always be
  592. zero.
  593. This function is responsible for adjusting the section contents as
  594. necessary, and (if using Rela relocs and generating a relocatable
  595. output file) adjusting the reloc addend as necessary.
  596. This function does not have to worry about setting the reloc
  597. address or the reloc symbol index.
  598. LOCAL_SYMS is a pointer to the swapped in local symbols.
  599. LOCAL_SECTIONS is an array giving the section in the input file
  600. corresponding to the st_shndx field of each local symbol.
  601. The global hash table entry for the global symbols can be found
  602. via elf_sym_hashes (input_bfd).
  603. When generating relocatable output, this function must handle
  604. STB_LOCAL/STT_SECTION symbols specially. The output symbol is
  605. going to be the section symbol corresponding to the output
  606. section, which means that the addend must be adjusted
  607. accordingly. */
  608. static int
  609. rl78_elf_relocate_section
  610. (bfd * output_bfd,
  611. struct bfd_link_info * info,
  612. bfd * input_bfd,
  613. asection * input_section,
  614. bfd_byte * contents,
  615. Elf_Internal_Rela * relocs,
  616. Elf_Internal_Sym * local_syms,
  617. asection ** local_sections)
  618. {
  619. Elf_Internal_Shdr * symtab_hdr;
  620. struct elf_link_hash_entry ** sym_hashes;
  621. Elf_Internal_Rela * rel;
  622. Elf_Internal_Rela * relend;
  623. asection *splt;
  624. bool ret;
  625. symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
  626. sym_hashes = elf_sym_hashes (input_bfd);
  627. relend = relocs + input_section->reloc_count;
  628. splt = elf_hash_table (info)->splt;
  629. ret = true;
  630. for (rel = relocs; rel < relend; rel ++)
  631. {
  632. reloc_howto_type *howto;
  633. unsigned long r_symndx;
  634. Elf_Internal_Sym *sym;
  635. asection *sec;
  636. struct elf_link_hash_entry *h;
  637. bfd_vma relocation;
  638. bfd_reloc_status_type r;
  639. const char *name = NULL;
  640. bool unresolved_reloc = true;
  641. int r_type;
  642. char *error_message;
  643. r_type = ELF32_R_TYPE (rel->r_info);
  644. r_symndx = ELF32_R_SYM (rel->r_info);
  645. howto = rl78_elf_howto_table + ELF32_R_TYPE (rel->r_info);
  646. h = NULL;
  647. sym = NULL;
  648. sec = NULL;
  649. relocation = 0;
  650. if (r_symndx < symtab_hdr->sh_info)
  651. {
  652. sym = local_syms + r_symndx;
  653. sec = local_sections [r_symndx];
  654. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, & sec, rel);
  655. name = bfd_elf_string_from_elf_section
  656. (input_bfd, symtab_hdr->sh_link, sym->st_name);
  657. name = sym->st_name == 0 ? bfd_section_name (sec) : name;
  658. }
  659. else
  660. {
  661. bool warned ATTRIBUTE_UNUSED;
  662. bool ignored ATTRIBUTE_UNUSED;
  663. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  664. r_symndx, symtab_hdr, sym_hashes, h,
  665. sec, relocation, unresolved_reloc,
  666. warned, ignored);
  667. name = h->root.root.string;
  668. }
  669. if (sec != NULL && discarded_section (sec))
  670. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  671. rel, 1, relend, howto, 0, contents);
  672. if (bfd_link_relocatable (info))
  673. {
  674. /* This is a relocatable link. We don't have to change
  675. anything, unless the reloc is against a section symbol,
  676. in which case we have to adjust according to where the
  677. section symbol winds up in the output section. */
  678. if (sym != NULL && ELF_ST_TYPE (sym->st_info) == STT_SECTION)
  679. rel->r_addend += sec->output_offset;
  680. continue;
  681. }
  682. switch (ELF32_R_TYPE (rel->r_info))
  683. {
  684. case R_RL78_DIR16S:
  685. {
  686. bfd_vma *plt_offset;
  687. if (h != NULL)
  688. plt_offset = &h->plt.offset;
  689. else
  690. plt_offset = elf_local_got_offsets (input_bfd) + r_symndx;
  691. if (! valid_16bit_address (relocation))
  692. {
  693. /* If this is the first time we've processed this symbol,
  694. fill in the plt entry with the correct symbol address. */
  695. if ((*plt_offset & 1) == 0)
  696. {
  697. unsigned int x;
  698. x = 0x000000ec; /* br !!abs24 */
  699. x |= (relocation << 8) & 0xffffff00;
  700. bfd_put_32 (input_bfd, x, splt->contents + *plt_offset);
  701. *plt_offset |= 1;
  702. }
  703. relocation = (splt->output_section->vma
  704. + splt->output_offset
  705. + (*plt_offset & -2));
  706. if (name)
  707. {
  708. char *newname = bfd_malloc (strlen(name)+5);
  709. strcpy (newname, name);
  710. strcat(newname, ".plt");
  711. _bfd_generic_link_add_one_symbol (info,
  712. input_bfd,
  713. newname,
  714. BSF_FUNCTION | BSF_WEAK,
  715. splt,
  716. (*plt_offset & -2),
  717. 0,
  718. 1,
  719. 0,
  720. 0);
  721. }
  722. }
  723. }
  724. break;
  725. }
  726. if (h != NULL && h->root.type == bfd_link_hash_undefweak)
  727. /* If the symbol is undefined and weak
  728. then the relocation resolves to zero. */
  729. relocation = 0;
  730. else
  731. {
  732. if (howto->pc_relative)
  733. {
  734. relocation -= (input_section->output_section->vma
  735. + input_section->output_offset
  736. + rel->r_offset);
  737. relocation -= bfd_get_reloc_size (howto);
  738. }
  739. relocation += rel->r_addend;
  740. }
  741. r = bfd_reloc_ok;
  742. if (howto->bitsize != 0
  743. && (rel->r_offset >= input_section->size
  744. || ((howto->bitsize + 7u) / 8
  745. > input_section->size - rel->r_offset)))
  746. r = bfd_reloc_outofrange;
  747. else
  748. switch (r_type)
  749. {
  750. case R_RL78_NONE:
  751. break;
  752. case R_RL78_RH_RELAX:
  753. break;
  754. case R_RL78_DIR8S_PCREL:
  755. OP (0) = relocation;
  756. break;
  757. case R_RL78_DIR8S:
  758. OP (0) = relocation;
  759. break;
  760. case R_RL78_DIR8U:
  761. OP (0) = relocation;
  762. break;
  763. case R_RL78_DIR16S_PCREL:
  764. OP (0) = relocation;
  765. OP (1) = relocation >> 8;
  766. break;
  767. case R_RL78_DIR16S:
  768. if ((relocation & 0xf0000) == 0xf0000)
  769. relocation &= 0xffff;
  770. OP (0) = relocation;
  771. OP (1) = relocation >> 8;
  772. break;
  773. case R_RL78_DIR16U:
  774. OP (0) = relocation;
  775. OP (1) = relocation >> 8;
  776. break;
  777. case R_RL78_DIR16:
  778. OP (0) = relocation;
  779. OP (1) = relocation >> 8;
  780. break;
  781. case R_RL78_DIR16_REV:
  782. OP (1) = relocation;
  783. OP (0) = relocation >> 8;
  784. break;
  785. case R_RL78_DIR3U_PCREL:
  786. OP (0) &= 0xf8;
  787. OP (0) |= relocation & 0x07;
  788. /* Map [3, 10] to [0, 7]. The code below using howto
  789. bitsize will check for unsigned overflow. */
  790. relocation -= 3;
  791. break;
  792. case R_RL78_DIR24S_PCREL:
  793. OP (0) = relocation;
  794. OP (1) = relocation >> 8;
  795. OP (2) = relocation >> 16;
  796. break;
  797. case R_RL78_DIR24S:
  798. OP (0) = relocation;
  799. OP (1) = relocation >> 8;
  800. OP (2) = relocation >> 16;
  801. break;
  802. case R_RL78_DIR32:
  803. OP (0) = relocation;
  804. OP (1) = relocation >> 8;
  805. OP (2) = relocation >> 16;
  806. OP (3) = relocation >> 24;
  807. break;
  808. case R_RL78_DIR32_REV:
  809. OP (3) = relocation;
  810. OP (2) = relocation >> 8;
  811. OP (1) = relocation >> 16;
  812. OP (0) = relocation >> 24;
  813. break;
  814. case R_RL78_RH_SFR:
  815. relocation -= 0xfff00;
  816. OP (0) = relocation;
  817. break;
  818. case R_RL78_RH_SADDR:
  819. relocation -= 0xffe20;
  820. OP (0) = relocation;
  821. break;
  822. /* Complex reloc handling: */
  823. case R_RL78_ABS32:
  824. case R_RL78_ABS32_REV:
  825. case R_RL78_ABS24S_PCREL:
  826. case R_RL78_ABS24S:
  827. case R_RL78_ABS16:
  828. case R_RL78_ABS16_REV:
  829. case R_RL78_ABS16S_PCREL:
  830. case R_RL78_ABS16S:
  831. case R_RL78_ABS16U:
  832. case R_RL78_ABS16UL:
  833. case R_RL78_ABS16UW:
  834. case R_RL78_ABS8:
  835. case R_RL78_ABS8U:
  836. case R_RL78_ABS8UL:
  837. case R_RL78_ABS8UW:
  838. case R_RL78_ABS8S_PCREL:
  839. case R_RL78_ABS8S:
  840. case R_RL78_OPneg:
  841. case R_RL78_OPadd:
  842. case R_RL78_OPsub:
  843. case R_RL78_OPmul:
  844. case R_RL78_OPdiv:
  845. case R_RL78_OPshla:
  846. case R_RL78_OPshra:
  847. case R_RL78_OPsctsize:
  848. case R_RL78_OPscttop:
  849. case R_RL78_OPand:
  850. case R_RL78_OPor:
  851. case R_RL78_OPxor:
  852. case R_RL78_OPnot:
  853. case R_RL78_OPmod:
  854. relocation = rl78_compute_complex_reloc (r_type, 0, input_section,
  855. &r, &error_message);
  856. switch (r_type)
  857. {
  858. case R_RL78_ABS32:
  859. OP (0) = relocation;
  860. OP (1) = relocation >> 8;
  861. OP (2) = relocation >> 16;
  862. OP (3) = relocation >> 24;
  863. break;
  864. case R_RL78_ABS32_REV:
  865. OP (3) = relocation;
  866. OP (2) = relocation >> 8;
  867. OP (1) = relocation >> 16;
  868. OP (0) = relocation >> 24;
  869. break;
  870. case R_RL78_ABS24S_PCREL:
  871. case R_RL78_ABS24S:
  872. OP (0) = relocation;
  873. OP (1) = relocation >> 8;
  874. OP (2) = relocation >> 16;
  875. break;
  876. case R_RL78_ABS16:
  877. OP (0) = relocation;
  878. OP (1) = relocation >> 8;
  879. break;
  880. case R_RL78_ABS16_REV:
  881. OP (1) = relocation;
  882. OP (0) = relocation >> 8;
  883. break;
  884. case R_RL78_ABS16S_PCREL:
  885. case R_RL78_ABS16S:
  886. OP (0) = relocation;
  887. OP (1) = relocation >> 8;
  888. break;
  889. case R_RL78_ABS16U:
  890. case R_RL78_ABS16UL:
  891. case R_RL78_ABS16UW:
  892. OP (0) = relocation;
  893. OP (1) = relocation >> 8;
  894. break;
  895. case R_RL78_ABS8:
  896. OP (0) = relocation;
  897. break;
  898. case R_RL78_ABS8U:
  899. case R_RL78_ABS8UL:
  900. case R_RL78_ABS8UW:
  901. OP (0) = relocation;
  902. break;
  903. case R_RL78_ABS8S_PCREL:
  904. case R_RL78_ABS8S:
  905. OP (0) = relocation;
  906. break;
  907. default:
  908. break;
  909. }
  910. break;
  911. case R_RL78_SYM:
  912. if (r_symndx < symtab_hdr->sh_info)
  913. relocation = sec->output_section->vma + sec->output_offset
  914. + sym->st_value + rel->r_addend;
  915. else if (h != NULL
  916. && (h->root.type == bfd_link_hash_defined
  917. || h->root.type == bfd_link_hash_defweak))
  918. relocation = h->root.u.def.value
  919. + sec->output_section->vma
  920. + sec->output_offset
  921. + rel->r_addend;
  922. else
  923. {
  924. relocation = 0;
  925. if (h->root.type != bfd_link_hash_undefweak)
  926. _bfd_error_handler
  927. (_("warning: RL78_SYM reloc with an unknown symbol"));
  928. }
  929. (void) rl78_compute_complex_reloc (r_type, relocation, input_section,
  930. &r, &error_message);
  931. break;
  932. case R_RL78_OPromtop:
  933. relocation = get_romstart (info, input_bfd, input_section,
  934. rel->r_offset);
  935. (void) rl78_compute_complex_reloc (r_type, relocation, input_section,
  936. &r, &error_message);
  937. break;
  938. case R_RL78_OPramtop:
  939. relocation = get_ramstart (info, input_bfd, input_section,
  940. rel->r_offset);
  941. (void) rl78_compute_complex_reloc (r_type, relocation, input_section,
  942. &r, &error_message);
  943. break;
  944. default:
  945. r = bfd_reloc_notsupported;
  946. break;
  947. }
  948. if (r == bfd_reloc_ok)
  949. r = check_overflow (howto, relocation);
  950. if (r != bfd_reloc_ok)
  951. {
  952. switch (r)
  953. {
  954. case bfd_reloc_overflow:
  955. (*info->callbacks->reloc_overflow)
  956. (info, (h ? &h->root : NULL), name, howto->name, (bfd_vma) 0,
  957. input_bfd, input_section, rel->r_offset);
  958. break;
  959. case bfd_reloc_undefined:
  960. (*info->callbacks->undefined_symbol)
  961. (info, name, input_bfd, input_section, rel->r_offset, true);
  962. break;
  963. case bfd_reloc_outofrange:
  964. /* xgettext:c-format */
  965. (*info->callbacks->einfo)
  966. (_("%H: %s out of range\n"),
  967. input_bfd, input_section, rel->r_offset, howto->name);
  968. break;
  969. case bfd_reloc_notsupported:
  970. /* xgettext:c-format */
  971. (*info->callbacks->einfo)
  972. (_("%H: relocation type %u is not supported\n"),
  973. input_bfd, input_section, rel->r_offset, r_type);
  974. break;
  975. case bfd_reloc_dangerous:
  976. (*info->callbacks->reloc_dangerous)
  977. (info, error_message, input_bfd, input_section, rel->r_offset);
  978. break;
  979. default:
  980. /* xgettext:c-format */
  981. (*info->callbacks->einfo)
  982. (_("%H: relocation %s returns an unrecognized value %x\n"),
  983. input_bfd, input_section, rel->r_offset, howto->name, r);
  984. break;
  985. }
  986. ret = false;
  987. }
  988. }
  989. return ret;
  990. }
  991. /* Function to set the ELF flag bits. */
  992. static bool
  993. rl78_elf_set_private_flags (bfd * abfd, flagword flags)
  994. {
  995. elf_elfheader (abfd)->e_flags = flags;
  996. elf_flags_init (abfd) = true;
  997. return true;
  998. }
  999. static bool no_warn_mismatch = false;
  1000. void bfd_elf32_rl78_set_target_flags (bool);
  1001. void
  1002. bfd_elf32_rl78_set_target_flags (bool user_no_warn_mismatch)
  1003. {
  1004. no_warn_mismatch = user_no_warn_mismatch;
  1005. }
  1006. static const char *
  1007. rl78_cpu_name (flagword flags)
  1008. {
  1009. switch (flags & E_FLAG_RL78_CPU_MASK)
  1010. {
  1011. default: return "";
  1012. case E_FLAG_RL78_G10: return "G10";
  1013. case E_FLAG_RL78_G13: return "G13";
  1014. case E_FLAG_RL78_G14: return "G14";
  1015. }
  1016. }
  1017. /* Merge backend specific data from an object file to the output
  1018. object file when linking. */
  1019. static bool
  1020. rl78_elf_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  1021. {
  1022. bfd *obfd = info->output_bfd;
  1023. flagword new_flags;
  1024. flagword old_flags;
  1025. bool error = false;
  1026. new_flags = elf_elfheader (ibfd)->e_flags;
  1027. old_flags = elf_elfheader (obfd)->e_flags;
  1028. if (!elf_flags_init (obfd))
  1029. {
  1030. /* First call, no flags set. */
  1031. elf_flags_init (obfd) = true;
  1032. elf_elfheader (obfd)->e_flags = new_flags;
  1033. }
  1034. else if (old_flags != new_flags)
  1035. {
  1036. flagword changed_flags = old_flags ^ new_flags;
  1037. if (changed_flags & E_FLAG_RL78_CPU_MASK)
  1038. {
  1039. flagword out_cpu = old_flags & E_FLAG_RL78_CPU_MASK;
  1040. flagword in_cpu = new_flags & E_FLAG_RL78_CPU_MASK;
  1041. if (in_cpu == E_FLAG_RL78_ANY_CPU || in_cpu == out_cpu)
  1042. /* It does not matter what new_cpu may have. */;
  1043. else if (out_cpu == E_FLAG_RL78_ANY_CPU)
  1044. {
  1045. if (in_cpu == E_FLAG_RL78_G10)
  1046. {
  1047. /* G10 files can only be linked with other G10 files.
  1048. If the output is set to "any" this means that it is
  1049. a G14 file that does not use hardware multiply/divide,
  1050. but that is still incompatible with the G10 ABI. */
  1051. error = true;
  1052. _bfd_error_handler
  1053. /* xgettext:c-format */
  1054. (_("RL78 ABI conflict: G10 file %pB cannot be linked"
  1055. " with %s file %pB"),
  1056. ibfd, rl78_cpu_name (out_cpu), obfd);
  1057. }
  1058. else
  1059. {
  1060. old_flags &= ~ E_FLAG_RL78_CPU_MASK;
  1061. old_flags |= in_cpu;
  1062. elf_elfheader (obfd)->e_flags = old_flags;
  1063. }
  1064. }
  1065. else
  1066. {
  1067. error = true;
  1068. _bfd_error_handler
  1069. /* xgettext:c-format */
  1070. (_("RL78 ABI conflict: cannot link %s file %pB with %s file %pB"),
  1071. rl78_cpu_name (in_cpu), ibfd,
  1072. rl78_cpu_name (out_cpu), obfd);
  1073. }
  1074. }
  1075. if (changed_flags & E_FLAG_RL78_64BIT_DOUBLES)
  1076. {
  1077. _bfd_error_handler
  1078. (_("RL78 merge conflict: cannot link 32-bit and 64-bit objects together"));
  1079. if (old_flags & E_FLAG_RL78_64BIT_DOUBLES)
  1080. /* xgettext:c-format */
  1081. _bfd_error_handler (_("- %pB is 64-bit, %pB is not"),
  1082. obfd, ibfd);
  1083. else
  1084. /* xgettext:c-format */
  1085. _bfd_error_handler (_("- %pB is 64-bit, %pB is not"),
  1086. ibfd, obfd);
  1087. error = true;
  1088. }
  1089. }
  1090. return !error;
  1091. }
  1092. static bool
  1093. rl78_elf_print_private_bfd_data (bfd * abfd, void * ptr)
  1094. {
  1095. FILE * file = (FILE *) ptr;
  1096. flagword flags;
  1097. BFD_ASSERT (abfd != NULL && ptr != NULL);
  1098. /* Print normal ELF private data. */
  1099. _bfd_elf_print_private_bfd_data (abfd, ptr);
  1100. flags = elf_elfheader (abfd)->e_flags;
  1101. fprintf (file, _("private flags = 0x%lx:"), (long) flags);
  1102. if (flags & E_FLAG_RL78_CPU_MASK)
  1103. fprintf (file, " [%s]", rl78_cpu_name (flags));
  1104. if (flags & E_FLAG_RL78_64BIT_DOUBLES)
  1105. fprintf (file, _(" [64-bit doubles]"));
  1106. fputc ('\n', file);
  1107. return true;
  1108. }
  1109. /* Return the MACH for an e_flags value. */
  1110. static int
  1111. elf32_rl78_machine (bfd * abfd ATTRIBUTE_UNUSED)
  1112. {
  1113. return bfd_mach_rl78;
  1114. }
  1115. static bool
  1116. rl78_elf_object_p (bfd * abfd)
  1117. {
  1118. bfd_default_set_arch_mach (abfd, bfd_arch_rl78,
  1119. elf32_rl78_machine (abfd));
  1120. return true;
  1121. }
  1122. /* support PLT for 16-bit references to 24-bit functions. */
  1123. /* We support 16-bit pointers to code above 64k by generating a thunk
  1124. below 64k containing a JMP instruction to the final address. */
  1125. static bool
  1126. rl78_elf_check_relocs
  1127. (bfd * abfd,
  1128. struct bfd_link_info * info,
  1129. asection * sec,
  1130. const Elf_Internal_Rela * relocs)
  1131. {
  1132. Elf_Internal_Shdr * symtab_hdr;
  1133. struct elf_link_hash_entry ** sym_hashes;
  1134. const Elf_Internal_Rela * rel;
  1135. const Elf_Internal_Rela * rel_end;
  1136. bfd_vma *local_plt_offsets;
  1137. asection *splt;
  1138. bfd *dynobj;
  1139. if (bfd_link_relocatable (info))
  1140. return true;
  1141. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1142. sym_hashes = elf_sym_hashes (abfd);
  1143. local_plt_offsets = elf_local_got_offsets (abfd);
  1144. dynobj = elf_hash_table(info)->dynobj;
  1145. rel_end = relocs + sec->reloc_count;
  1146. for (rel = relocs; rel < rel_end; rel++)
  1147. {
  1148. struct elf_link_hash_entry *h;
  1149. unsigned long r_symndx;
  1150. bfd_vma *offset;
  1151. r_symndx = ELF32_R_SYM (rel->r_info);
  1152. if (r_symndx < symtab_hdr->sh_info)
  1153. h = NULL;
  1154. else
  1155. {
  1156. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  1157. while (h->root.type == bfd_link_hash_indirect
  1158. || h->root.type == bfd_link_hash_warning)
  1159. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1160. }
  1161. switch (ELF32_R_TYPE (rel->r_info))
  1162. {
  1163. /* This relocation describes a 16-bit pointer to a function.
  1164. We may need to allocate a thunk in low memory; reserve memory
  1165. for it now. */
  1166. case R_RL78_DIR16S:
  1167. if (dynobj == NULL)
  1168. elf_hash_table (info)->dynobj = dynobj = abfd;
  1169. splt = elf_hash_table (info)->splt;
  1170. if (splt == NULL)
  1171. {
  1172. flagword flags = (SEC_ALLOC | SEC_LOAD | SEC_HAS_CONTENTS
  1173. | SEC_IN_MEMORY | SEC_LINKER_CREATED
  1174. | SEC_READONLY | SEC_CODE);
  1175. splt = bfd_make_section_anyway_with_flags (dynobj, ".plt",
  1176. flags);
  1177. elf_hash_table (info)->splt = splt;
  1178. if (splt == NULL
  1179. || !bfd_set_section_alignment (splt, 1))
  1180. return false;
  1181. }
  1182. if (h != NULL)
  1183. offset = &h->plt.offset;
  1184. else
  1185. {
  1186. if (local_plt_offsets == NULL)
  1187. {
  1188. size_t size;
  1189. unsigned int i;
  1190. size = symtab_hdr->sh_info * sizeof (bfd_vma);
  1191. local_plt_offsets = (bfd_vma *) bfd_alloc (abfd, size);
  1192. if (local_plt_offsets == NULL)
  1193. return false;
  1194. elf_local_got_offsets (abfd) = local_plt_offsets;
  1195. for (i = 0; i < symtab_hdr->sh_info; i++)
  1196. local_plt_offsets[i] = (bfd_vma) -1;
  1197. }
  1198. offset = &local_plt_offsets[r_symndx];
  1199. }
  1200. if (*offset == (bfd_vma) -1)
  1201. {
  1202. *offset = splt->size;
  1203. splt->size += 4;
  1204. }
  1205. break;
  1206. }
  1207. }
  1208. return true;
  1209. }
  1210. /* This must exist if dynobj is ever set. */
  1211. static bool
  1212. rl78_elf_finish_dynamic_sections (bfd *abfd ATTRIBUTE_UNUSED,
  1213. struct bfd_link_info *info)
  1214. {
  1215. bfd *dynobj;
  1216. asection *splt;
  1217. if (!elf_hash_table (info)->dynamic_sections_created)
  1218. return true;
  1219. /* As an extra sanity check, verify that all plt entries have been
  1220. filled in. However, relaxing might have changed the relocs so
  1221. that some plt entries don't get filled in, so we have to skip
  1222. this check if we're relaxing. Unfortunately, check_relocs is
  1223. called before relaxation. */
  1224. if (info->relax_trip > 0)
  1225. return true;
  1226. dynobj = elf_hash_table (info)->dynobj;
  1227. splt = elf_hash_table (info)->splt;
  1228. if (dynobj != NULL && splt != NULL)
  1229. {
  1230. bfd_byte *contents = splt->contents;
  1231. unsigned int i, size = splt->size;
  1232. for (i = 0; i < size; i += 4)
  1233. {
  1234. unsigned int x = bfd_get_32 (dynobj, contents + i);
  1235. BFD_ASSERT (x != 0);
  1236. }
  1237. }
  1238. return true;
  1239. }
  1240. static bool
  1241. rl78_elf_always_size_sections (bfd *output_bfd ATTRIBUTE_UNUSED,
  1242. struct bfd_link_info *info)
  1243. {
  1244. bfd *dynobj;
  1245. asection *splt;
  1246. if (bfd_link_relocatable (info))
  1247. return true;
  1248. dynobj = elf_hash_table (info)->dynobj;
  1249. if (dynobj == NULL)
  1250. return true;
  1251. splt = elf_hash_table (info)->splt;
  1252. BFD_ASSERT (splt != NULL);
  1253. splt->contents = (bfd_byte *) bfd_zalloc (dynobj, splt->size);
  1254. if (splt->contents == NULL)
  1255. return false;
  1256. return true;
  1257. }
  1258. /* Handle relaxing. */
  1259. /* A subroutine of rl78_elf_relax_section. If the global symbol H
  1260. is within the low 64k, remove any entry for it in the plt. */
  1261. struct relax_plt_data
  1262. {
  1263. asection *splt;
  1264. bool *again;
  1265. };
  1266. static bool
  1267. rl78_relax_plt_check (struct elf_link_hash_entry *h, void * xdata)
  1268. {
  1269. struct relax_plt_data *data = (struct relax_plt_data *) xdata;
  1270. if (h->plt.offset != (bfd_vma) -1)
  1271. {
  1272. bfd_vma address;
  1273. if (h->root.type == bfd_link_hash_undefined
  1274. || h->root.type == bfd_link_hash_undefweak)
  1275. address = 0;
  1276. else
  1277. address = (h->root.u.def.section->output_section->vma
  1278. + h->root.u.def.section->output_offset
  1279. + h->root.u.def.value);
  1280. if (valid_16bit_address (address))
  1281. {
  1282. h->plt.offset = -1;
  1283. data->splt->size -= 4;
  1284. *data->again = true;
  1285. }
  1286. }
  1287. return true;
  1288. }
  1289. /* A subroutine of rl78_elf_relax_section. If the global symbol H
  1290. previously had a plt entry, give it a new entry offset. */
  1291. static bool
  1292. rl78_relax_plt_realloc (struct elf_link_hash_entry *h, void * xdata)
  1293. {
  1294. bfd_vma *entry = (bfd_vma *) xdata;
  1295. if (h->plt.offset != (bfd_vma) -1)
  1296. {
  1297. h->plt.offset = *entry;
  1298. *entry += 4;
  1299. }
  1300. return true;
  1301. }
  1302. static bool
  1303. rl78_elf_relax_plt_section (bfd *dynobj,
  1304. asection *splt,
  1305. struct bfd_link_info *info,
  1306. bool *again)
  1307. {
  1308. struct relax_plt_data relax_plt_data;
  1309. bfd *ibfd;
  1310. /* Assume nothing changes. */
  1311. *again = false;
  1312. if (bfd_link_relocatable (info))
  1313. return true;
  1314. /* We only relax the .plt section at the moment. */
  1315. if (dynobj != elf_hash_table (info)->dynobj
  1316. || strcmp (splt->name, ".plt") != 0)
  1317. return true;
  1318. /* Quick check for an empty plt. */
  1319. if (splt->size == 0)
  1320. return true;
  1321. /* Map across all global symbols; see which ones happen to
  1322. fall in the low 64k. */
  1323. relax_plt_data.splt = splt;
  1324. relax_plt_data.again = again;
  1325. elf_link_hash_traverse (elf_hash_table (info), rl78_relax_plt_check,
  1326. &relax_plt_data);
  1327. /* Likewise for local symbols, though that's somewhat less convenient
  1328. as we have to walk the list of input bfds and swap in symbol data. */
  1329. for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
  1330. {
  1331. bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
  1332. Elf_Internal_Shdr *symtab_hdr;
  1333. Elf_Internal_Sym *isymbuf = NULL;
  1334. unsigned int idx;
  1335. if (! local_plt_offsets)
  1336. continue;
  1337. symtab_hdr = &elf_tdata (ibfd)->symtab_hdr;
  1338. if (symtab_hdr->sh_info != 0)
  1339. {
  1340. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  1341. if (isymbuf == NULL)
  1342. isymbuf = bfd_elf_get_elf_syms (ibfd, symtab_hdr,
  1343. symtab_hdr->sh_info, 0,
  1344. NULL, NULL, NULL);
  1345. if (isymbuf == NULL)
  1346. return false;
  1347. }
  1348. for (idx = 0; idx < symtab_hdr->sh_info; ++idx)
  1349. {
  1350. Elf_Internal_Sym *isym;
  1351. asection *tsec;
  1352. bfd_vma address;
  1353. if (local_plt_offsets[idx] == (bfd_vma) -1)
  1354. continue;
  1355. isym = &isymbuf[idx];
  1356. if (isym->st_shndx == SHN_UNDEF)
  1357. continue;
  1358. else if (isym->st_shndx == SHN_ABS)
  1359. tsec = bfd_abs_section_ptr;
  1360. else if (isym->st_shndx == SHN_COMMON)
  1361. tsec = bfd_com_section_ptr;
  1362. else
  1363. tsec = bfd_section_from_elf_index (ibfd, isym->st_shndx);
  1364. address = (tsec->output_section->vma
  1365. + tsec->output_offset
  1366. + isym->st_value);
  1367. if (valid_16bit_address (address))
  1368. {
  1369. local_plt_offsets[idx] = -1;
  1370. splt->size -= 4;
  1371. *again = true;
  1372. }
  1373. }
  1374. if (isymbuf != NULL
  1375. && symtab_hdr->contents != (unsigned char *) isymbuf)
  1376. {
  1377. if (! info->keep_memory)
  1378. free (isymbuf);
  1379. else
  1380. {
  1381. /* Cache the symbols for elf_link_input_bfd. */
  1382. symtab_hdr->contents = (unsigned char *) isymbuf;
  1383. }
  1384. }
  1385. }
  1386. /* If we changed anything, walk the symbols again to reallocate
  1387. .plt entry addresses. */
  1388. if (*again && splt->size > 0)
  1389. {
  1390. bfd_vma entry = 0;
  1391. elf_link_hash_traverse (elf_hash_table (info),
  1392. rl78_relax_plt_realloc, &entry);
  1393. for (ibfd = info->input_bfds; ibfd ; ibfd = ibfd->link.next)
  1394. {
  1395. bfd_vma *local_plt_offsets = elf_local_got_offsets (ibfd);
  1396. unsigned int nlocals = elf_tdata (ibfd)->symtab_hdr.sh_info;
  1397. unsigned int idx;
  1398. if (! local_plt_offsets)
  1399. continue;
  1400. for (idx = 0; idx < nlocals; ++idx)
  1401. if (local_plt_offsets[idx] != (bfd_vma) -1)
  1402. {
  1403. local_plt_offsets[idx] = entry;
  1404. entry += 4;
  1405. }
  1406. }
  1407. }
  1408. return true;
  1409. }
  1410. /* Delete some bytes from a section while relaxing. */
  1411. static bool
  1412. elf32_rl78_relax_delete_bytes (bfd *abfd, asection *sec, bfd_vma addr, int count,
  1413. Elf_Internal_Rela *alignment_rel, int force_snip)
  1414. {
  1415. Elf_Internal_Shdr * symtab_hdr;
  1416. unsigned int sec_shndx;
  1417. bfd_byte * contents;
  1418. Elf_Internal_Rela * irel;
  1419. Elf_Internal_Rela * irelend;
  1420. Elf_Internal_Sym * isym;
  1421. Elf_Internal_Sym * isymend;
  1422. bfd_vma toaddr;
  1423. unsigned int symcount;
  1424. struct elf_link_hash_entry ** sym_hashes;
  1425. struct elf_link_hash_entry ** end_hashes;
  1426. if (!alignment_rel)
  1427. force_snip = 1;
  1428. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  1429. contents = elf_section_data (sec)->this_hdr.contents;
  1430. /* The deletion must stop at the next alignment boundary, if
  1431. ALIGNMENT_REL is non-NULL. */
  1432. toaddr = sec->size;
  1433. if (alignment_rel)
  1434. toaddr = alignment_rel->r_offset;
  1435. irel = elf_section_data (sec)->relocs;
  1436. if (irel == NULL)
  1437. {
  1438. _bfd_elf_link_read_relocs (sec->owner, sec, NULL, NULL, true);
  1439. irel = elf_section_data (sec)->relocs;
  1440. }
  1441. irelend = irel + sec->reloc_count;
  1442. /* Actually delete the bytes. */
  1443. memmove (contents + addr, contents + addr + count,
  1444. (size_t) (toaddr - addr - count));
  1445. /* If we don't have an alignment marker to worry about, we can just
  1446. shrink the section. Otherwise, we have to fill in the newly
  1447. created gap with NOP insns (0x03). */
  1448. if (force_snip)
  1449. sec->size -= count;
  1450. else
  1451. memset (contents + toaddr - count, 0x03, count);
  1452. /* Adjust all the relocs. */
  1453. for (; irel && irel < irelend; irel++)
  1454. {
  1455. /* Get the new reloc address. */
  1456. if (irel->r_offset > addr
  1457. && (irel->r_offset < toaddr
  1458. || (force_snip && irel->r_offset == toaddr)))
  1459. irel->r_offset -= count;
  1460. /* If we see an ALIGN marker at the end of the gap, we move it
  1461. to the beginning of the gap, since marking these gaps is what
  1462. they're for. */
  1463. if (irel->r_offset == toaddr
  1464. && ELF32_R_TYPE (irel->r_info) == R_RL78_RH_RELAX
  1465. && irel->r_addend & RL78_RELAXA_ALIGN)
  1466. irel->r_offset -= count;
  1467. }
  1468. /* Adjust the local symbols defined in this section. */
  1469. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1470. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  1471. isymend = isym + symtab_hdr->sh_info;
  1472. for (; isym < isymend; isym++)
  1473. {
  1474. /* If the symbol is in the range of memory we just moved, we
  1475. have to adjust its value. */
  1476. if (isym->st_shndx == sec_shndx
  1477. && isym->st_value > addr
  1478. && isym->st_value < toaddr)
  1479. isym->st_value -= count;
  1480. /* If the symbol *spans* the bytes we just deleted (i.e. it's
  1481. *end* is in the moved bytes but it's *start* isn't), then we
  1482. must adjust its size. */
  1483. if (isym->st_shndx == sec_shndx
  1484. && isym->st_value < addr
  1485. && isym->st_value + isym->st_size > addr
  1486. && isym->st_value + isym->st_size < toaddr)
  1487. isym->st_size -= count;
  1488. }
  1489. /* Now adjust the global symbols defined in this section. */
  1490. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  1491. - symtab_hdr->sh_info);
  1492. sym_hashes = elf_sym_hashes (abfd);
  1493. end_hashes = sym_hashes + symcount;
  1494. for (; sym_hashes < end_hashes; sym_hashes++)
  1495. {
  1496. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  1497. if ((sym_hash->root.type == bfd_link_hash_defined
  1498. || sym_hash->root.type == bfd_link_hash_defweak)
  1499. && sym_hash->root.u.def.section == sec)
  1500. {
  1501. /* As above, adjust the value if needed. */
  1502. if (sym_hash->root.u.def.value > addr
  1503. && sym_hash->root.u.def.value < toaddr)
  1504. sym_hash->root.u.def.value -= count;
  1505. /* As above, adjust the size if needed. */
  1506. if (sym_hash->root.u.def.value < addr
  1507. && sym_hash->root.u.def.value + sym_hash->size > addr
  1508. && sym_hash->root.u.def.value + sym_hash->size < toaddr)
  1509. sym_hash->size -= count;
  1510. }
  1511. }
  1512. return true;
  1513. }
  1514. /* Used to sort relocs by address. If relocs have the same address,
  1515. we maintain their relative order, except that R_RL78_RH_RELAX
  1516. alignment relocs must be the first reloc for any given address. */
  1517. static void
  1518. reloc_bubblesort (Elf_Internal_Rela * r, int count)
  1519. {
  1520. int i;
  1521. bool again;
  1522. bool swappit;
  1523. /* This is almost a classic bubblesort. It's the slowest sort, but
  1524. we're taking advantage of the fact that the relocations are
  1525. mostly in order already (the assembler emits them that way) and
  1526. we need relocs with the same address to remain in the same
  1527. relative order. */
  1528. again = true;
  1529. while (again)
  1530. {
  1531. again = false;
  1532. for (i = 0; i < count - 1; i ++)
  1533. {
  1534. if (r[i].r_offset > r[i + 1].r_offset)
  1535. swappit = true;
  1536. else if (r[i].r_offset < r[i + 1].r_offset)
  1537. swappit = false;
  1538. else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RL78_RH_RELAX
  1539. && (r[i + 1].r_addend & RL78_RELAXA_ALIGN))
  1540. swappit = true;
  1541. else if (ELF32_R_TYPE (r[i + 1].r_info) == R_RL78_RH_RELAX
  1542. && (r[i + 1].r_addend & RL78_RELAXA_ELIGN)
  1543. && !(ELF32_R_TYPE (r[i].r_info) == R_RL78_RH_RELAX
  1544. && (r[i].r_addend & RL78_RELAXA_ALIGN)))
  1545. swappit = true;
  1546. else
  1547. swappit = false;
  1548. if (swappit)
  1549. {
  1550. Elf_Internal_Rela tmp;
  1551. tmp = r[i];
  1552. r[i] = r[i + 1];
  1553. r[i + 1] = tmp;
  1554. /* If we do move a reloc back, re-scan to see if it
  1555. needs to be moved even further back. This avoids
  1556. most of the O(n^2) behavior for our cases. */
  1557. if (i > 0)
  1558. i -= 2;
  1559. again = true;
  1560. }
  1561. }
  1562. }
  1563. }
  1564. #define OFFSET_FOR_RELOC(rel, lrel, scale) \
  1565. rl78_offset_for_reloc (abfd, rel + 1, symtab_hdr, shndx_buf, intsyms, \
  1566. lrel, abfd, sec, link_info, scale)
  1567. static bfd_vma
  1568. rl78_offset_for_reloc (bfd * abfd,
  1569. Elf_Internal_Rela * rel,
  1570. Elf_Internal_Shdr * symtab_hdr,
  1571. bfd_byte * shndx_buf ATTRIBUTE_UNUSED,
  1572. Elf_Internal_Sym * intsyms,
  1573. Elf_Internal_Rela ** lrel,
  1574. bfd * input_bfd,
  1575. asection * input_section,
  1576. struct bfd_link_info * info,
  1577. int * scale)
  1578. {
  1579. bfd_vma symval;
  1580. *scale = 1;
  1581. /* REL is the first of 1..N relocations. We compute the symbol
  1582. value for each relocation, then combine them if needed. LREL
  1583. gets a pointer to the last relocation used. */
  1584. while (1)
  1585. {
  1586. unsigned long r_type;
  1587. /* Get the value of the symbol referred to by the reloc. */
  1588. if (ELF32_R_SYM (rel->r_info) < symtab_hdr->sh_info)
  1589. {
  1590. /* A local symbol. */
  1591. Elf_Internal_Sym *isym;
  1592. asection *ssec;
  1593. isym = intsyms + ELF32_R_SYM (rel->r_info);
  1594. if (isym->st_shndx == SHN_UNDEF)
  1595. ssec = bfd_und_section_ptr;
  1596. else if (isym->st_shndx == SHN_ABS)
  1597. ssec = bfd_abs_section_ptr;
  1598. else if (isym->st_shndx == SHN_COMMON)
  1599. ssec = bfd_com_section_ptr;
  1600. else
  1601. ssec = bfd_section_from_elf_index (abfd,
  1602. isym->st_shndx);
  1603. /* Initial symbol value. */
  1604. symval = isym->st_value;
  1605. /* GAS may have made this symbol relative to a section, in
  1606. which case, we have to add the addend to find the
  1607. symbol. */
  1608. if (ELF_ST_TYPE (isym->st_info) == STT_SECTION)
  1609. symval += rel->r_addend;
  1610. if (ssec)
  1611. {
  1612. if ((ssec->flags & SEC_MERGE)
  1613. && ssec->sec_info_type == SEC_INFO_TYPE_MERGE)
  1614. symval = _bfd_merged_section_offset (abfd, & ssec,
  1615. elf_section_data (ssec)->sec_info,
  1616. symval);
  1617. }
  1618. /* Now make the offset relative to where the linker is putting it. */
  1619. if (ssec)
  1620. symval +=
  1621. ssec->output_section->vma + ssec->output_offset;
  1622. symval += rel->r_addend;
  1623. }
  1624. else
  1625. {
  1626. unsigned long indx;
  1627. struct elf_link_hash_entry * h;
  1628. /* An external symbol. */
  1629. indx = ELF32_R_SYM (rel->r_info) - symtab_hdr->sh_info;
  1630. h = elf_sym_hashes (abfd)[indx];
  1631. BFD_ASSERT (h != NULL);
  1632. if (h->root.type != bfd_link_hash_defined
  1633. && h->root.type != bfd_link_hash_defweak)
  1634. {
  1635. /* This appears to be a reference to an undefined
  1636. symbol. Just ignore it--it will be caught by the
  1637. regular reloc processing. */
  1638. if (lrel)
  1639. *lrel = rel;
  1640. return 0;
  1641. }
  1642. symval = (h->root.u.def.value
  1643. + h->root.u.def.section->output_section->vma
  1644. + h->root.u.def.section->output_offset);
  1645. symval += rel->r_addend;
  1646. }
  1647. r_type = ELF32_R_TYPE (rel->r_info);
  1648. switch (r_type)
  1649. {
  1650. case R_RL78_SYM:
  1651. (void) rl78_compute_complex_reloc (r_type, symval, input_section,
  1652. NULL, NULL);
  1653. break;
  1654. case R_RL78_OPromtop:
  1655. symval = get_romstart (info, input_bfd, input_section, rel->r_offset);
  1656. (void) rl78_compute_complex_reloc (r_type, symval, input_section,
  1657. NULL, NULL);
  1658. break;
  1659. case R_RL78_OPramtop:
  1660. symval = get_ramstart (info, input_bfd, input_section, rel->r_offset);
  1661. (void) rl78_compute_complex_reloc (r_type, symval, input_section,
  1662. NULL, NULL);
  1663. break;
  1664. case R_RL78_OPneg:
  1665. case R_RL78_OPadd:
  1666. case R_RL78_OPsub:
  1667. case R_RL78_OPmul:
  1668. case R_RL78_OPdiv:
  1669. case R_RL78_OPshla:
  1670. case R_RL78_OPshra:
  1671. case R_RL78_OPsctsize:
  1672. case R_RL78_OPscttop:
  1673. case R_RL78_OPand:
  1674. case R_RL78_OPor:
  1675. case R_RL78_OPxor:
  1676. case R_RL78_OPnot:
  1677. case R_RL78_OPmod:
  1678. (void) rl78_compute_complex_reloc (r_type, 0, input_section,
  1679. NULL, NULL);
  1680. break;
  1681. case R_RL78_DIR16UL:
  1682. case R_RL78_DIR8UL:
  1683. case R_RL78_ABS16UL:
  1684. case R_RL78_ABS8UL:
  1685. *scale = 4;
  1686. goto reloc_computes_value;
  1687. case R_RL78_DIR16UW:
  1688. case R_RL78_DIR8UW:
  1689. case R_RL78_ABS16UW:
  1690. case R_RL78_ABS8UW:
  1691. *scale = 2;
  1692. goto reloc_computes_value;
  1693. default:
  1694. reloc_computes_value:
  1695. symval = rl78_compute_complex_reloc (r_type, symval, input_section,
  1696. NULL, NULL);
  1697. /* Fall through. */
  1698. case R_RL78_DIR32:
  1699. case R_RL78_DIR24S:
  1700. case R_RL78_DIR16:
  1701. case R_RL78_DIR16U:
  1702. case R_RL78_DIR16S:
  1703. case R_RL78_DIR24S_PCREL:
  1704. case R_RL78_DIR16S_PCREL:
  1705. case R_RL78_DIR8S_PCREL:
  1706. if (lrel)
  1707. *lrel = rel;
  1708. return symval;
  1709. }
  1710. rel ++;
  1711. }
  1712. }
  1713. const struct {
  1714. int prefix; /* or -1 for "no prefix" */
  1715. int insn; /* or -1 for "end of list" */
  1716. int insn_for_saddr; /* or -1 for "no alternative" */
  1717. int insn_for_sfr; /* or -1 for "no alternative" */
  1718. } relax_addr16[] = {
  1719. { -1, 0x02, 0x06, -1 }, /* ADDW AX, !addr16 */
  1720. { -1, 0x22, 0x26, -1 }, /* SUBW AX, !addr16 */
  1721. { -1, 0x42, 0x46, -1 }, /* CMPW AX, !addr16 */
  1722. { -1, 0x40, 0x4a, -1 }, /* CMP !addr16, #byte */
  1723. { -1, 0x0f, 0x0b, -1 }, /* ADD A, !addr16 */
  1724. { -1, 0x1f, 0x1b, -1 }, /* ADDC A, !addr16 */
  1725. { -1, 0x2f, 0x2b, -1 }, /* SUB A, !addr16 */
  1726. { -1, 0x3f, 0x3b, -1 }, /* SUBC A, !addr16 */
  1727. { -1, 0x4f, 0x4b, -1 }, /* CMP A, !addr16 */
  1728. { -1, 0x5f, 0x5b, -1 }, /* AND A, !addr16 */
  1729. { -1, 0x6f, 0x6b, -1 }, /* OR A, !addr16 */
  1730. { -1, 0x7f, 0x7b, -1 }, /* XOR A, !addr16 */
  1731. { -1, 0x8f, 0x8d, 0x8e }, /* MOV A, !addr16 */
  1732. { -1, 0x9f, 0x9d, 0x9e }, /* MOV !addr16, A */
  1733. { -1, 0xaf, 0xad, 0xae }, /* MOVW AX, !addr16 */
  1734. { -1, 0xbf, 0xbd, 0xbe }, /* MOVW !addr16, AX */
  1735. { -1, 0xcf, 0xcd, 0xce }, /* MOVW !addr16, #word */
  1736. { -1, 0xa0, 0xa4, -1 }, /* INC !addr16 */
  1737. { -1, 0xa2, 0xa6, -1 }, /* INCW !addr16 */
  1738. { -1, 0xb0, 0xb4, -1 }, /* DEC !addr16 */
  1739. { -1, 0xb2, 0xb6, -1 }, /* DECW !addr16 */
  1740. { -1, 0xd5, 0xd4, -1 }, /* CMP0 !addr16 */
  1741. { -1, 0xe5, 0xe4, -1 }, /* ONEB !addr16 */
  1742. { -1, 0xf5, 0xf4, -1 }, /* CLRB !addr16 */
  1743. { -1, 0xd9, 0xd8, -1 }, /* MOV X, !addr16 */
  1744. { -1, 0xe9, 0xe8, -1 }, /* MOV B, !addr16 */
  1745. { -1, 0xf9, 0xf8, -1 }, /* MOV C, !addr16 */
  1746. { -1, 0xdb, 0xda, -1 }, /* MOVW BC, !addr16 */
  1747. { -1, 0xeb, 0xea, -1 }, /* MOVW DE, !addr16 */
  1748. { -1, 0xfb, 0xfa, -1 }, /* MOVW HL, !addr16 */
  1749. { 0x61, 0xaa, 0xa8, -1 }, /* XCH A, !addr16 */
  1750. { 0x71, 0x00, 0x02, 0x0a }, /* SET1 !addr16.0 */
  1751. { 0x71, 0x10, 0x12, 0x1a }, /* SET1 !addr16.0 */
  1752. { 0x71, 0x20, 0x22, 0x2a }, /* SET1 !addr16.0 */
  1753. { 0x71, 0x30, 0x32, 0x3a }, /* SET1 !addr16.0 */
  1754. { 0x71, 0x40, 0x42, 0x4a }, /* SET1 !addr16.0 */
  1755. { 0x71, 0x50, 0x52, 0x5a }, /* SET1 !addr16.0 */
  1756. { 0x71, 0x60, 0x62, 0x6a }, /* SET1 !addr16.0 */
  1757. { 0x71, 0x70, 0x72, 0x7a }, /* SET1 !addr16.0 */
  1758. { 0x71, 0x08, 0x03, 0x0b }, /* CLR1 !addr16.0 */
  1759. { 0x71, 0x18, 0x13, 0x1b }, /* CLR1 !addr16.0 */
  1760. { 0x71, 0x28, 0x23, 0x2b }, /* CLR1 !addr16.0 */
  1761. { 0x71, 0x38, 0x33, 0x3b }, /* CLR1 !addr16.0 */
  1762. { 0x71, 0x48, 0x43, 0x4b }, /* CLR1 !addr16.0 */
  1763. { 0x71, 0x58, 0x53, 0x5b }, /* CLR1 !addr16.0 */
  1764. { 0x71, 0x68, 0x63, 0x6b }, /* CLR1 !addr16.0 */
  1765. { 0x71, 0x78, 0x73, 0x7b }, /* CLR1 !addr16.0 */
  1766. { -1, -1, -1, -1 }
  1767. };
  1768. /* Relax one section. */
  1769. static bool
  1770. rl78_elf_relax_section (bfd *abfd,
  1771. asection *sec,
  1772. struct bfd_link_info *link_info,
  1773. bool *again)
  1774. {
  1775. Elf_Internal_Shdr * symtab_hdr;
  1776. Elf_Internal_Shdr * shndx_hdr;
  1777. Elf_Internal_Rela * internal_relocs;
  1778. Elf_Internal_Rela * free_relocs = NULL;
  1779. Elf_Internal_Rela * irel;
  1780. Elf_Internal_Rela * srel;
  1781. Elf_Internal_Rela * irelend;
  1782. Elf_Internal_Rela * next_alignment;
  1783. bfd_byte * contents = NULL;
  1784. bfd_byte * free_contents = NULL;
  1785. Elf_Internal_Sym * intsyms = NULL;
  1786. Elf_Internal_Sym * free_intsyms = NULL;
  1787. bfd_byte * shndx_buf = NULL;
  1788. bfd_vma pc;
  1789. bfd_vma symval ATTRIBUTE_UNUSED = 0;
  1790. int pcrel ATTRIBUTE_UNUSED = 0;
  1791. int code ATTRIBUTE_UNUSED = 0;
  1792. int section_alignment_glue;
  1793. int scale;
  1794. if (abfd == elf_hash_table (link_info)->dynobj
  1795. && strcmp (sec->name, ".plt") == 0)
  1796. return rl78_elf_relax_plt_section (abfd, sec, link_info, again);
  1797. /* Assume nothing changes. */
  1798. *again = false;
  1799. /* We don't have to do anything for a relocatable link, if
  1800. this section does not have relocs, or if this is not a
  1801. code section. */
  1802. if (bfd_link_relocatable (link_info)
  1803. || (sec->flags & SEC_RELOC) == 0
  1804. || sec->reloc_count == 0
  1805. || (sec->flags & SEC_CODE) == 0)
  1806. return true;
  1807. symtab_hdr = & elf_symtab_hdr (abfd);
  1808. if (elf_symtab_shndx_list (abfd))
  1809. shndx_hdr = & elf_symtab_shndx_list (abfd)->hdr;
  1810. else
  1811. shndx_hdr = NULL;
  1812. /* Get the section contents. */
  1813. if (elf_section_data (sec)->this_hdr.contents != NULL)
  1814. contents = elf_section_data (sec)->this_hdr.contents;
  1815. /* Go get them off disk. */
  1816. else
  1817. {
  1818. if (! bfd_malloc_and_get_section (abfd, sec, &contents))
  1819. goto error_return;
  1820. elf_section_data (sec)->this_hdr.contents = contents;
  1821. }
  1822. /* Read this BFD's symbols. */
  1823. /* Get cached copy if it exists. */
  1824. if (symtab_hdr->contents != NULL)
  1825. intsyms = (Elf_Internal_Sym *) symtab_hdr->contents;
  1826. else
  1827. {
  1828. intsyms = bfd_elf_get_elf_syms (abfd, symtab_hdr, symtab_hdr->sh_info, 0, NULL, NULL, NULL);
  1829. symtab_hdr->contents = (bfd_byte *) intsyms;
  1830. }
  1831. if (shndx_hdr && shndx_hdr->sh_size != 0)
  1832. {
  1833. size_t amt;
  1834. if (_bfd_mul_overflow (symtab_hdr->sh_info,
  1835. sizeof (Elf_External_Sym_Shndx), &amt))
  1836. {
  1837. bfd_set_error (bfd_error_no_memory);
  1838. goto error_return;
  1839. }
  1840. if (bfd_seek (abfd, shndx_hdr->sh_offset, SEEK_SET) != 0)
  1841. goto error_return;
  1842. shndx_buf = _bfd_malloc_and_read (abfd, amt, amt);
  1843. if (shndx_buf == NULL)
  1844. goto error_return;
  1845. shndx_hdr->contents = shndx_buf;
  1846. }
  1847. /* Get a copy of the native relocations. */
  1848. internal_relocs = (_bfd_elf_link_read_relocs
  1849. (abfd, sec, NULL, (Elf_Internal_Rela *) NULL,
  1850. link_info->keep_memory));
  1851. if (internal_relocs == NULL)
  1852. goto error_return;
  1853. if (! link_info->keep_memory)
  1854. free_relocs = internal_relocs;
  1855. /* The RL_ relocs must be just before the operand relocs they go
  1856. with, so we must sort them to guarantee this. We use bubblesort
  1857. instead of qsort so we can guarantee that relocs with the same
  1858. address remain in the same relative order. */
  1859. reloc_bubblesort (internal_relocs, sec->reloc_count);
  1860. /* Walk through them looking for relaxing opportunities. */
  1861. irelend = internal_relocs + sec->reloc_count;
  1862. /* This will either be NULL or a pointer to the next alignment
  1863. relocation. */
  1864. next_alignment = internal_relocs;
  1865. /* We calculate worst case shrinkage caused by alignment directives.
  1866. No fool-proof, but better than either ignoring the problem or
  1867. doing heavy duty analysis of all the alignment markers in all
  1868. input sections. */
  1869. section_alignment_glue = 0;
  1870. for (irel = internal_relocs; irel < irelend; irel++)
  1871. if (ELF32_R_TYPE (irel->r_info) == R_RL78_RH_RELAX
  1872. && irel->r_addend & RL78_RELAXA_ALIGN)
  1873. {
  1874. int this_glue = 1 << (irel->r_addend & RL78_RELAXA_ANUM);
  1875. if (section_alignment_glue < this_glue)
  1876. section_alignment_glue = this_glue;
  1877. }
  1878. /* Worst case is all 0..N alignments, in order, causing 2*N-1 byte
  1879. shrinkage. */
  1880. section_alignment_glue *= 2;
  1881. for (irel = internal_relocs; irel < irelend; irel++)
  1882. {
  1883. unsigned char *insn;
  1884. int nrelocs;
  1885. /* The insns we care about are all marked with one of these. */
  1886. if (ELF32_R_TYPE (irel->r_info) != R_RL78_RH_RELAX)
  1887. continue;
  1888. if (irel->r_addend & RL78_RELAXA_ALIGN
  1889. || next_alignment == internal_relocs)
  1890. {
  1891. /* When we delete bytes, we need to maintain all the alignments
  1892. indicated. In addition, we need to be careful about relaxing
  1893. jumps across alignment boundaries - these displacements
  1894. *grow* when we delete bytes. For now, don't shrink
  1895. displacements across an alignment boundary, just in case.
  1896. Note that this only affects relocations to the same
  1897. section. */
  1898. next_alignment += 2;
  1899. while (next_alignment < irelend
  1900. && (ELF32_R_TYPE (next_alignment->r_info) != R_RL78_RH_RELAX
  1901. || !(next_alignment->r_addend & RL78_RELAXA_ELIGN)))
  1902. next_alignment ++;
  1903. if (next_alignment >= irelend || next_alignment->r_offset == 0)
  1904. next_alignment = NULL;
  1905. }
  1906. /* When we hit alignment markers, see if we've shrunk enough
  1907. before them to reduce the gap without violating the alignment
  1908. requirements. */
  1909. if (irel->r_addend & RL78_RELAXA_ALIGN)
  1910. {
  1911. /* At this point, the next relocation *should* be the ELIGN
  1912. end marker. */
  1913. Elf_Internal_Rela *erel = irel + 1;
  1914. unsigned int alignment, nbytes;
  1915. if (ELF32_R_TYPE (erel->r_info) != R_RL78_RH_RELAX)
  1916. continue;
  1917. if (!(erel->r_addend & RL78_RELAXA_ELIGN))
  1918. continue;
  1919. alignment = 1 << (irel->r_addend & RL78_RELAXA_ANUM);
  1920. if (erel->r_offset - irel->r_offset < alignment)
  1921. continue;
  1922. nbytes = erel->r_offset - irel->r_offset;
  1923. nbytes /= alignment;
  1924. nbytes *= alignment;
  1925. elf32_rl78_relax_delete_bytes (abfd, sec, erel->r_offset - nbytes, nbytes,
  1926. next_alignment, erel->r_offset == sec->size);
  1927. *again = true;
  1928. continue;
  1929. }
  1930. if (irel->r_addend & RL78_RELAXA_ELIGN)
  1931. continue;
  1932. insn = contents + irel->r_offset;
  1933. nrelocs = irel->r_addend & RL78_RELAXA_RNUM;
  1934. /* At this point, we have an insn that is a candidate for linker
  1935. relaxation. There are NRELOCS relocs following that may be
  1936. relaxed, although each reloc may be made of more than one
  1937. reloc entry (such as gp-rel symbols). */
  1938. /* Get the value of the symbol referred to by the reloc. Just
  1939. in case this is the last reloc in the list, use the RL's
  1940. addend to choose between this reloc (no addend) or the next
  1941. (yes addend, which means at least one following reloc). */
  1942. /* srel points to the "current" reloction for this insn -
  1943. actually the last reloc for a given operand, which is the one
  1944. we need to update. We check the relaxations in the same
  1945. order that the relocations happen, so we'll just push it
  1946. along as we go. */
  1947. srel = irel;
  1948. pc = sec->output_section->vma + sec->output_offset
  1949. + srel->r_offset;
  1950. #define GET_RELOC \
  1951. BFD_ASSERT (nrelocs > 0); \
  1952. symval = OFFSET_FOR_RELOC (srel, &srel, &scale); \
  1953. pcrel = symval - pc + srel->r_addend; \
  1954. nrelocs --;
  1955. #define SNIPNR(offset, nbytes) \
  1956. elf32_rl78_relax_delete_bytes (abfd, sec, (insn - contents) + offset, nbytes, next_alignment, 0);
  1957. #define SNIP(offset, nbytes, newtype) \
  1958. SNIPNR (offset, nbytes); \
  1959. srel->r_info = ELF32_R_INFO (ELF32_R_SYM (srel->r_info), newtype)
  1960. /* The order of these bit tests must match the order that the
  1961. relocs appear in. Since we sorted those by offset, we can
  1962. predict them. */
  1963. /*----------------------------------------------------------------------*/
  1964. /* EF ad BR $rel8 pcrel
  1965. ED al ah BR !abs16 abs
  1966. EE al ah BR $!rel16 pcrel
  1967. EC al ah as BR !!abs20 abs
  1968. FD al ah CALL !abs16 abs
  1969. FE al ah CALL $!rel16 pcrel
  1970. FC al ah as CALL !!abs20 abs
  1971. DC ad BC $rel8
  1972. DE ad BNC $rel8
  1973. DD ad BZ $rel8
  1974. DF ad BNZ $rel8
  1975. 61 C3 ad BH $rel8
  1976. 61 D3 ad BNH $rel8
  1977. 61 C8 EF ad SKC ; BR $rel8
  1978. 61 D8 EF ad SKNC ; BR $rel8
  1979. 61 E8 EF ad SKZ ; BR $rel8
  1980. 61 F8 EF ad SKNZ ; BR $rel8
  1981. 61 E3 EF ad SKH ; BR $rel8
  1982. 61 F3 EF ad SKNH ; BR $rel8
  1983. */
  1984. if ((irel->r_addend & RL78_RELAXA_MASK) == RL78_RELAXA_BRA)
  1985. {
  1986. /* SKIP opcodes that skip non-branches will have a relax tag
  1987. but no corresponding symbol to relax against; we just
  1988. skip those. */
  1989. if (irel->r_addend & RL78_RELAXA_RNUM)
  1990. {
  1991. GET_RELOC;
  1992. }
  1993. switch (insn[0])
  1994. {
  1995. case 0xdc: /* BC */
  1996. case 0xdd: /* BZ */
  1997. case 0xde: /* BNC */
  1998. case 0xdf: /* BNZ */
  1999. if (insn[1] == 0x03 && insn[2] == 0xee /* BR */
  2000. && (srel->r_offset - irel->r_offset) > 1) /* a B<c> without its own reloc */
  2001. {
  2002. /* This is a "long" conditional as generated by gas:
  2003. DC 03 EE ad.dr */
  2004. if (pcrel < 127
  2005. && pcrel > -127)
  2006. {
  2007. insn[0] ^= 0x02; /* invert conditional */
  2008. SNIPNR (4, 1);
  2009. SNIP (1, 2, R_RL78_DIR8S_PCREL);
  2010. insn[1] = pcrel;
  2011. *again = true;
  2012. }
  2013. }
  2014. break;
  2015. case 0xec: /* BR !!abs20 */
  2016. if (pcrel < 127
  2017. && pcrel > -127)
  2018. {
  2019. insn[0] = 0xef;
  2020. insn[1] = pcrel;
  2021. SNIP (2, 2, R_RL78_DIR8S_PCREL);
  2022. *again = true;
  2023. }
  2024. else if (symval < 65536)
  2025. {
  2026. insn[0] = 0xed;
  2027. insn[1] = symval & 0xff;
  2028. insn[2] = symval >> 8;
  2029. SNIP (2, 1, R_RL78_DIR16U);
  2030. *again = true;
  2031. }
  2032. else if (pcrel < 32767
  2033. && pcrel > -32767)
  2034. {
  2035. insn[0] = 0xee;
  2036. insn[1] = pcrel & 0xff;
  2037. insn[2] = pcrel >> 8;
  2038. SNIP (2, 1, R_RL78_DIR16S_PCREL);
  2039. *again = true;
  2040. }
  2041. break;
  2042. case 0xee: /* BR $!pcrel16 */
  2043. case 0xed: /* BR $!abs16 */
  2044. if (pcrel < 127
  2045. && pcrel > -127)
  2046. {
  2047. insn[0] = 0xef;
  2048. insn[1] = pcrel;
  2049. SNIP (2, 1, R_RL78_DIR8S_PCREL);
  2050. *again = true;
  2051. }
  2052. break;
  2053. case 0xfc: /* CALL !!abs20 */
  2054. if (symval < 65536)
  2055. {
  2056. insn[0] = 0xfd;
  2057. insn[1] = symval & 0xff;
  2058. insn[2] = symval >> 8;
  2059. SNIP (2, 1, R_RL78_DIR16U);
  2060. *again = true;
  2061. }
  2062. else if (pcrel < 32767
  2063. && pcrel > -32767)
  2064. {
  2065. insn[0] = 0xfe;
  2066. insn[1] = pcrel & 0xff;
  2067. insn[2] = pcrel >> 8;
  2068. SNIP (2, 1, R_RL78_DIR16S_PCREL);
  2069. *again = true;
  2070. }
  2071. break;
  2072. case 0x61: /* PREFIX */
  2073. /* For SKIP/BR, we change the BR opcode and delete the
  2074. SKIP. That way, we don't have to find and change the
  2075. relocation for the BR. */
  2076. /* Note that, for the case where we're skipping some
  2077. other insn, we have no "other" reloc but that's safe
  2078. here anyway. */
  2079. switch (insn[1])
  2080. {
  2081. case 0xd3: /* BNH */
  2082. case 0xc3: /* BH */
  2083. if (insn[2] == 0x03 && insn[3] == 0xee
  2084. && (srel->r_offset - irel->r_offset) > 2) /* a B<c> without its own reloc */
  2085. {
  2086. /* Another long branch by gas:
  2087. 61 D3 03 EE ad.dr */
  2088. if (pcrel < 127
  2089. && pcrel > -127)
  2090. {
  2091. insn[1] ^= 0x10; /* invert conditional */
  2092. SNIPNR (5, 1);
  2093. SNIP (2, 2, R_RL78_DIR8S_PCREL);
  2094. insn[2] = pcrel;
  2095. *again = true;
  2096. }
  2097. }
  2098. break;
  2099. case 0xc8: /* SKC */
  2100. if (insn[2] == 0xef)
  2101. {
  2102. insn[2] = 0xde; /* BNC */
  2103. SNIPNR (0, 2);
  2104. }
  2105. break;
  2106. case 0xd8: /* SKNC */
  2107. if (insn[2] == 0xef)
  2108. {
  2109. insn[2] = 0xdc; /* BC */
  2110. SNIPNR (0, 2);
  2111. }
  2112. break;
  2113. case 0xe8: /* SKZ */
  2114. if (insn[2] == 0xef)
  2115. {
  2116. insn[2] = 0xdf; /* BNZ */
  2117. SNIPNR (0, 2);
  2118. }
  2119. break;
  2120. case 0xf8: /* SKNZ */
  2121. if (insn[2] == 0xef)
  2122. {
  2123. insn[2] = 0xdd; /* BZ */
  2124. SNIPNR (0, 2);
  2125. }
  2126. break;
  2127. case 0xe3: /* SKH */
  2128. if (insn[2] == 0xef)
  2129. {
  2130. insn[2] = 0xd3; /* BNH */
  2131. SNIPNR (1, 1); /* we reuse the 0x61 prefix from the SKH */
  2132. }
  2133. break;
  2134. case 0xf3: /* SKNH */
  2135. if (insn[2] == 0xef)
  2136. {
  2137. insn[2] = 0xc3; /* BH */
  2138. SNIPNR (1, 1); /* we reuse the 0x61 prefix from the SKH */
  2139. }
  2140. break;
  2141. }
  2142. break;
  2143. }
  2144. }
  2145. if ((irel->r_addend & RL78_RELAXA_MASK) == RL78_RELAXA_ADDR16
  2146. && nrelocs > 0)
  2147. {
  2148. /*----------------------------------------------------------------------*/
  2149. /* Some insns have both a 16-bit address operand and an 8-bit
  2150. variant if the address is within a special range:
  2151. Address 16-bit operand SADDR range SFR range
  2152. FFF00-FFFFF 0xff00-0xffff 0x00-0xff
  2153. FFE20-FFF1F 0xfe20-0xff1f 0x00-0xff
  2154. The RELAX_ADDR16[] array has the insn encodings for the
  2155. 16-bit operand version, as well as the SFR and SADDR
  2156. variants. We only need to replace the encodings and
  2157. adjust the operand.
  2158. Note: we intentionally do not attempt to decode and skip
  2159. any ES: prefix, as adding ES: means the addr16 (likely)
  2160. no longer points to saddr/sfr space.
  2161. */
  2162. int is_sfr;
  2163. int is_saddr;
  2164. int idx;
  2165. int poff;
  2166. GET_RELOC;
  2167. if (0xffe20 <= symval && symval <= 0xfffff)
  2168. {
  2169. is_saddr = (0xffe20 <= symval && symval <= 0xfff1f);
  2170. is_sfr = (0xfff00 <= symval && symval <= 0xfffff);
  2171. for (idx = 0; relax_addr16[idx].insn != -1; idx ++)
  2172. {
  2173. if (relax_addr16[idx].prefix != -1
  2174. && insn[0] == relax_addr16[idx].prefix
  2175. && insn[1] == relax_addr16[idx].insn)
  2176. {
  2177. poff = 1;
  2178. }
  2179. else if (relax_addr16[idx].prefix == -1
  2180. && insn[0] == relax_addr16[idx].insn)
  2181. {
  2182. poff = 0;
  2183. }
  2184. else
  2185. continue;
  2186. /* We have a matched insn, and poff is 0 or 1 depending
  2187. on the base pattern size. */
  2188. if (is_sfr && relax_addr16[idx].insn_for_sfr != -1)
  2189. {
  2190. insn[poff] = relax_addr16[idx].insn_for_sfr;
  2191. SNIP (poff+2, 1, R_RL78_RH_SFR);
  2192. }
  2193. else if (is_saddr && relax_addr16[idx].insn_for_saddr != -1)
  2194. {
  2195. insn[poff] = relax_addr16[idx].insn_for_saddr;
  2196. SNIP (poff+2, 1, R_RL78_RH_SADDR);
  2197. }
  2198. }
  2199. }
  2200. }
  2201. /*----------------------------------------------------------------------*/
  2202. }
  2203. return true;
  2204. error_return:
  2205. free (free_relocs);
  2206. free (free_contents);
  2207. if (shndx_buf != NULL)
  2208. {
  2209. shndx_hdr->contents = NULL;
  2210. free (shndx_buf);
  2211. }
  2212. free (free_intsyms);
  2213. return true;
  2214. }
  2215. #define ELF_ARCH bfd_arch_rl78
  2216. #define ELF_MACHINE_CODE EM_RL78
  2217. #define ELF_MAXPAGESIZE 0x1000
  2218. #define TARGET_LITTLE_SYM rl78_elf32_vec
  2219. #define TARGET_LITTLE_NAME "elf32-rl78"
  2220. #define elf_info_to_howto_rel NULL
  2221. #define elf_info_to_howto rl78_info_to_howto_rela
  2222. #define elf_backend_object_p rl78_elf_object_p
  2223. #define elf_backend_relocate_section rl78_elf_relocate_section
  2224. #define elf_symbol_leading_char ('_')
  2225. #define elf_backend_can_gc_sections 1
  2226. #define bfd_elf32_bfd_reloc_type_lookup rl78_reloc_type_lookup
  2227. #define bfd_elf32_bfd_reloc_name_lookup rl78_reloc_name_lookup
  2228. #define bfd_elf32_bfd_set_private_flags rl78_elf_set_private_flags
  2229. #define bfd_elf32_bfd_merge_private_bfd_data rl78_elf_merge_private_bfd_data
  2230. #define bfd_elf32_bfd_print_private_bfd_data rl78_elf_print_private_bfd_data
  2231. #define bfd_elf32_bfd_relax_section rl78_elf_relax_section
  2232. #define elf_backend_check_relocs rl78_elf_check_relocs
  2233. #define elf_backend_always_size_sections \
  2234. rl78_elf_always_size_sections
  2235. #define elf_backend_finish_dynamic_sections \
  2236. rl78_elf_finish_dynamic_sections
  2237. #include "elf32-target.h"