coff-sh.c 93 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207
  1. /* BFD back-end for Renesas Super-H COFF binaries.
  2. Copyright (C) 1993-2015 Free Software Foundation, Inc.
  3. Contributed by Cygnus Support.
  4. Written by Steve Chamberlain, <sac@cygnus.com>.
  5. Relaxing code written by Ian Lance Taylor, <ian@cygnus.com>.
  6. This file is part of BFD, the Binary File Descriptor library.
  7. This program is free software; you can redistribute it and/or modify
  8. it under the terms of the GNU General Public License as published by
  9. the Free Software Foundation; either version 3 of the License, or
  10. (at your option) any later version.
  11. This program is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. GNU General Public License for more details.
  15. You should have received a copy of the GNU General Public License
  16. along with this program; if not, write to the Free Software
  17. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  18. MA 02110-1301, USA. */
  19. #include "sysdep.h"
  20. #include "bfd.h"
  21. #include "libiberty.h"
  22. #include "libbfd.h"
  23. #include "bfdlink.h"
  24. #include "coff/sh.h"
  25. #include "coff/internal.h"
  26. #undef bfd_pe_print_pdata
  27. #ifdef COFF_WITH_PE
  28. #include "coff/pe.h"
  29. #ifndef COFF_IMAGE_WITH_PE
  30. static bfd_boolean sh_align_load_span
  31. (bfd *, asection *, bfd_byte *,
  32. bfd_boolean (*) (bfd *, asection *, void *, bfd_byte *, bfd_vma),
  33. void *, bfd_vma **, bfd_vma *, bfd_vma, bfd_vma, bfd_boolean *);
  34. #define _bfd_sh_align_load_span sh_align_load_span
  35. #endif
  36. #define bfd_pe_print_pdata _bfd_pe_print_ce_compressed_pdata
  37. #else
  38. #define bfd_pe_print_pdata NULL
  39. #endif /* COFF_WITH_PE. */
  40. #include "libcoff.h"
  41. /* Internal functions. */
  42. #ifdef COFF_WITH_PE
  43. /* Can't build import tables with 2**4 alignment. */
  44. #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 2
  45. #else
  46. /* Default section alignment to 2**4. */
  47. #define COFF_DEFAULT_SECTION_ALIGNMENT_POWER 4
  48. #endif
  49. #ifdef COFF_IMAGE_WITH_PE
  50. /* Align PE executables. */
  51. #define COFF_PAGE_SIZE 0x1000
  52. #endif
  53. /* Generate long file names. */
  54. #define COFF_LONG_FILENAMES
  55. #ifdef COFF_WITH_PE
  56. /* Return TRUE if this relocation should
  57. appear in the output .reloc section. */
  58. static bfd_boolean
  59. in_reloc_p (bfd * abfd ATTRIBUTE_UNUSED,
  60. reloc_howto_type * howto)
  61. {
  62. return ! howto->pc_relative && howto->type != R_SH_IMAGEBASE;
  63. }
  64. #endif
  65. static bfd_reloc_status_type
  66. sh_reloc (bfd *, arelent *, asymbol *, void *, asection *, bfd *, char **);
  67. static bfd_boolean
  68. sh_relocate_section (bfd *, struct bfd_link_info *, bfd *, asection *,
  69. bfd_byte *, struct internal_reloc *,
  70. struct internal_syment *, asection **);
  71. static bfd_boolean
  72. sh_align_loads (bfd *, asection *, struct internal_reloc *,
  73. bfd_byte *, bfd_boolean *);
  74. /* The supported relocations. There are a lot of relocations defined
  75. in coff/internal.h which we do not expect to ever see. */
  76. static reloc_howto_type sh_coff_howtos[] =
  77. {
  78. EMPTY_HOWTO (0),
  79. EMPTY_HOWTO (1),
  80. #ifdef COFF_WITH_PE
  81. /* Windows CE */
  82. HOWTO (R_SH_IMM32CE, /* type */
  83. 0, /* rightshift */
  84. 2, /* size (0 = byte, 1 = short, 2 = long) */
  85. 32, /* bitsize */
  86. FALSE, /* pc_relative */
  87. 0, /* bitpos */
  88. complain_overflow_bitfield, /* complain_on_overflow */
  89. sh_reloc, /* special_function */
  90. "r_imm32ce", /* name */
  91. TRUE, /* partial_inplace */
  92. 0xffffffff, /* src_mask */
  93. 0xffffffff, /* dst_mask */
  94. FALSE), /* pcrel_offset */
  95. #else
  96. EMPTY_HOWTO (2),
  97. #endif
  98. EMPTY_HOWTO (3), /* R_SH_PCREL8 */
  99. EMPTY_HOWTO (4), /* R_SH_PCREL16 */
  100. EMPTY_HOWTO (5), /* R_SH_HIGH8 */
  101. EMPTY_HOWTO (6), /* R_SH_IMM24 */
  102. EMPTY_HOWTO (7), /* R_SH_LOW16 */
  103. EMPTY_HOWTO (8),
  104. EMPTY_HOWTO (9), /* R_SH_PCDISP8BY4 */
  105. HOWTO (R_SH_PCDISP8BY2, /* type */
  106. 1, /* rightshift */
  107. 1, /* size (0 = byte, 1 = short, 2 = long) */
  108. 8, /* bitsize */
  109. TRUE, /* pc_relative */
  110. 0, /* bitpos */
  111. complain_overflow_signed, /* complain_on_overflow */
  112. sh_reloc, /* special_function */
  113. "r_pcdisp8by2", /* name */
  114. TRUE, /* partial_inplace */
  115. 0xff, /* src_mask */
  116. 0xff, /* dst_mask */
  117. TRUE), /* pcrel_offset */
  118. EMPTY_HOWTO (11), /* R_SH_PCDISP8 */
  119. HOWTO (R_SH_PCDISP, /* type */
  120. 1, /* rightshift */
  121. 1, /* size (0 = byte, 1 = short, 2 = long) */
  122. 12, /* bitsize */
  123. TRUE, /* pc_relative */
  124. 0, /* bitpos */
  125. complain_overflow_signed, /* complain_on_overflow */
  126. sh_reloc, /* special_function */
  127. "r_pcdisp12by2", /* name */
  128. TRUE, /* partial_inplace */
  129. 0xfff, /* src_mask */
  130. 0xfff, /* dst_mask */
  131. TRUE), /* pcrel_offset */
  132. EMPTY_HOWTO (13),
  133. HOWTO (R_SH_IMM32, /* type */
  134. 0, /* rightshift */
  135. 2, /* size (0 = byte, 1 = short, 2 = long) */
  136. 32, /* bitsize */
  137. FALSE, /* pc_relative */
  138. 0, /* bitpos */
  139. complain_overflow_bitfield, /* complain_on_overflow */
  140. sh_reloc, /* special_function */
  141. "r_imm32", /* name */
  142. TRUE, /* partial_inplace */
  143. 0xffffffff, /* src_mask */
  144. 0xffffffff, /* dst_mask */
  145. FALSE), /* pcrel_offset */
  146. EMPTY_HOWTO (15),
  147. #ifdef COFF_WITH_PE
  148. HOWTO (R_SH_IMAGEBASE, /* type */
  149. 0, /* rightshift */
  150. 2, /* size (0 = byte, 1 = short, 2 = long) */
  151. 32, /* bitsize */
  152. FALSE, /* pc_relative */
  153. 0, /* bitpos */
  154. complain_overflow_bitfield, /* complain_on_overflow */
  155. sh_reloc, /* special_function */
  156. "rva32", /* name */
  157. TRUE, /* partial_inplace */
  158. 0xffffffff, /* src_mask */
  159. 0xffffffff, /* dst_mask */
  160. FALSE), /* pcrel_offset */
  161. #else
  162. EMPTY_HOWTO (16), /* R_SH_IMM8 */
  163. #endif
  164. EMPTY_HOWTO (17), /* R_SH_IMM8BY2 */
  165. EMPTY_HOWTO (18), /* R_SH_IMM8BY4 */
  166. EMPTY_HOWTO (19), /* R_SH_IMM4 */
  167. EMPTY_HOWTO (20), /* R_SH_IMM4BY2 */
  168. EMPTY_HOWTO (21), /* R_SH_IMM4BY4 */
  169. HOWTO (R_SH_PCRELIMM8BY2, /* type */
  170. 1, /* rightshift */
  171. 1, /* size (0 = byte, 1 = short, 2 = long) */
  172. 8, /* bitsize */
  173. TRUE, /* pc_relative */
  174. 0, /* bitpos */
  175. complain_overflow_unsigned, /* complain_on_overflow */
  176. sh_reloc, /* special_function */
  177. "r_pcrelimm8by2", /* name */
  178. TRUE, /* partial_inplace */
  179. 0xff, /* src_mask */
  180. 0xff, /* dst_mask */
  181. TRUE), /* pcrel_offset */
  182. HOWTO (R_SH_PCRELIMM8BY4, /* type */
  183. 2, /* rightshift */
  184. 1, /* size (0 = byte, 1 = short, 2 = long) */
  185. 8, /* bitsize */
  186. TRUE, /* pc_relative */
  187. 0, /* bitpos */
  188. complain_overflow_unsigned, /* complain_on_overflow */
  189. sh_reloc, /* special_function */
  190. "r_pcrelimm8by4", /* name */
  191. TRUE, /* partial_inplace */
  192. 0xff, /* src_mask */
  193. 0xff, /* dst_mask */
  194. TRUE), /* pcrel_offset */
  195. HOWTO (R_SH_IMM16, /* type */
  196. 0, /* rightshift */
  197. 1, /* size (0 = byte, 1 = short, 2 = long) */
  198. 16, /* bitsize */
  199. FALSE, /* pc_relative */
  200. 0, /* bitpos */
  201. complain_overflow_bitfield, /* complain_on_overflow */
  202. sh_reloc, /* special_function */
  203. "r_imm16", /* name */
  204. TRUE, /* partial_inplace */
  205. 0xffff, /* src_mask */
  206. 0xffff, /* dst_mask */
  207. FALSE), /* pcrel_offset */
  208. HOWTO (R_SH_SWITCH16, /* type */
  209. 0, /* rightshift */
  210. 1, /* size (0 = byte, 1 = short, 2 = long) */
  211. 16, /* bitsize */
  212. FALSE, /* pc_relative */
  213. 0, /* bitpos */
  214. complain_overflow_bitfield, /* complain_on_overflow */
  215. sh_reloc, /* special_function */
  216. "r_switch16", /* name */
  217. TRUE, /* partial_inplace */
  218. 0xffff, /* src_mask */
  219. 0xffff, /* dst_mask */
  220. FALSE), /* pcrel_offset */
  221. HOWTO (R_SH_SWITCH32, /* type */
  222. 0, /* rightshift */
  223. 2, /* size (0 = byte, 1 = short, 2 = long) */
  224. 32, /* bitsize */
  225. FALSE, /* pc_relative */
  226. 0, /* bitpos */
  227. complain_overflow_bitfield, /* complain_on_overflow */
  228. sh_reloc, /* special_function */
  229. "r_switch32", /* name */
  230. TRUE, /* partial_inplace */
  231. 0xffffffff, /* src_mask */
  232. 0xffffffff, /* dst_mask */
  233. FALSE), /* pcrel_offset */
  234. HOWTO (R_SH_USES, /* type */
  235. 0, /* rightshift */
  236. 1, /* size (0 = byte, 1 = short, 2 = long) */
  237. 16, /* bitsize */
  238. FALSE, /* pc_relative */
  239. 0, /* bitpos */
  240. complain_overflow_bitfield, /* complain_on_overflow */
  241. sh_reloc, /* special_function */
  242. "r_uses", /* name */
  243. TRUE, /* partial_inplace */
  244. 0xffff, /* src_mask */
  245. 0xffff, /* dst_mask */
  246. FALSE), /* pcrel_offset */
  247. HOWTO (R_SH_COUNT, /* type */
  248. 0, /* rightshift */
  249. 2, /* size (0 = byte, 1 = short, 2 = long) */
  250. 32, /* bitsize */
  251. FALSE, /* pc_relative */
  252. 0, /* bitpos */
  253. complain_overflow_bitfield, /* complain_on_overflow */
  254. sh_reloc, /* special_function */
  255. "r_count", /* name */
  256. TRUE, /* partial_inplace */
  257. 0xffffffff, /* src_mask */
  258. 0xffffffff, /* dst_mask */
  259. FALSE), /* pcrel_offset */
  260. HOWTO (R_SH_ALIGN, /* type */
  261. 0, /* rightshift */
  262. 2, /* size (0 = byte, 1 = short, 2 = long) */
  263. 32, /* bitsize */
  264. FALSE, /* pc_relative */
  265. 0, /* bitpos */
  266. complain_overflow_bitfield, /* complain_on_overflow */
  267. sh_reloc, /* special_function */
  268. "r_align", /* name */
  269. TRUE, /* partial_inplace */
  270. 0xffffffff, /* src_mask */
  271. 0xffffffff, /* dst_mask */
  272. FALSE), /* pcrel_offset */
  273. HOWTO (R_SH_CODE, /* type */
  274. 0, /* rightshift */
  275. 2, /* size (0 = byte, 1 = short, 2 = long) */
  276. 32, /* bitsize */
  277. FALSE, /* pc_relative */
  278. 0, /* bitpos */
  279. complain_overflow_bitfield, /* complain_on_overflow */
  280. sh_reloc, /* special_function */
  281. "r_code", /* name */
  282. TRUE, /* partial_inplace */
  283. 0xffffffff, /* src_mask */
  284. 0xffffffff, /* dst_mask */
  285. FALSE), /* pcrel_offset */
  286. HOWTO (R_SH_DATA, /* type */
  287. 0, /* rightshift */
  288. 2, /* size (0 = byte, 1 = short, 2 = long) */
  289. 32, /* bitsize */
  290. FALSE, /* pc_relative */
  291. 0, /* bitpos */
  292. complain_overflow_bitfield, /* complain_on_overflow */
  293. sh_reloc, /* special_function */
  294. "r_data", /* name */
  295. TRUE, /* partial_inplace */
  296. 0xffffffff, /* src_mask */
  297. 0xffffffff, /* dst_mask */
  298. FALSE), /* pcrel_offset */
  299. HOWTO (R_SH_LABEL, /* type */
  300. 0, /* rightshift */
  301. 2, /* size (0 = byte, 1 = short, 2 = long) */
  302. 32, /* bitsize */
  303. FALSE, /* pc_relative */
  304. 0, /* bitpos */
  305. complain_overflow_bitfield, /* complain_on_overflow */
  306. sh_reloc, /* special_function */
  307. "r_label", /* name */
  308. TRUE, /* partial_inplace */
  309. 0xffffffff, /* src_mask */
  310. 0xffffffff, /* dst_mask */
  311. FALSE), /* pcrel_offset */
  312. HOWTO (R_SH_SWITCH8, /* type */
  313. 0, /* rightshift */
  314. 0, /* size (0 = byte, 1 = short, 2 = long) */
  315. 8, /* bitsize */
  316. FALSE, /* pc_relative */
  317. 0, /* bitpos */
  318. complain_overflow_bitfield, /* complain_on_overflow */
  319. sh_reloc, /* special_function */
  320. "r_switch8", /* name */
  321. TRUE, /* partial_inplace */
  322. 0xff, /* src_mask */
  323. 0xff, /* dst_mask */
  324. FALSE) /* pcrel_offset */
  325. };
  326. #define SH_COFF_HOWTO_COUNT (sizeof sh_coff_howtos / sizeof sh_coff_howtos[0])
  327. /* Check for a bad magic number. */
  328. #define BADMAG(x) SHBADMAG(x)
  329. /* Customize coffcode.h (this is not currently used). */
  330. #define SH 1
  331. /* FIXME: This should not be set here. */
  332. #define __A_MAGIC_SET__
  333. #ifndef COFF_WITH_PE
  334. /* Swap the r_offset field in and out. */
  335. #define SWAP_IN_RELOC_OFFSET H_GET_32
  336. #define SWAP_OUT_RELOC_OFFSET H_PUT_32
  337. /* Swap out extra information in the reloc structure. */
  338. #define SWAP_OUT_RELOC_EXTRA(abfd, src, dst) \
  339. do \
  340. { \
  341. dst->r_stuff[0] = 'S'; \
  342. dst->r_stuff[1] = 'C'; \
  343. } \
  344. while (0)
  345. #endif
  346. /* Get the value of a symbol, when performing a relocation. */
  347. static long
  348. get_symbol_value (asymbol *symbol)
  349. {
  350. bfd_vma relocation;
  351. if (bfd_is_com_section (symbol->section))
  352. relocation = 0;
  353. else
  354. relocation = (symbol->value +
  355. symbol->section->output_section->vma +
  356. symbol->section->output_offset);
  357. return relocation;
  358. }
  359. #ifdef COFF_WITH_PE
  360. /* Convert an rtype to howto for the COFF backend linker.
  361. Copied from coff-i386. */
  362. #define coff_rtype_to_howto coff_sh_rtype_to_howto
  363. static reloc_howto_type *
  364. coff_sh_rtype_to_howto (bfd * abfd ATTRIBUTE_UNUSED,
  365. asection * sec,
  366. struct internal_reloc * rel,
  367. struct coff_link_hash_entry * h,
  368. struct internal_syment * sym,
  369. bfd_vma * addendp)
  370. {
  371. reloc_howto_type * howto;
  372. howto = sh_coff_howtos + rel->r_type;
  373. *addendp = 0;
  374. if (howto->pc_relative)
  375. *addendp += sec->vma;
  376. if (sym != NULL && sym->n_scnum == 0 && sym->n_value != 0)
  377. {
  378. /* This is a common symbol. The section contents include the
  379. size (sym->n_value) as an addend. The relocate_section
  380. function will be adding in the final value of the symbol. We
  381. need to subtract out the current size in order to get the
  382. correct result. */
  383. BFD_ASSERT (h != NULL);
  384. }
  385. if (howto->pc_relative)
  386. {
  387. *addendp -= 4;
  388. /* If the symbol is defined, then the generic code is going to
  389. add back the symbol value in order to cancel out an
  390. adjustment it made to the addend. However, we set the addend
  391. to 0 at the start of this function. We need to adjust here,
  392. to avoid the adjustment the generic code will make. FIXME:
  393. This is getting a bit hackish. */
  394. if (sym != NULL && sym->n_scnum != 0)
  395. *addendp -= sym->n_value;
  396. }
  397. if (rel->r_type == R_SH_IMAGEBASE)
  398. *addendp -= pe_data (sec->output_section->owner)->pe_opthdr.ImageBase;
  399. return howto;
  400. }
  401. #endif /* COFF_WITH_PE */
  402. /* This structure is used to map BFD reloc codes to SH PE relocs. */
  403. struct shcoff_reloc_map
  404. {
  405. bfd_reloc_code_real_type bfd_reloc_val;
  406. unsigned char shcoff_reloc_val;
  407. };
  408. #ifdef COFF_WITH_PE
  409. /* An array mapping BFD reloc codes to SH PE relocs. */
  410. static const struct shcoff_reloc_map sh_reloc_map[] =
  411. {
  412. { BFD_RELOC_32, R_SH_IMM32CE },
  413. { BFD_RELOC_RVA, R_SH_IMAGEBASE },
  414. { BFD_RELOC_CTOR, R_SH_IMM32CE },
  415. };
  416. #else
  417. /* An array mapping BFD reloc codes to SH PE relocs. */
  418. static const struct shcoff_reloc_map sh_reloc_map[] =
  419. {
  420. { BFD_RELOC_32, R_SH_IMM32 },
  421. { BFD_RELOC_CTOR, R_SH_IMM32 },
  422. };
  423. #endif
  424. /* Given a BFD reloc code, return the howto structure for the
  425. corresponding SH PE reloc. */
  426. #define coff_bfd_reloc_type_lookup sh_coff_reloc_type_lookup
  427. #define coff_bfd_reloc_name_lookup sh_coff_reloc_name_lookup
  428. static reloc_howto_type *
  429. sh_coff_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
  430. bfd_reloc_code_real_type code)
  431. {
  432. unsigned int i;
  433. for (i = ARRAY_SIZE (sh_reloc_map); i--;)
  434. if (sh_reloc_map[i].bfd_reloc_val == code)
  435. return &sh_coff_howtos[(int) sh_reloc_map[i].shcoff_reloc_val];
  436. (*_bfd_error_handler) (_("SH Error: unknown reloc type %d"), code);
  437. return NULL;
  438. }
  439. static reloc_howto_type *
  440. sh_coff_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  441. const char *r_name)
  442. {
  443. unsigned int i;
  444. for (i = 0; i < sizeof (sh_coff_howtos) / sizeof (sh_coff_howtos[0]); i++)
  445. if (sh_coff_howtos[i].name != NULL
  446. && strcasecmp (sh_coff_howtos[i].name, r_name) == 0)
  447. return &sh_coff_howtos[i];
  448. return NULL;
  449. }
  450. /* This macro is used in coffcode.h to get the howto corresponding to
  451. an internal reloc. */
  452. #define RTYPE2HOWTO(relent, internal) \
  453. ((relent)->howto = \
  454. ((internal)->r_type < SH_COFF_HOWTO_COUNT \
  455. ? &sh_coff_howtos[(internal)->r_type] \
  456. : (reloc_howto_type *) NULL))
  457. /* This is the same as the macro in coffcode.h, except that it copies
  458. r_offset into reloc_entry->addend for some relocs. */
  459. #define CALC_ADDEND(abfd, ptr, reloc, cache_ptr) \
  460. { \
  461. coff_symbol_type *coffsym = (coff_symbol_type *) NULL; \
  462. if (ptr && bfd_asymbol_bfd (ptr) != abfd) \
  463. coffsym = (obj_symbols (abfd) \
  464. + (cache_ptr->sym_ptr_ptr - symbols)); \
  465. else if (ptr) \
  466. coffsym = coff_symbol_from (ptr); \
  467. if (coffsym != (coff_symbol_type *) NULL \
  468. && coffsym->native->u.syment.n_scnum == 0) \
  469. cache_ptr->addend = 0; \
  470. else if (ptr && bfd_asymbol_bfd (ptr) == abfd \
  471. && ptr->section != (asection *) NULL) \
  472. cache_ptr->addend = - (ptr->section->vma + ptr->value); \
  473. else \
  474. cache_ptr->addend = 0; \
  475. if ((reloc).r_type == R_SH_SWITCH8 \
  476. || (reloc).r_type == R_SH_SWITCH16 \
  477. || (reloc).r_type == R_SH_SWITCH32 \
  478. || (reloc).r_type == R_SH_USES \
  479. || (reloc).r_type == R_SH_COUNT \
  480. || (reloc).r_type == R_SH_ALIGN) \
  481. cache_ptr->addend = (reloc).r_offset; \
  482. }
  483. /* This is the howto function for the SH relocations. */
  484. static bfd_reloc_status_type
  485. sh_reloc (bfd * abfd,
  486. arelent * reloc_entry,
  487. asymbol * symbol_in,
  488. void * data,
  489. asection * input_section,
  490. bfd * output_bfd,
  491. char ** error_message ATTRIBUTE_UNUSED)
  492. {
  493. unsigned long insn;
  494. bfd_vma sym_value;
  495. unsigned short r_type;
  496. bfd_vma addr = reloc_entry->address;
  497. bfd_byte *hit_data = addr + (bfd_byte *) data;
  498. r_type = reloc_entry->howto->type;
  499. if (output_bfd != NULL)
  500. {
  501. /* Partial linking--do nothing. */
  502. reloc_entry->address += input_section->output_offset;
  503. return bfd_reloc_ok;
  504. }
  505. /* Almost all relocs have to do with relaxing. If any work must be
  506. done for them, it has been done in sh_relax_section. */
  507. if (r_type != R_SH_IMM32
  508. #ifdef COFF_WITH_PE
  509. && r_type != R_SH_IMM32CE
  510. && r_type != R_SH_IMAGEBASE
  511. #endif
  512. && (r_type != R_SH_PCDISP
  513. || (symbol_in->flags & BSF_LOCAL) != 0))
  514. return bfd_reloc_ok;
  515. if (symbol_in != NULL
  516. && bfd_is_und_section (symbol_in->section))
  517. return bfd_reloc_undefined;
  518. sym_value = get_symbol_value (symbol_in);
  519. switch (r_type)
  520. {
  521. case R_SH_IMM32:
  522. #ifdef COFF_WITH_PE
  523. case R_SH_IMM32CE:
  524. #endif
  525. insn = bfd_get_32 (abfd, hit_data);
  526. insn += sym_value + reloc_entry->addend;
  527. bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
  528. break;
  529. #ifdef COFF_WITH_PE
  530. case R_SH_IMAGEBASE:
  531. insn = bfd_get_32 (abfd, hit_data);
  532. insn += sym_value + reloc_entry->addend;
  533. insn -= pe_data (input_section->output_section->owner)->pe_opthdr.ImageBase;
  534. bfd_put_32 (abfd, (bfd_vma) insn, hit_data);
  535. break;
  536. #endif
  537. case R_SH_PCDISP:
  538. insn = bfd_get_16 (abfd, hit_data);
  539. sym_value += reloc_entry->addend;
  540. sym_value -= (input_section->output_section->vma
  541. + input_section->output_offset
  542. + addr
  543. + 4);
  544. sym_value += (insn & 0xfff) << 1;
  545. if (insn & 0x800)
  546. sym_value -= 0x1000;
  547. insn = (insn & 0xf000) | (sym_value & 0xfff);
  548. bfd_put_16 (abfd, (bfd_vma) insn, hit_data);
  549. if (sym_value < (bfd_vma) -0x1000 || sym_value >= 0x1000)
  550. return bfd_reloc_overflow;
  551. break;
  552. default:
  553. abort ();
  554. break;
  555. }
  556. return bfd_reloc_ok;
  557. }
  558. #define coff_bfd_merge_private_bfd_data _bfd_generic_verify_endian_match
  559. /* We can do relaxing. */
  560. #define coff_bfd_relax_section sh_relax_section
  561. /* We use the special COFF backend linker. */
  562. #define coff_relocate_section sh_relocate_section
  563. /* When relaxing, we need to use special code to get the relocated
  564. section contents. */
  565. #define coff_bfd_get_relocated_section_contents \
  566. sh_coff_get_relocated_section_contents
  567. #include "coffcode.h"
  568. static bfd_boolean
  569. sh_relax_delete_bytes (bfd *, asection *, bfd_vma, int);
  570. /* This function handles relaxing on the SH.
  571. Function calls on the SH look like this:
  572. movl L1,r0
  573. ...
  574. jsr @r0
  575. ...
  576. L1:
  577. .long function
  578. The compiler and assembler will cooperate to create R_SH_USES
  579. relocs on the jsr instructions. The r_offset field of the
  580. R_SH_USES reloc is the PC relative offset to the instruction which
  581. loads the register (the r_offset field is computed as though it
  582. were a jump instruction, so the offset value is actually from four
  583. bytes past the instruction). The linker can use this reloc to
  584. determine just which function is being called, and thus decide
  585. whether it is possible to replace the jsr with a bsr.
  586. If multiple function calls are all based on a single register load
  587. (i.e., the same function is called multiple times), the compiler
  588. guarantees that each function call will have an R_SH_USES reloc.
  589. Therefore, if the linker is able to convert each R_SH_USES reloc
  590. which refers to that address, it can safely eliminate the register
  591. load.
  592. When the assembler creates an R_SH_USES reloc, it examines it to
  593. determine which address is being loaded (L1 in the above example).
  594. It then counts the number of references to that address, and
  595. creates an R_SH_COUNT reloc at that address. The r_offset field of
  596. the R_SH_COUNT reloc will be the number of references. If the
  597. linker is able to eliminate a register load, it can use the
  598. R_SH_COUNT reloc to see whether it can also eliminate the function
  599. address.
  600. SH relaxing also handles another, unrelated, matter. On the SH, if
  601. a load or store instruction is not aligned on a four byte boundary,
  602. the memory cycle interferes with the 32 bit instruction fetch,
  603. causing a one cycle bubble in the pipeline. Therefore, we try to
  604. align load and store instructions on four byte boundaries if we
  605. can, by swapping them with one of the adjacent instructions. */
  606. static bfd_boolean
  607. sh_relax_section (bfd *abfd,
  608. asection *sec,
  609. struct bfd_link_info *link_info,
  610. bfd_boolean *again)
  611. {
  612. struct internal_reloc *internal_relocs;
  613. bfd_boolean have_code;
  614. struct internal_reloc *irel, *irelend;
  615. bfd_byte *contents = NULL;
  616. *again = FALSE;
  617. if (bfd_link_relocatable (link_info)
  618. || (sec->flags & SEC_RELOC) == 0
  619. || sec->reloc_count == 0)
  620. return TRUE;
  621. if (coff_section_data (abfd, sec) == NULL)
  622. {
  623. bfd_size_type amt = sizeof (struct coff_section_tdata);
  624. sec->used_by_bfd = bfd_zalloc (abfd, amt);
  625. if (sec->used_by_bfd == NULL)
  626. return FALSE;
  627. }
  628. internal_relocs = (_bfd_coff_read_internal_relocs
  629. (abfd, sec, link_info->keep_memory,
  630. (bfd_byte *) NULL, FALSE,
  631. (struct internal_reloc *) NULL));
  632. if (internal_relocs == NULL)
  633. goto error_return;
  634. have_code = FALSE;
  635. irelend = internal_relocs + sec->reloc_count;
  636. for (irel = internal_relocs; irel < irelend; irel++)
  637. {
  638. bfd_vma laddr, paddr, symval;
  639. unsigned short insn;
  640. struct internal_reloc *irelfn, *irelscan, *irelcount;
  641. struct internal_syment sym;
  642. bfd_signed_vma foff;
  643. if (irel->r_type == R_SH_CODE)
  644. have_code = TRUE;
  645. if (irel->r_type != R_SH_USES)
  646. continue;
  647. /* Get the section contents. */
  648. if (contents == NULL)
  649. {
  650. if (coff_section_data (abfd, sec)->contents != NULL)
  651. contents = coff_section_data (abfd, sec)->contents;
  652. else
  653. {
  654. if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  655. goto error_return;
  656. }
  657. }
  658. /* The r_offset field of the R_SH_USES reloc will point us to
  659. the register load. The 4 is because the r_offset field is
  660. computed as though it were a jump offset, which are based
  661. from 4 bytes after the jump instruction. */
  662. laddr = irel->r_vaddr - sec->vma + 4;
  663. /* Careful to sign extend the 32-bit offset. */
  664. laddr += ((irel->r_offset & 0xffffffff) ^ 0x80000000) - 0x80000000;
  665. if (laddr >= sec->size)
  666. {
  667. (*_bfd_error_handler) ("%B: 0x%lx: warning: bad R_SH_USES offset",
  668. abfd, (unsigned long) irel->r_vaddr);
  669. continue;
  670. }
  671. insn = bfd_get_16 (abfd, contents + laddr);
  672. /* If the instruction is not mov.l NN,rN, we don't know what to do. */
  673. if ((insn & 0xf000) != 0xd000)
  674. {
  675. ((*_bfd_error_handler)
  676. ("%B: 0x%lx: warning: R_SH_USES points to unrecognized insn 0x%x",
  677. abfd, (unsigned long) irel->r_vaddr, insn));
  678. continue;
  679. }
  680. /* Get the address from which the register is being loaded. The
  681. displacement in the mov.l instruction is quadrupled. It is a
  682. displacement from four bytes after the movl instruction, but,
  683. before adding in the PC address, two least significant bits
  684. of the PC are cleared. We assume that the section is aligned
  685. on a four byte boundary. */
  686. paddr = insn & 0xff;
  687. paddr *= 4;
  688. paddr += (laddr + 4) &~ (bfd_vma) 3;
  689. if (paddr >= sec->size)
  690. {
  691. ((*_bfd_error_handler)
  692. ("%B: 0x%lx: warning: bad R_SH_USES load offset",
  693. abfd, (unsigned long) irel->r_vaddr));
  694. continue;
  695. }
  696. /* Get the reloc for the address from which the register is
  697. being loaded. This reloc will tell us which function is
  698. actually being called. */
  699. paddr += sec->vma;
  700. for (irelfn = internal_relocs; irelfn < irelend; irelfn++)
  701. if (irelfn->r_vaddr == paddr
  702. #ifdef COFF_WITH_PE
  703. && (irelfn->r_type == R_SH_IMM32
  704. || irelfn->r_type == R_SH_IMM32CE
  705. || irelfn->r_type == R_SH_IMAGEBASE)
  706. #else
  707. && irelfn->r_type == R_SH_IMM32
  708. #endif
  709. )
  710. break;
  711. if (irelfn >= irelend)
  712. {
  713. ((*_bfd_error_handler)
  714. ("%B: 0x%lx: warning: could not find expected reloc",
  715. abfd, (unsigned long) paddr));
  716. continue;
  717. }
  718. /* Get the value of the symbol referred to by the reloc. */
  719. if (! _bfd_coff_get_external_symbols (abfd))
  720. goto error_return;
  721. bfd_coff_swap_sym_in (abfd,
  722. ((bfd_byte *) obj_coff_external_syms (abfd)
  723. + (irelfn->r_symndx
  724. * bfd_coff_symesz (abfd))),
  725. &sym);
  726. if (sym.n_scnum != 0 && sym.n_scnum != sec->target_index)
  727. {
  728. ((*_bfd_error_handler)
  729. ("%B: 0x%lx: warning: symbol in unexpected section",
  730. abfd, (unsigned long) paddr));
  731. continue;
  732. }
  733. if (sym.n_sclass != C_EXT)
  734. {
  735. symval = (sym.n_value
  736. - sec->vma
  737. + sec->output_section->vma
  738. + sec->output_offset);
  739. }
  740. else
  741. {
  742. struct coff_link_hash_entry *h;
  743. h = obj_coff_sym_hashes (abfd)[irelfn->r_symndx];
  744. BFD_ASSERT (h != NULL);
  745. if (h->root.type != bfd_link_hash_defined
  746. && h->root.type != bfd_link_hash_defweak)
  747. {
  748. /* This appears to be a reference to an undefined
  749. symbol. Just ignore it--it will be caught by the
  750. regular reloc processing. */
  751. continue;
  752. }
  753. symval = (h->root.u.def.value
  754. + h->root.u.def.section->output_section->vma
  755. + h->root.u.def.section->output_offset);
  756. }
  757. symval += bfd_get_32 (abfd, contents + paddr - sec->vma);
  758. /* See if this function call can be shortened. */
  759. foff = (symval
  760. - (irel->r_vaddr
  761. - sec->vma
  762. + sec->output_section->vma
  763. + sec->output_offset
  764. + 4));
  765. if (foff < -0x1000 || foff >= 0x1000)
  766. {
  767. /* After all that work, we can't shorten this function call. */
  768. continue;
  769. }
  770. /* Shorten the function call. */
  771. /* For simplicity of coding, we are going to modify the section
  772. contents, the section relocs, and the BFD symbol table. We
  773. must tell the rest of the code not to free up this
  774. information. It would be possible to instead create a table
  775. of changes which have to be made, as is done in coff-mips.c;
  776. that would be more work, but would require less memory when
  777. the linker is run. */
  778. coff_section_data (abfd, sec)->relocs = internal_relocs;
  779. coff_section_data (abfd, sec)->keep_relocs = TRUE;
  780. coff_section_data (abfd, sec)->contents = contents;
  781. coff_section_data (abfd, sec)->keep_contents = TRUE;
  782. obj_coff_keep_syms (abfd) = TRUE;
  783. /* Replace the jsr with a bsr. */
  784. /* Change the R_SH_USES reloc into an R_SH_PCDISP reloc, and
  785. replace the jsr with a bsr. */
  786. irel->r_type = R_SH_PCDISP;
  787. irel->r_symndx = irelfn->r_symndx;
  788. if (sym.n_sclass != C_EXT)
  789. {
  790. /* If this needs to be changed because of future relaxing,
  791. it will be handled here like other internal PCDISP
  792. relocs. */
  793. bfd_put_16 (abfd,
  794. (bfd_vma) 0xb000 | ((foff >> 1) & 0xfff),
  795. contents + irel->r_vaddr - sec->vma);
  796. }
  797. else
  798. {
  799. /* We can't fully resolve this yet, because the external
  800. symbol value may be changed by future relaxing. We let
  801. the final link phase handle it. */
  802. bfd_put_16 (abfd, (bfd_vma) 0xb000,
  803. contents + irel->r_vaddr - sec->vma);
  804. }
  805. /* See if there is another R_SH_USES reloc referring to the same
  806. register load. */
  807. for (irelscan = internal_relocs; irelscan < irelend; irelscan++)
  808. if (irelscan->r_type == R_SH_USES
  809. && laddr == irelscan->r_vaddr - sec->vma + 4 + irelscan->r_offset)
  810. break;
  811. if (irelscan < irelend)
  812. {
  813. /* Some other function call depends upon this register load,
  814. and we have not yet converted that function call.
  815. Indeed, we may never be able to convert it. There is
  816. nothing else we can do at this point. */
  817. continue;
  818. }
  819. /* Look for a R_SH_COUNT reloc on the location where the
  820. function address is stored. Do this before deleting any
  821. bytes, to avoid confusion about the address. */
  822. for (irelcount = internal_relocs; irelcount < irelend; irelcount++)
  823. if (irelcount->r_vaddr == paddr
  824. && irelcount->r_type == R_SH_COUNT)
  825. break;
  826. /* Delete the register load. */
  827. if (! sh_relax_delete_bytes (abfd, sec, laddr, 2))
  828. goto error_return;
  829. /* That will change things, so, just in case it permits some
  830. other function call to come within range, we should relax
  831. again. Note that this is not required, and it may be slow. */
  832. *again = TRUE;
  833. /* Now check whether we got a COUNT reloc. */
  834. if (irelcount >= irelend)
  835. {
  836. ((*_bfd_error_handler)
  837. ("%B: 0x%lx: warning: could not find expected COUNT reloc",
  838. abfd, (unsigned long) paddr));
  839. continue;
  840. }
  841. /* The number of uses is stored in the r_offset field. We've
  842. just deleted one. */
  843. if (irelcount->r_offset == 0)
  844. {
  845. ((*_bfd_error_handler) ("%B: 0x%lx: warning: bad count",
  846. abfd, (unsigned long) paddr));
  847. continue;
  848. }
  849. --irelcount->r_offset;
  850. /* If there are no more uses, we can delete the address. Reload
  851. the address from irelfn, in case it was changed by the
  852. previous call to sh_relax_delete_bytes. */
  853. if (irelcount->r_offset == 0)
  854. {
  855. if (! sh_relax_delete_bytes (abfd, sec,
  856. irelfn->r_vaddr - sec->vma, 4))
  857. goto error_return;
  858. }
  859. /* We've done all we can with that function call. */
  860. }
  861. /* Look for load and store instructions that we can align on four
  862. byte boundaries. */
  863. if (have_code)
  864. {
  865. bfd_boolean swapped;
  866. /* Get the section contents. */
  867. if (contents == NULL)
  868. {
  869. if (coff_section_data (abfd, sec)->contents != NULL)
  870. contents = coff_section_data (abfd, sec)->contents;
  871. else
  872. {
  873. if (!bfd_malloc_and_get_section (abfd, sec, &contents))
  874. goto error_return;
  875. }
  876. }
  877. if (! sh_align_loads (abfd, sec, internal_relocs, contents, &swapped))
  878. goto error_return;
  879. if (swapped)
  880. {
  881. coff_section_data (abfd, sec)->relocs = internal_relocs;
  882. coff_section_data (abfd, sec)->keep_relocs = TRUE;
  883. coff_section_data (abfd, sec)->contents = contents;
  884. coff_section_data (abfd, sec)->keep_contents = TRUE;
  885. obj_coff_keep_syms (abfd) = TRUE;
  886. }
  887. }
  888. if (internal_relocs != NULL
  889. && internal_relocs != coff_section_data (abfd, sec)->relocs)
  890. {
  891. if (! link_info->keep_memory)
  892. free (internal_relocs);
  893. else
  894. coff_section_data (abfd, sec)->relocs = internal_relocs;
  895. }
  896. if (contents != NULL && contents != coff_section_data (abfd, sec)->contents)
  897. {
  898. if (! link_info->keep_memory)
  899. free (contents);
  900. else
  901. /* Cache the section contents for coff_link_input_bfd. */
  902. coff_section_data (abfd, sec)->contents = contents;
  903. }
  904. return TRUE;
  905. error_return:
  906. if (internal_relocs != NULL
  907. && internal_relocs != coff_section_data (abfd, sec)->relocs)
  908. free (internal_relocs);
  909. if (contents != NULL && contents != coff_section_data (abfd, sec)->contents)
  910. free (contents);
  911. return FALSE;
  912. }
  913. /* Delete some bytes from a section while relaxing. */
  914. static bfd_boolean
  915. sh_relax_delete_bytes (bfd *abfd,
  916. asection *sec,
  917. bfd_vma addr,
  918. int count)
  919. {
  920. bfd_byte *contents;
  921. struct internal_reloc *irel, *irelend;
  922. struct internal_reloc *irelalign;
  923. bfd_vma toaddr;
  924. bfd_byte *esym, *esymend;
  925. bfd_size_type symesz;
  926. struct coff_link_hash_entry **sym_hash;
  927. asection *o;
  928. contents = coff_section_data (abfd, sec)->contents;
  929. /* The deletion must stop at the next ALIGN reloc for an aligment
  930. power larger than the number of bytes we are deleting. */
  931. irelalign = NULL;
  932. toaddr = sec->size;
  933. irel = coff_section_data (abfd, sec)->relocs;
  934. irelend = irel + sec->reloc_count;
  935. for (; irel < irelend; irel++)
  936. {
  937. if (irel->r_type == R_SH_ALIGN
  938. && irel->r_vaddr - sec->vma > addr
  939. && count < (1 << irel->r_offset))
  940. {
  941. irelalign = irel;
  942. toaddr = irel->r_vaddr - sec->vma;
  943. break;
  944. }
  945. }
  946. /* Actually delete the bytes. */
  947. memmove (contents + addr, contents + addr + count,
  948. (size_t) (toaddr - addr - count));
  949. if (irelalign == NULL)
  950. sec->size -= count;
  951. else
  952. {
  953. int i;
  954. #define NOP_OPCODE (0x0009)
  955. BFD_ASSERT ((count & 1) == 0);
  956. for (i = 0; i < count; i += 2)
  957. bfd_put_16 (abfd, (bfd_vma) NOP_OPCODE, contents + toaddr - count + i);
  958. }
  959. /* Adjust all the relocs. */
  960. for (irel = coff_section_data (abfd, sec)->relocs; irel < irelend; irel++)
  961. {
  962. bfd_vma nraddr, stop;
  963. bfd_vma start = 0;
  964. int insn = 0;
  965. struct internal_syment sym;
  966. int off, adjust, oinsn;
  967. bfd_signed_vma voff = 0;
  968. bfd_boolean overflow;
  969. /* Get the new reloc address. */
  970. nraddr = irel->r_vaddr - sec->vma;
  971. if ((irel->r_vaddr - sec->vma > addr
  972. && irel->r_vaddr - sec->vma < toaddr)
  973. || (irel->r_type == R_SH_ALIGN
  974. && irel->r_vaddr - sec->vma == toaddr))
  975. nraddr -= count;
  976. /* See if this reloc was for the bytes we have deleted, in which
  977. case we no longer care about it. Don't delete relocs which
  978. represent addresses, though. */
  979. if (irel->r_vaddr - sec->vma >= addr
  980. && irel->r_vaddr - sec->vma < addr + count
  981. && irel->r_type != R_SH_ALIGN
  982. && irel->r_type != R_SH_CODE
  983. && irel->r_type != R_SH_DATA
  984. && irel->r_type != R_SH_LABEL)
  985. irel->r_type = R_SH_UNUSED;
  986. /* If this is a PC relative reloc, see if the range it covers
  987. includes the bytes we have deleted. */
  988. switch (irel->r_type)
  989. {
  990. default:
  991. break;
  992. case R_SH_PCDISP8BY2:
  993. case R_SH_PCDISP:
  994. case R_SH_PCRELIMM8BY2:
  995. case R_SH_PCRELIMM8BY4:
  996. start = irel->r_vaddr - sec->vma;
  997. insn = bfd_get_16 (abfd, contents + nraddr);
  998. break;
  999. }
  1000. switch (irel->r_type)
  1001. {
  1002. default:
  1003. start = stop = addr;
  1004. break;
  1005. case R_SH_IMM32:
  1006. #ifdef COFF_WITH_PE
  1007. case R_SH_IMM32CE:
  1008. case R_SH_IMAGEBASE:
  1009. #endif
  1010. /* If this reloc is against a symbol defined in this
  1011. section, and the symbol will not be adjusted below, we
  1012. must check the addend to see it will put the value in
  1013. range to be adjusted, and hence must be changed. */
  1014. bfd_coff_swap_sym_in (abfd,
  1015. ((bfd_byte *) obj_coff_external_syms (abfd)
  1016. + (irel->r_symndx
  1017. * bfd_coff_symesz (abfd))),
  1018. &sym);
  1019. if (sym.n_sclass != C_EXT
  1020. && sym.n_scnum == sec->target_index
  1021. && ((bfd_vma) sym.n_value <= addr
  1022. || (bfd_vma) sym.n_value >= toaddr))
  1023. {
  1024. bfd_vma val;
  1025. val = bfd_get_32 (abfd, contents + nraddr);
  1026. val += sym.n_value;
  1027. if (val > addr && val < toaddr)
  1028. bfd_put_32 (abfd, val - count, contents + nraddr);
  1029. }
  1030. start = stop = addr;
  1031. break;
  1032. case R_SH_PCDISP8BY2:
  1033. off = insn & 0xff;
  1034. if (off & 0x80)
  1035. off -= 0x100;
  1036. stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
  1037. break;
  1038. case R_SH_PCDISP:
  1039. bfd_coff_swap_sym_in (abfd,
  1040. ((bfd_byte *) obj_coff_external_syms (abfd)
  1041. + (irel->r_symndx
  1042. * bfd_coff_symesz (abfd))),
  1043. &sym);
  1044. if (sym.n_sclass == C_EXT)
  1045. start = stop = addr;
  1046. else
  1047. {
  1048. off = insn & 0xfff;
  1049. if (off & 0x800)
  1050. off -= 0x1000;
  1051. stop = (bfd_vma) ((bfd_signed_vma) start + 4 + off * 2);
  1052. }
  1053. break;
  1054. case R_SH_PCRELIMM8BY2:
  1055. off = insn & 0xff;
  1056. stop = start + 4 + off * 2;
  1057. break;
  1058. case R_SH_PCRELIMM8BY4:
  1059. off = insn & 0xff;
  1060. stop = (start &~ (bfd_vma) 3) + 4 + off * 4;
  1061. break;
  1062. case R_SH_SWITCH8:
  1063. case R_SH_SWITCH16:
  1064. case R_SH_SWITCH32:
  1065. /* These relocs types represent
  1066. .word L2-L1
  1067. The r_offset field holds the difference between the reloc
  1068. address and L1. That is the start of the reloc, and
  1069. adding in the contents gives us the top. We must adjust
  1070. both the r_offset field and the section contents. */
  1071. start = irel->r_vaddr - sec->vma;
  1072. stop = (bfd_vma) ((bfd_signed_vma) start - (long) irel->r_offset);
  1073. if (start > addr
  1074. && start < toaddr
  1075. && (stop <= addr || stop >= toaddr))
  1076. irel->r_offset += count;
  1077. else if (stop > addr
  1078. && stop < toaddr
  1079. && (start <= addr || start >= toaddr))
  1080. irel->r_offset -= count;
  1081. start = stop;
  1082. if (irel->r_type == R_SH_SWITCH16)
  1083. voff = bfd_get_signed_16 (abfd, contents + nraddr);
  1084. else if (irel->r_type == R_SH_SWITCH8)
  1085. voff = bfd_get_8 (abfd, contents + nraddr);
  1086. else
  1087. voff = bfd_get_signed_32 (abfd, contents + nraddr);
  1088. stop = (bfd_vma) ((bfd_signed_vma) start + voff);
  1089. break;
  1090. case R_SH_USES:
  1091. start = irel->r_vaddr - sec->vma;
  1092. stop = (bfd_vma) ((bfd_signed_vma) start
  1093. + (long) irel->r_offset
  1094. + 4);
  1095. break;
  1096. }
  1097. if (start > addr
  1098. && start < toaddr
  1099. && (stop <= addr || stop >= toaddr))
  1100. adjust = count;
  1101. else if (stop > addr
  1102. && stop < toaddr
  1103. && (start <= addr || start >= toaddr))
  1104. adjust = - count;
  1105. else
  1106. adjust = 0;
  1107. if (adjust != 0)
  1108. {
  1109. oinsn = insn;
  1110. overflow = FALSE;
  1111. switch (irel->r_type)
  1112. {
  1113. default:
  1114. abort ();
  1115. break;
  1116. case R_SH_PCDISP8BY2:
  1117. case R_SH_PCRELIMM8BY2:
  1118. insn += adjust / 2;
  1119. if ((oinsn & 0xff00) != (insn & 0xff00))
  1120. overflow = TRUE;
  1121. bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
  1122. break;
  1123. case R_SH_PCDISP:
  1124. insn += adjust / 2;
  1125. if ((oinsn & 0xf000) != (insn & 0xf000))
  1126. overflow = TRUE;
  1127. bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
  1128. break;
  1129. case R_SH_PCRELIMM8BY4:
  1130. BFD_ASSERT (adjust == count || count >= 4);
  1131. if (count >= 4)
  1132. insn += adjust / 4;
  1133. else
  1134. {
  1135. if ((irel->r_vaddr & 3) == 0)
  1136. ++insn;
  1137. }
  1138. if ((oinsn & 0xff00) != (insn & 0xff00))
  1139. overflow = TRUE;
  1140. bfd_put_16 (abfd, (bfd_vma) insn, contents + nraddr);
  1141. break;
  1142. case R_SH_SWITCH8:
  1143. voff += adjust;
  1144. if (voff < 0 || voff >= 0xff)
  1145. overflow = TRUE;
  1146. bfd_put_8 (abfd, (bfd_vma) voff, contents + nraddr);
  1147. break;
  1148. case R_SH_SWITCH16:
  1149. voff += adjust;
  1150. if (voff < - 0x8000 || voff >= 0x8000)
  1151. overflow = TRUE;
  1152. bfd_put_signed_16 (abfd, (bfd_vma) voff, contents + nraddr);
  1153. break;
  1154. case R_SH_SWITCH32:
  1155. voff += adjust;
  1156. bfd_put_signed_32 (abfd, (bfd_vma) voff, contents + nraddr);
  1157. break;
  1158. case R_SH_USES:
  1159. irel->r_offset += adjust;
  1160. break;
  1161. }
  1162. if (overflow)
  1163. {
  1164. ((*_bfd_error_handler)
  1165. ("%B: 0x%lx: fatal: reloc overflow while relaxing",
  1166. abfd, (unsigned long) irel->r_vaddr));
  1167. bfd_set_error (bfd_error_bad_value);
  1168. return FALSE;
  1169. }
  1170. }
  1171. irel->r_vaddr = nraddr + sec->vma;
  1172. }
  1173. /* Look through all the other sections. If there contain any IMM32
  1174. relocs against internal symbols which we are not going to adjust
  1175. below, we may need to adjust the addends. */
  1176. for (o = abfd->sections; o != NULL; o = o->next)
  1177. {
  1178. struct internal_reloc *internal_relocs;
  1179. struct internal_reloc *irelscan, *irelscanend;
  1180. bfd_byte *ocontents;
  1181. if (o == sec
  1182. || (o->flags & SEC_RELOC) == 0
  1183. || o->reloc_count == 0)
  1184. continue;
  1185. /* We always cache the relocs. Perhaps, if info->keep_memory is
  1186. FALSE, we should free them, if we are permitted to, when we
  1187. leave sh_coff_relax_section. */
  1188. internal_relocs = (_bfd_coff_read_internal_relocs
  1189. (abfd, o, TRUE, (bfd_byte *) NULL, FALSE,
  1190. (struct internal_reloc *) NULL));
  1191. if (internal_relocs == NULL)
  1192. return FALSE;
  1193. ocontents = NULL;
  1194. irelscanend = internal_relocs + o->reloc_count;
  1195. for (irelscan = internal_relocs; irelscan < irelscanend; irelscan++)
  1196. {
  1197. struct internal_syment sym;
  1198. #ifdef COFF_WITH_PE
  1199. if (irelscan->r_type != R_SH_IMM32
  1200. && irelscan->r_type != R_SH_IMAGEBASE
  1201. && irelscan->r_type != R_SH_IMM32CE)
  1202. #else
  1203. if (irelscan->r_type != R_SH_IMM32)
  1204. #endif
  1205. continue;
  1206. bfd_coff_swap_sym_in (abfd,
  1207. ((bfd_byte *) obj_coff_external_syms (abfd)
  1208. + (irelscan->r_symndx
  1209. * bfd_coff_symesz (abfd))),
  1210. &sym);
  1211. if (sym.n_sclass != C_EXT
  1212. && sym.n_scnum == sec->target_index
  1213. && ((bfd_vma) sym.n_value <= addr
  1214. || (bfd_vma) sym.n_value >= toaddr))
  1215. {
  1216. bfd_vma val;
  1217. if (ocontents == NULL)
  1218. {
  1219. if (coff_section_data (abfd, o)->contents != NULL)
  1220. ocontents = coff_section_data (abfd, o)->contents;
  1221. else
  1222. {
  1223. if (!bfd_malloc_and_get_section (abfd, o, &ocontents))
  1224. return FALSE;
  1225. /* We always cache the section contents.
  1226. Perhaps, if info->keep_memory is FALSE, we
  1227. should free them, if we are permitted to,
  1228. when we leave sh_coff_relax_section. */
  1229. coff_section_data (abfd, o)->contents = ocontents;
  1230. }
  1231. }
  1232. val = bfd_get_32 (abfd, ocontents + irelscan->r_vaddr - o->vma);
  1233. val += sym.n_value;
  1234. if (val > addr && val < toaddr)
  1235. bfd_put_32 (abfd, val - count,
  1236. ocontents + irelscan->r_vaddr - o->vma);
  1237. coff_section_data (abfd, o)->keep_contents = TRUE;
  1238. }
  1239. }
  1240. }
  1241. /* Adjusting the internal symbols will not work if something has
  1242. already retrieved the generic symbols. It would be possible to
  1243. make this work by adjusting the generic symbols at the same time.
  1244. However, this case should not arise in normal usage. */
  1245. if (obj_symbols (abfd) != NULL
  1246. || obj_raw_syments (abfd) != NULL)
  1247. {
  1248. ((*_bfd_error_handler)
  1249. ("%B: fatal: generic symbols retrieved before relaxing", abfd));
  1250. bfd_set_error (bfd_error_invalid_operation);
  1251. return FALSE;
  1252. }
  1253. /* Adjust all the symbols. */
  1254. sym_hash = obj_coff_sym_hashes (abfd);
  1255. symesz = bfd_coff_symesz (abfd);
  1256. esym = (bfd_byte *) obj_coff_external_syms (abfd);
  1257. esymend = esym + obj_raw_syment_count (abfd) * symesz;
  1258. while (esym < esymend)
  1259. {
  1260. struct internal_syment isym;
  1261. bfd_coff_swap_sym_in (abfd, esym, &isym);
  1262. if (isym.n_scnum == sec->target_index
  1263. && (bfd_vma) isym.n_value > addr
  1264. && (bfd_vma) isym.n_value < toaddr)
  1265. {
  1266. isym.n_value -= count;
  1267. bfd_coff_swap_sym_out (abfd, &isym, esym);
  1268. if (*sym_hash != NULL)
  1269. {
  1270. BFD_ASSERT ((*sym_hash)->root.type == bfd_link_hash_defined
  1271. || (*sym_hash)->root.type == bfd_link_hash_defweak);
  1272. BFD_ASSERT ((*sym_hash)->root.u.def.value >= addr
  1273. && (*sym_hash)->root.u.def.value < toaddr);
  1274. (*sym_hash)->root.u.def.value -= count;
  1275. }
  1276. }
  1277. esym += (isym.n_numaux + 1) * symesz;
  1278. sym_hash += isym.n_numaux + 1;
  1279. }
  1280. /* See if we can move the ALIGN reloc forward. We have adjusted
  1281. r_vaddr for it already. */
  1282. if (irelalign != NULL)
  1283. {
  1284. bfd_vma alignto, alignaddr;
  1285. alignto = BFD_ALIGN (toaddr, 1 << irelalign->r_offset);
  1286. alignaddr = BFD_ALIGN (irelalign->r_vaddr - sec->vma,
  1287. 1 << irelalign->r_offset);
  1288. if (alignto != alignaddr)
  1289. {
  1290. /* Tail recursion. */
  1291. return sh_relax_delete_bytes (abfd, sec, alignaddr,
  1292. (int) (alignto - alignaddr));
  1293. }
  1294. }
  1295. return TRUE;
  1296. }
  1297. /* This is yet another version of the SH opcode table, used to rapidly
  1298. get information about a particular instruction. */
  1299. /* The opcode map is represented by an array of these structures. The
  1300. array is indexed by the high order four bits in the instruction. */
  1301. struct sh_major_opcode
  1302. {
  1303. /* A pointer to the instruction list. This is an array which
  1304. contains all the instructions with this major opcode. */
  1305. const struct sh_minor_opcode *minor_opcodes;
  1306. /* The number of elements in minor_opcodes. */
  1307. unsigned short count;
  1308. };
  1309. /* This structure holds information for a set of SH opcodes. The
  1310. instruction code is anded with the mask value, and the resulting
  1311. value is used to search the order opcode list. */
  1312. struct sh_minor_opcode
  1313. {
  1314. /* The sorted opcode list. */
  1315. const struct sh_opcode *opcodes;
  1316. /* The number of elements in opcodes. */
  1317. unsigned short count;
  1318. /* The mask value to use when searching the opcode list. */
  1319. unsigned short mask;
  1320. };
  1321. /* This structure holds information for an SH instruction. An array
  1322. of these structures is sorted in order by opcode. */
  1323. struct sh_opcode
  1324. {
  1325. /* The code for this instruction, after it has been anded with the
  1326. mask value in the sh_major_opcode structure. */
  1327. unsigned short opcode;
  1328. /* Flags for this instruction. */
  1329. unsigned long flags;
  1330. };
  1331. /* Flag which appear in the sh_opcode structure. */
  1332. /* This instruction loads a value from memory. */
  1333. #define LOAD (0x1)
  1334. /* This instruction stores a value to memory. */
  1335. #define STORE (0x2)
  1336. /* This instruction is a branch. */
  1337. #define BRANCH (0x4)
  1338. /* This instruction has a delay slot. */
  1339. #define DELAY (0x8)
  1340. /* This instruction uses the value in the register in the field at
  1341. mask 0x0f00 of the instruction. */
  1342. #define USES1 (0x10)
  1343. #define USES1_REG(x) ((x & 0x0f00) >> 8)
  1344. /* This instruction uses the value in the register in the field at
  1345. mask 0x00f0 of the instruction. */
  1346. #define USES2 (0x20)
  1347. #define USES2_REG(x) ((x & 0x00f0) >> 4)
  1348. /* This instruction uses the value in register 0. */
  1349. #define USESR0 (0x40)
  1350. /* This instruction sets the value in the register in the field at
  1351. mask 0x0f00 of the instruction. */
  1352. #define SETS1 (0x80)
  1353. #define SETS1_REG(x) ((x & 0x0f00) >> 8)
  1354. /* This instruction sets the value in the register in the field at
  1355. mask 0x00f0 of the instruction. */
  1356. #define SETS2 (0x100)
  1357. #define SETS2_REG(x) ((x & 0x00f0) >> 4)
  1358. /* This instruction sets register 0. */
  1359. #define SETSR0 (0x200)
  1360. /* This instruction sets a special register. */
  1361. #define SETSSP (0x400)
  1362. /* This instruction uses a special register. */
  1363. #define USESSP (0x800)
  1364. /* This instruction uses the floating point register in the field at
  1365. mask 0x0f00 of the instruction. */
  1366. #define USESF1 (0x1000)
  1367. #define USESF1_REG(x) ((x & 0x0f00) >> 8)
  1368. /* This instruction uses the floating point register in the field at
  1369. mask 0x00f0 of the instruction. */
  1370. #define USESF2 (0x2000)
  1371. #define USESF2_REG(x) ((x & 0x00f0) >> 4)
  1372. /* This instruction uses floating point register 0. */
  1373. #define USESF0 (0x4000)
  1374. /* This instruction sets the floating point register in the field at
  1375. mask 0x0f00 of the instruction. */
  1376. #define SETSF1 (0x8000)
  1377. #define SETSF1_REG(x) ((x & 0x0f00) >> 8)
  1378. #define USESAS (0x10000)
  1379. #define USESAS_REG(x) (((((x) >> 8) - 2) & 3) + 2)
  1380. #define USESR8 (0x20000)
  1381. #define SETSAS (0x40000)
  1382. #define SETSAS_REG(x) USESAS_REG (x)
  1383. #define MAP(a) a, sizeof a / sizeof a[0]
  1384. #ifndef COFF_IMAGE_WITH_PE
  1385. /* The opcode maps. */
  1386. static const struct sh_opcode sh_opcode00[] =
  1387. {
  1388. { 0x0008, SETSSP }, /* clrt */
  1389. { 0x0009, 0 }, /* nop */
  1390. { 0x000b, BRANCH | DELAY | USESSP }, /* rts */
  1391. { 0x0018, SETSSP }, /* sett */
  1392. { 0x0019, SETSSP }, /* div0u */
  1393. { 0x001b, 0 }, /* sleep */
  1394. { 0x0028, SETSSP }, /* clrmac */
  1395. { 0x002b, BRANCH | DELAY | SETSSP }, /* rte */
  1396. { 0x0038, USESSP | SETSSP }, /* ldtlb */
  1397. { 0x0048, SETSSP }, /* clrs */
  1398. { 0x0058, SETSSP } /* sets */
  1399. };
  1400. static const struct sh_opcode sh_opcode01[] =
  1401. {
  1402. { 0x0003, BRANCH | DELAY | USES1 | SETSSP }, /* bsrf rn */
  1403. { 0x000a, SETS1 | USESSP }, /* sts mach,rn */
  1404. { 0x001a, SETS1 | USESSP }, /* sts macl,rn */
  1405. { 0x0023, BRANCH | DELAY | USES1 }, /* braf rn */
  1406. { 0x0029, SETS1 | USESSP }, /* movt rn */
  1407. { 0x002a, SETS1 | USESSP }, /* sts pr,rn */
  1408. { 0x005a, SETS1 | USESSP }, /* sts fpul,rn */
  1409. { 0x006a, SETS1 | USESSP }, /* sts fpscr,rn / sts dsr,rn */
  1410. { 0x0083, LOAD | USES1 }, /* pref @rn */
  1411. { 0x007a, SETS1 | USESSP }, /* sts a0,rn */
  1412. { 0x008a, SETS1 | USESSP }, /* sts x0,rn */
  1413. { 0x009a, SETS1 | USESSP }, /* sts x1,rn */
  1414. { 0x00aa, SETS1 | USESSP }, /* sts y0,rn */
  1415. { 0x00ba, SETS1 | USESSP } /* sts y1,rn */
  1416. };
  1417. static const struct sh_opcode sh_opcode02[] =
  1418. {
  1419. { 0x0002, SETS1 | USESSP }, /* stc <special_reg>,rn */
  1420. { 0x0004, STORE | USES1 | USES2 | USESR0 }, /* mov.b rm,@(r0,rn) */
  1421. { 0x0005, STORE | USES1 | USES2 | USESR0 }, /* mov.w rm,@(r0,rn) */
  1422. { 0x0006, STORE | USES1 | USES2 | USESR0 }, /* mov.l rm,@(r0,rn) */
  1423. { 0x0007, SETSSP | USES1 | USES2 }, /* mul.l rm,rn */
  1424. { 0x000c, LOAD | SETS1 | USES2 | USESR0 }, /* mov.b @(r0,rm),rn */
  1425. { 0x000d, LOAD | SETS1 | USES2 | USESR0 }, /* mov.w @(r0,rm),rn */
  1426. { 0x000e, LOAD | SETS1 | USES2 | USESR0 }, /* mov.l @(r0,rm),rn */
  1427. { 0x000f, LOAD|SETS1|SETS2|SETSSP|USES1|USES2|USESSP }, /* mac.l @rm+,@rn+ */
  1428. };
  1429. static const struct sh_minor_opcode sh_opcode0[] =
  1430. {
  1431. { MAP (sh_opcode00), 0xffff },
  1432. { MAP (sh_opcode01), 0xf0ff },
  1433. { MAP (sh_opcode02), 0xf00f }
  1434. };
  1435. static const struct sh_opcode sh_opcode10[] =
  1436. {
  1437. { 0x1000, STORE | USES1 | USES2 } /* mov.l rm,@(disp,rn) */
  1438. };
  1439. static const struct sh_minor_opcode sh_opcode1[] =
  1440. {
  1441. { MAP (sh_opcode10), 0xf000 }
  1442. };
  1443. static const struct sh_opcode sh_opcode20[] =
  1444. {
  1445. { 0x2000, STORE | USES1 | USES2 }, /* mov.b rm,@rn */
  1446. { 0x2001, STORE | USES1 | USES2 }, /* mov.w rm,@rn */
  1447. { 0x2002, STORE | USES1 | USES2 }, /* mov.l rm,@rn */
  1448. { 0x2004, STORE | SETS1 | USES1 | USES2 }, /* mov.b rm,@-rn */
  1449. { 0x2005, STORE | SETS1 | USES1 | USES2 }, /* mov.w rm,@-rn */
  1450. { 0x2006, STORE | SETS1 | USES1 | USES2 }, /* mov.l rm,@-rn */
  1451. { 0x2007, SETSSP | USES1 | USES2 | USESSP }, /* div0s */
  1452. { 0x2008, SETSSP | USES1 | USES2 }, /* tst rm,rn */
  1453. { 0x2009, SETS1 | USES1 | USES2 }, /* and rm,rn */
  1454. { 0x200a, SETS1 | USES1 | USES2 }, /* xor rm,rn */
  1455. { 0x200b, SETS1 | USES1 | USES2 }, /* or rm,rn */
  1456. { 0x200c, SETSSP | USES1 | USES2 }, /* cmp/str rm,rn */
  1457. { 0x200d, SETS1 | USES1 | USES2 }, /* xtrct rm,rn */
  1458. { 0x200e, SETSSP | USES1 | USES2 }, /* mulu.w rm,rn */
  1459. { 0x200f, SETSSP | USES1 | USES2 } /* muls.w rm,rn */
  1460. };
  1461. static const struct sh_minor_opcode sh_opcode2[] =
  1462. {
  1463. { MAP (sh_opcode20), 0xf00f }
  1464. };
  1465. static const struct sh_opcode sh_opcode30[] =
  1466. {
  1467. { 0x3000, SETSSP | USES1 | USES2 }, /* cmp/eq rm,rn */
  1468. { 0x3002, SETSSP | USES1 | USES2 }, /* cmp/hs rm,rn */
  1469. { 0x3003, SETSSP | USES1 | USES2 }, /* cmp/ge rm,rn */
  1470. { 0x3004, SETSSP | USESSP | USES1 | USES2 }, /* div1 rm,rn */
  1471. { 0x3005, SETSSP | USES1 | USES2 }, /* dmulu.l rm,rn */
  1472. { 0x3006, SETSSP | USES1 | USES2 }, /* cmp/hi rm,rn */
  1473. { 0x3007, SETSSP | USES1 | USES2 }, /* cmp/gt rm,rn */
  1474. { 0x3008, SETS1 | USES1 | USES2 }, /* sub rm,rn */
  1475. { 0x300a, SETS1 | SETSSP | USES1 | USES2 | USESSP }, /* subc rm,rn */
  1476. { 0x300b, SETS1 | SETSSP | USES1 | USES2 }, /* subv rm,rn */
  1477. { 0x300c, SETS1 | USES1 | USES2 }, /* add rm,rn */
  1478. { 0x300d, SETSSP | USES1 | USES2 }, /* dmuls.l rm,rn */
  1479. { 0x300e, SETS1 | SETSSP | USES1 | USES2 | USESSP }, /* addc rm,rn */
  1480. { 0x300f, SETS1 | SETSSP | USES1 | USES2 } /* addv rm,rn */
  1481. };
  1482. static const struct sh_minor_opcode sh_opcode3[] =
  1483. {
  1484. { MAP (sh_opcode30), 0xf00f }
  1485. };
  1486. static const struct sh_opcode sh_opcode40[] =
  1487. {
  1488. { 0x4000, SETS1 | SETSSP | USES1 }, /* shll rn */
  1489. { 0x4001, SETS1 | SETSSP | USES1 }, /* shlr rn */
  1490. { 0x4002, STORE | SETS1 | USES1 | USESSP }, /* sts.l mach,@-rn */
  1491. { 0x4004, SETS1 | SETSSP | USES1 }, /* rotl rn */
  1492. { 0x4005, SETS1 | SETSSP | USES1 }, /* rotr rn */
  1493. { 0x4006, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,mach */
  1494. { 0x4008, SETS1 | USES1 }, /* shll2 rn */
  1495. { 0x4009, SETS1 | USES1 }, /* shlr2 rn */
  1496. { 0x400a, SETSSP | USES1 }, /* lds rm,mach */
  1497. { 0x400b, BRANCH | DELAY | USES1 }, /* jsr @rn */
  1498. { 0x4010, SETS1 | SETSSP | USES1 }, /* dt rn */
  1499. { 0x4011, SETSSP | USES1 }, /* cmp/pz rn */
  1500. { 0x4012, STORE | SETS1 | USES1 | USESSP }, /* sts.l macl,@-rn */
  1501. { 0x4014, SETSSP | USES1 }, /* setrc rm */
  1502. { 0x4015, SETSSP | USES1 }, /* cmp/pl rn */
  1503. { 0x4016, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,macl */
  1504. { 0x4018, SETS1 | USES1 }, /* shll8 rn */
  1505. { 0x4019, SETS1 | USES1 }, /* shlr8 rn */
  1506. { 0x401a, SETSSP | USES1 }, /* lds rm,macl */
  1507. { 0x401b, LOAD | SETSSP | USES1 }, /* tas.b @rn */
  1508. { 0x4020, SETS1 | SETSSP | USES1 }, /* shal rn */
  1509. { 0x4021, SETS1 | SETSSP | USES1 }, /* shar rn */
  1510. { 0x4022, STORE | SETS1 | USES1 | USESSP }, /* sts.l pr,@-rn */
  1511. { 0x4024, SETS1 | SETSSP | USES1 | USESSP }, /* rotcl rn */
  1512. { 0x4025, SETS1 | SETSSP | USES1 | USESSP }, /* rotcr rn */
  1513. { 0x4026, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,pr */
  1514. { 0x4028, SETS1 | USES1 }, /* shll16 rn */
  1515. { 0x4029, SETS1 | USES1 }, /* shlr16 rn */
  1516. { 0x402a, SETSSP | USES1 }, /* lds rm,pr */
  1517. { 0x402b, BRANCH | DELAY | USES1 }, /* jmp @rn */
  1518. { 0x4052, STORE | SETS1 | USES1 | USESSP }, /* sts.l fpul,@-rn */
  1519. { 0x4056, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,fpul */
  1520. { 0x405a, SETSSP | USES1 }, /* lds.l rm,fpul */
  1521. { 0x4062, STORE | SETS1 | USES1 | USESSP }, /* sts.l fpscr / dsr,@-rn */
  1522. { 0x4066, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,fpscr / dsr */
  1523. { 0x406a, SETSSP | USES1 }, /* lds rm,fpscr / lds rm,dsr */
  1524. { 0x4072, STORE | SETS1 | USES1 | USESSP }, /* sts.l a0,@-rn */
  1525. { 0x4076, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,a0 */
  1526. { 0x407a, SETSSP | USES1 }, /* lds.l rm,a0 */
  1527. { 0x4082, STORE | SETS1 | USES1 | USESSP }, /* sts.l x0,@-rn */
  1528. { 0x4086, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,x0 */
  1529. { 0x408a, SETSSP | USES1 }, /* lds.l rm,x0 */
  1530. { 0x4092, STORE | SETS1 | USES1 | USESSP }, /* sts.l x1,@-rn */
  1531. { 0x4096, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,x1 */
  1532. { 0x409a, SETSSP | USES1 }, /* lds.l rm,x1 */
  1533. { 0x40a2, STORE | SETS1 | USES1 | USESSP }, /* sts.l y0,@-rn */
  1534. { 0x40a6, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,y0 */
  1535. { 0x40aa, SETSSP | USES1 }, /* lds.l rm,y0 */
  1536. { 0x40b2, STORE | SETS1 | USES1 | USESSP }, /* sts.l y1,@-rn */
  1537. { 0x40b6, LOAD | SETS1 | SETSSP | USES1 }, /* lds.l @rm+,y1 */
  1538. { 0x40ba, SETSSP | USES1 } /* lds.l rm,y1 */
  1539. };
  1540. static const struct sh_opcode sh_opcode41[] =
  1541. {
  1542. { 0x4003, STORE | SETS1 | USES1 | USESSP }, /* stc.l <special_reg>,@-rn */
  1543. { 0x4007, LOAD | SETS1 | SETSSP | USES1 }, /* ldc.l @rm+,<special_reg> */
  1544. { 0x400c, SETS1 | USES1 | USES2 }, /* shad rm,rn */
  1545. { 0x400d, SETS1 | USES1 | USES2 }, /* shld rm,rn */
  1546. { 0x400e, SETSSP | USES1 }, /* ldc rm,<special_reg> */
  1547. { 0x400f, LOAD|SETS1|SETS2|SETSSP|USES1|USES2|USESSP }, /* mac.w @rm+,@rn+ */
  1548. };
  1549. static const struct sh_minor_opcode sh_opcode4[] =
  1550. {
  1551. { MAP (sh_opcode40), 0xf0ff },
  1552. { MAP (sh_opcode41), 0xf00f }
  1553. };
  1554. static const struct sh_opcode sh_opcode50[] =
  1555. {
  1556. { 0x5000, LOAD | SETS1 | USES2 } /* mov.l @(disp,rm),rn */
  1557. };
  1558. static const struct sh_minor_opcode sh_opcode5[] =
  1559. {
  1560. { MAP (sh_opcode50), 0xf000 }
  1561. };
  1562. static const struct sh_opcode sh_opcode60[] =
  1563. {
  1564. { 0x6000, LOAD | SETS1 | USES2 }, /* mov.b @rm,rn */
  1565. { 0x6001, LOAD | SETS1 | USES2 }, /* mov.w @rm,rn */
  1566. { 0x6002, LOAD | SETS1 | USES2 }, /* mov.l @rm,rn */
  1567. { 0x6003, SETS1 | USES2 }, /* mov rm,rn */
  1568. { 0x6004, LOAD | SETS1 | SETS2 | USES2 }, /* mov.b @rm+,rn */
  1569. { 0x6005, LOAD | SETS1 | SETS2 | USES2 }, /* mov.w @rm+,rn */
  1570. { 0x6006, LOAD | SETS1 | SETS2 | USES2 }, /* mov.l @rm+,rn */
  1571. { 0x6007, SETS1 | USES2 }, /* not rm,rn */
  1572. { 0x6008, SETS1 | USES2 }, /* swap.b rm,rn */
  1573. { 0x6009, SETS1 | USES2 }, /* swap.w rm,rn */
  1574. { 0x600a, SETS1 | SETSSP | USES2 | USESSP }, /* negc rm,rn */
  1575. { 0x600b, SETS1 | USES2 }, /* neg rm,rn */
  1576. { 0x600c, SETS1 | USES2 }, /* extu.b rm,rn */
  1577. { 0x600d, SETS1 | USES2 }, /* extu.w rm,rn */
  1578. { 0x600e, SETS1 | USES2 }, /* exts.b rm,rn */
  1579. { 0x600f, SETS1 | USES2 } /* exts.w rm,rn */
  1580. };
  1581. static const struct sh_minor_opcode sh_opcode6[] =
  1582. {
  1583. { MAP (sh_opcode60), 0xf00f }
  1584. };
  1585. static const struct sh_opcode sh_opcode70[] =
  1586. {
  1587. { 0x7000, SETS1 | USES1 } /* add #imm,rn */
  1588. };
  1589. static const struct sh_minor_opcode sh_opcode7[] =
  1590. {
  1591. { MAP (sh_opcode70), 0xf000 }
  1592. };
  1593. static const struct sh_opcode sh_opcode80[] =
  1594. {
  1595. { 0x8000, STORE | USES2 | USESR0 }, /* mov.b r0,@(disp,rn) */
  1596. { 0x8100, STORE | USES2 | USESR0 }, /* mov.w r0,@(disp,rn) */
  1597. { 0x8200, SETSSP }, /* setrc #imm */
  1598. { 0x8400, LOAD | SETSR0 | USES2 }, /* mov.b @(disp,rm),r0 */
  1599. { 0x8500, LOAD | SETSR0 | USES2 }, /* mov.w @(disp,rn),r0 */
  1600. { 0x8800, SETSSP | USESR0 }, /* cmp/eq #imm,r0 */
  1601. { 0x8900, BRANCH | USESSP }, /* bt label */
  1602. { 0x8b00, BRANCH | USESSP }, /* bf label */
  1603. { 0x8c00, SETSSP }, /* ldrs @(disp,pc) */
  1604. { 0x8d00, BRANCH | DELAY | USESSP }, /* bt/s label */
  1605. { 0x8e00, SETSSP }, /* ldre @(disp,pc) */
  1606. { 0x8f00, BRANCH | DELAY | USESSP } /* bf/s label */
  1607. };
  1608. static const struct sh_minor_opcode sh_opcode8[] =
  1609. {
  1610. { MAP (sh_opcode80), 0xff00 }
  1611. };
  1612. static const struct sh_opcode sh_opcode90[] =
  1613. {
  1614. { 0x9000, LOAD | SETS1 } /* mov.w @(disp,pc),rn */
  1615. };
  1616. static const struct sh_minor_opcode sh_opcode9[] =
  1617. {
  1618. { MAP (sh_opcode90), 0xf000 }
  1619. };
  1620. static const struct sh_opcode sh_opcodea0[] =
  1621. {
  1622. { 0xa000, BRANCH | DELAY } /* bra label */
  1623. };
  1624. static const struct sh_minor_opcode sh_opcodea[] =
  1625. {
  1626. { MAP (sh_opcodea0), 0xf000 }
  1627. };
  1628. static const struct sh_opcode sh_opcodeb0[] =
  1629. {
  1630. { 0xb000, BRANCH | DELAY } /* bsr label */
  1631. };
  1632. static const struct sh_minor_opcode sh_opcodeb[] =
  1633. {
  1634. { MAP (sh_opcodeb0), 0xf000 }
  1635. };
  1636. static const struct sh_opcode sh_opcodec0[] =
  1637. {
  1638. { 0xc000, STORE | USESR0 | USESSP }, /* mov.b r0,@(disp,gbr) */
  1639. { 0xc100, STORE | USESR0 | USESSP }, /* mov.w r0,@(disp,gbr) */
  1640. { 0xc200, STORE | USESR0 | USESSP }, /* mov.l r0,@(disp,gbr) */
  1641. { 0xc300, BRANCH | USESSP }, /* trapa #imm */
  1642. { 0xc400, LOAD | SETSR0 | USESSP }, /* mov.b @(disp,gbr),r0 */
  1643. { 0xc500, LOAD | SETSR0 | USESSP }, /* mov.w @(disp,gbr),r0 */
  1644. { 0xc600, LOAD | SETSR0 | USESSP }, /* mov.l @(disp,gbr),r0 */
  1645. { 0xc700, SETSR0 }, /* mova @(disp,pc),r0 */
  1646. { 0xc800, SETSSP | USESR0 }, /* tst #imm,r0 */
  1647. { 0xc900, SETSR0 | USESR0 }, /* and #imm,r0 */
  1648. { 0xca00, SETSR0 | USESR0 }, /* xor #imm,r0 */
  1649. { 0xcb00, SETSR0 | USESR0 }, /* or #imm,r0 */
  1650. { 0xcc00, LOAD | SETSSP | USESR0 | USESSP }, /* tst.b #imm,@(r0,gbr) */
  1651. { 0xcd00, LOAD | STORE | USESR0 | USESSP }, /* and.b #imm,@(r0,gbr) */
  1652. { 0xce00, LOAD | STORE | USESR0 | USESSP }, /* xor.b #imm,@(r0,gbr) */
  1653. { 0xcf00, LOAD | STORE | USESR0 | USESSP } /* or.b #imm,@(r0,gbr) */
  1654. };
  1655. static const struct sh_minor_opcode sh_opcodec[] =
  1656. {
  1657. { MAP (sh_opcodec0), 0xff00 }
  1658. };
  1659. static const struct sh_opcode sh_opcoded0[] =
  1660. {
  1661. { 0xd000, LOAD | SETS1 } /* mov.l @(disp,pc),rn */
  1662. };
  1663. static const struct sh_minor_opcode sh_opcoded[] =
  1664. {
  1665. { MAP (sh_opcoded0), 0xf000 }
  1666. };
  1667. static const struct sh_opcode sh_opcodee0[] =
  1668. {
  1669. { 0xe000, SETS1 } /* mov #imm,rn */
  1670. };
  1671. static const struct sh_minor_opcode sh_opcodee[] =
  1672. {
  1673. { MAP (sh_opcodee0), 0xf000 }
  1674. };
  1675. static const struct sh_opcode sh_opcodef0[] =
  1676. {
  1677. { 0xf000, SETSF1 | USESF1 | USESF2 }, /* fadd fm,fn */
  1678. { 0xf001, SETSF1 | USESF1 | USESF2 }, /* fsub fm,fn */
  1679. { 0xf002, SETSF1 | USESF1 | USESF2 }, /* fmul fm,fn */
  1680. { 0xf003, SETSF1 | USESF1 | USESF2 }, /* fdiv fm,fn */
  1681. { 0xf004, SETSSP | USESF1 | USESF2 }, /* fcmp/eq fm,fn */
  1682. { 0xf005, SETSSP | USESF1 | USESF2 }, /* fcmp/gt fm,fn */
  1683. { 0xf006, LOAD | SETSF1 | USES2 | USESR0 }, /* fmov.s @(r0,rm),fn */
  1684. { 0xf007, STORE | USES1 | USESF2 | USESR0 }, /* fmov.s fm,@(r0,rn) */
  1685. { 0xf008, LOAD | SETSF1 | USES2 }, /* fmov.s @rm,fn */
  1686. { 0xf009, LOAD | SETS2 | SETSF1 | USES2 }, /* fmov.s @rm+,fn */
  1687. { 0xf00a, STORE | USES1 | USESF2 }, /* fmov.s fm,@rn */
  1688. { 0xf00b, STORE | SETS1 | USES1 | USESF2 }, /* fmov.s fm,@-rn */
  1689. { 0xf00c, SETSF1 | USESF2 }, /* fmov fm,fn */
  1690. { 0xf00e, SETSF1 | USESF1 | USESF2 | USESF0 } /* fmac f0,fm,fn */
  1691. };
  1692. static const struct sh_opcode sh_opcodef1[] =
  1693. {
  1694. { 0xf00d, SETSF1 | USESSP }, /* fsts fpul,fn */
  1695. { 0xf01d, SETSSP | USESF1 }, /* flds fn,fpul */
  1696. { 0xf02d, SETSF1 | USESSP }, /* float fpul,fn */
  1697. { 0xf03d, SETSSP | USESF1 }, /* ftrc fn,fpul */
  1698. { 0xf04d, SETSF1 | USESF1 }, /* fneg fn */
  1699. { 0xf05d, SETSF1 | USESF1 }, /* fabs fn */
  1700. { 0xf06d, SETSF1 | USESF1 }, /* fsqrt fn */
  1701. { 0xf07d, SETSSP | USESF1 }, /* ftst/nan fn */
  1702. { 0xf08d, SETSF1 }, /* fldi0 fn */
  1703. { 0xf09d, SETSF1 } /* fldi1 fn */
  1704. };
  1705. static const struct sh_minor_opcode sh_opcodef[] =
  1706. {
  1707. { MAP (sh_opcodef0), 0xf00f },
  1708. { MAP (sh_opcodef1), 0xf0ff }
  1709. };
  1710. static struct sh_major_opcode sh_opcodes[] =
  1711. {
  1712. { MAP (sh_opcode0) },
  1713. { MAP (sh_opcode1) },
  1714. { MAP (sh_opcode2) },
  1715. { MAP (sh_opcode3) },
  1716. { MAP (sh_opcode4) },
  1717. { MAP (sh_opcode5) },
  1718. { MAP (sh_opcode6) },
  1719. { MAP (sh_opcode7) },
  1720. { MAP (sh_opcode8) },
  1721. { MAP (sh_opcode9) },
  1722. { MAP (sh_opcodea) },
  1723. { MAP (sh_opcodeb) },
  1724. { MAP (sh_opcodec) },
  1725. { MAP (sh_opcoded) },
  1726. { MAP (sh_opcodee) },
  1727. { MAP (sh_opcodef) }
  1728. };
  1729. /* The double data transfer / parallel processing insns are not
  1730. described here. This will cause sh_align_load_span to leave them alone. */
  1731. static const struct sh_opcode sh_dsp_opcodef0[] =
  1732. {
  1733. { 0xf400, USESAS | SETSAS | LOAD | SETSSP }, /* movs.x @-as,ds */
  1734. { 0xf401, USESAS | SETSAS | STORE | USESSP }, /* movs.x ds,@-as */
  1735. { 0xf404, USESAS | LOAD | SETSSP }, /* movs.x @as,ds */
  1736. { 0xf405, USESAS | STORE | USESSP }, /* movs.x ds,@as */
  1737. { 0xf408, USESAS | SETSAS | LOAD | SETSSP }, /* movs.x @as+,ds */
  1738. { 0xf409, USESAS | SETSAS | STORE | USESSP }, /* movs.x ds,@as+ */
  1739. { 0xf40c, USESAS | SETSAS | LOAD | SETSSP | USESR8 }, /* movs.x @as+r8,ds */
  1740. { 0xf40d, USESAS | SETSAS | STORE | USESSP | USESR8 } /* movs.x ds,@as+r8 */
  1741. };
  1742. static const struct sh_minor_opcode sh_dsp_opcodef[] =
  1743. {
  1744. { MAP (sh_dsp_opcodef0), 0xfc0d }
  1745. };
  1746. /* Given an instruction, return a pointer to the corresponding
  1747. sh_opcode structure. Return NULL if the instruction is not
  1748. recognized. */
  1749. static const struct sh_opcode *
  1750. sh_insn_info (unsigned int insn)
  1751. {
  1752. const struct sh_major_opcode *maj;
  1753. const struct sh_minor_opcode *min, *minend;
  1754. maj = &sh_opcodes[(insn & 0xf000) >> 12];
  1755. min = maj->minor_opcodes;
  1756. minend = min + maj->count;
  1757. for (; min < minend; min++)
  1758. {
  1759. unsigned int l;
  1760. const struct sh_opcode *op, *opend;
  1761. l = insn & min->mask;
  1762. op = min->opcodes;
  1763. opend = op + min->count;
  1764. /* Since the opcodes tables are sorted, we could use a binary
  1765. search here if the count were above some cutoff value. */
  1766. for (; op < opend; op++)
  1767. if (op->opcode == l)
  1768. return op;
  1769. }
  1770. return NULL;
  1771. }
  1772. /* See whether an instruction uses a general purpose register. */
  1773. static bfd_boolean
  1774. sh_insn_uses_reg (unsigned int insn,
  1775. const struct sh_opcode *op,
  1776. unsigned int reg)
  1777. {
  1778. unsigned int f;
  1779. f = op->flags;
  1780. if ((f & USES1) != 0
  1781. && USES1_REG (insn) == reg)
  1782. return TRUE;
  1783. if ((f & USES2) != 0
  1784. && USES2_REG (insn) == reg)
  1785. return TRUE;
  1786. if ((f & USESR0) != 0
  1787. && reg == 0)
  1788. return TRUE;
  1789. if ((f & USESAS) && reg == USESAS_REG (insn))
  1790. return TRUE;
  1791. if ((f & USESR8) && reg == 8)
  1792. return TRUE;
  1793. return FALSE;
  1794. }
  1795. /* See whether an instruction sets a general purpose register. */
  1796. static bfd_boolean
  1797. sh_insn_sets_reg (unsigned int insn,
  1798. const struct sh_opcode *op,
  1799. unsigned int reg)
  1800. {
  1801. unsigned int f;
  1802. f = op->flags;
  1803. if ((f & SETS1) != 0
  1804. && SETS1_REG (insn) == reg)
  1805. return TRUE;
  1806. if ((f & SETS2) != 0
  1807. && SETS2_REG (insn) == reg)
  1808. return TRUE;
  1809. if ((f & SETSR0) != 0
  1810. && reg == 0)
  1811. return TRUE;
  1812. if ((f & SETSAS) && reg == SETSAS_REG (insn))
  1813. return TRUE;
  1814. return FALSE;
  1815. }
  1816. /* See whether an instruction uses or sets a general purpose register */
  1817. static bfd_boolean
  1818. sh_insn_uses_or_sets_reg (unsigned int insn,
  1819. const struct sh_opcode *op,
  1820. unsigned int reg)
  1821. {
  1822. if (sh_insn_uses_reg (insn, op, reg))
  1823. return TRUE;
  1824. return sh_insn_sets_reg (insn, op, reg);
  1825. }
  1826. /* See whether an instruction uses a floating point register. */
  1827. static bfd_boolean
  1828. sh_insn_uses_freg (unsigned int insn,
  1829. const struct sh_opcode *op,
  1830. unsigned int freg)
  1831. {
  1832. unsigned int f;
  1833. f = op->flags;
  1834. /* We can't tell if this is a double-precision insn, so just play safe
  1835. and assume that it might be. So not only have we test FREG against
  1836. itself, but also even FREG against FREG+1 - if the using insn uses
  1837. just the low part of a double precision value - but also an odd
  1838. FREG against FREG-1 - if the setting insn sets just the low part
  1839. of a double precision value.
  1840. So what this all boils down to is that we have to ignore the lowest
  1841. bit of the register number. */
  1842. if ((f & USESF1) != 0
  1843. && (USESF1_REG (insn) & 0xe) == (freg & 0xe))
  1844. return TRUE;
  1845. if ((f & USESF2) != 0
  1846. && (USESF2_REG (insn) & 0xe) == (freg & 0xe))
  1847. return TRUE;
  1848. if ((f & USESF0) != 0
  1849. && freg == 0)
  1850. return TRUE;
  1851. return FALSE;
  1852. }
  1853. /* See whether an instruction sets a floating point register. */
  1854. static bfd_boolean
  1855. sh_insn_sets_freg (unsigned int insn,
  1856. const struct sh_opcode *op,
  1857. unsigned int freg)
  1858. {
  1859. unsigned int f;
  1860. f = op->flags;
  1861. /* We can't tell if this is a double-precision insn, so just play safe
  1862. and assume that it might be. So not only have we test FREG against
  1863. itself, but also even FREG against FREG+1 - if the using insn uses
  1864. just the low part of a double precision value - but also an odd
  1865. FREG against FREG-1 - if the setting insn sets just the low part
  1866. of a double precision value.
  1867. So what this all boils down to is that we have to ignore the lowest
  1868. bit of the register number. */
  1869. if ((f & SETSF1) != 0
  1870. && (SETSF1_REG (insn) & 0xe) == (freg & 0xe))
  1871. return TRUE;
  1872. return FALSE;
  1873. }
  1874. /* See whether an instruction uses or sets a floating point register */
  1875. static bfd_boolean
  1876. sh_insn_uses_or_sets_freg (unsigned int insn,
  1877. const struct sh_opcode *op,
  1878. unsigned int reg)
  1879. {
  1880. if (sh_insn_uses_freg (insn, op, reg))
  1881. return TRUE;
  1882. return sh_insn_sets_freg (insn, op, reg);
  1883. }
  1884. /* See whether instructions I1 and I2 conflict, assuming I1 comes
  1885. before I2. OP1 and OP2 are the corresponding sh_opcode structures.
  1886. This should return TRUE if there is a conflict, or FALSE if the
  1887. instructions can be swapped safely. */
  1888. static bfd_boolean
  1889. sh_insns_conflict (unsigned int i1,
  1890. const struct sh_opcode *op1,
  1891. unsigned int i2,
  1892. const struct sh_opcode *op2)
  1893. {
  1894. unsigned int f1, f2;
  1895. f1 = op1->flags;
  1896. f2 = op2->flags;
  1897. /* Load of fpscr conflicts with floating point operations.
  1898. FIXME: shouldn't test raw opcodes here. */
  1899. if (((i1 & 0xf0ff) == 0x4066 && (i2 & 0xf000) == 0xf000)
  1900. || ((i2 & 0xf0ff) == 0x4066 && (i1 & 0xf000) == 0xf000))
  1901. return TRUE;
  1902. if ((f1 & (BRANCH | DELAY)) != 0
  1903. || (f2 & (BRANCH | DELAY)) != 0)
  1904. return TRUE;
  1905. if (((f1 | f2) & SETSSP)
  1906. && (f1 & (SETSSP | USESSP))
  1907. && (f2 & (SETSSP | USESSP)))
  1908. return TRUE;
  1909. if ((f1 & SETS1) != 0
  1910. && sh_insn_uses_or_sets_reg (i2, op2, SETS1_REG (i1)))
  1911. return TRUE;
  1912. if ((f1 & SETS2) != 0
  1913. && sh_insn_uses_or_sets_reg (i2, op2, SETS2_REG (i1)))
  1914. return TRUE;
  1915. if ((f1 & SETSR0) != 0
  1916. && sh_insn_uses_or_sets_reg (i2, op2, 0))
  1917. return TRUE;
  1918. if ((f1 & SETSAS)
  1919. && sh_insn_uses_or_sets_reg (i2, op2, SETSAS_REG (i1)))
  1920. return TRUE;
  1921. if ((f1 & SETSF1) != 0
  1922. && sh_insn_uses_or_sets_freg (i2, op2, SETSF1_REG (i1)))
  1923. return TRUE;
  1924. if ((f2 & SETS1) != 0
  1925. && sh_insn_uses_or_sets_reg (i1, op1, SETS1_REG (i2)))
  1926. return TRUE;
  1927. if ((f2 & SETS2) != 0
  1928. && sh_insn_uses_or_sets_reg (i1, op1, SETS2_REG (i2)))
  1929. return TRUE;
  1930. if ((f2 & SETSR0) != 0
  1931. && sh_insn_uses_or_sets_reg (i1, op1, 0))
  1932. return TRUE;
  1933. if ((f2 & SETSAS)
  1934. && sh_insn_uses_or_sets_reg (i1, op1, SETSAS_REG (i2)))
  1935. return TRUE;
  1936. if ((f2 & SETSF1) != 0
  1937. && sh_insn_uses_or_sets_freg (i1, op1, SETSF1_REG (i2)))
  1938. return TRUE;
  1939. /* The instructions do not conflict. */
  1940. return FALSE;
  1941. }
  1942. /* I1 is a load instruction, and I2 is some other instruction. Return
  1943. TRUE if I1 loads a register which I2 uses. */
  1944. static bfd_boolean
  1945. sh_load_use (unsigned int i1,
  1946. const struct sh_opcode *op1,
  1947. unsigned int i2,
  1948. const struct sh_opcode *op2)
  1949. {
  1950. unsigned int f1;
  1951. f1 = op1->flags;
  1952. if ((f1 & LOAD) == 0)
  1953. return FALSE;
  1954. /* If both SETS1 and SETSSP are set, that means a load to a special
  1955. register using postincrement addressing mode, which we don't care
  1956. about here. */
  1957. if ((f1 & SETS1) != 0
  1958. && (f1 & SETSSP) == 0
  1959. && sh_insn_uses_reg (i2, op2, (i1 & 0x0f00) >> 8))
  1960. return TRUE;
  1961. if ((f1 & SETSR0) != 0
  1962. && sh_insn_uses_reg (i2, op2, 0))
  1963. return TRUE;
  1964. if ((f1 & SETSF1) != 0
  1965. && sh_insn_uses_freg (i2, op2, (i1 & 0x0f00) >> 8))
  1966. return TRUE;
  1967. return FALSE;
  1968. }
  1969. /* Try to align loads and stores within a span of memory. This is
  1970. called by both the ELF and the COFF sh targets. ABFD and SEC are
  1971. the BFD and section we are examining. CONTENTS is the contents of
  1972. the section. SWAP is the routine to call to swap two instructions.
  1973. RELOCS is a pointer to the internal relocation information, to be
  1974. passed to SWAP. PLABEL is a pointer to the current label in a
  1975. sorted list of labels; LABEL_END is the end of the list. START and
  1976. STOP are the range of memory to examine. If a swap is made,
  1977. *PSWAPPED is set to TRUE. */
  1978. #ifdef COFF_WITH_PE
  1979. static
  1980. #endif
  1981. bfd_boolean
  1982. _bfd_sh_align_load_span (bfd *abfd,
  1983. asection *sec,
  1984. bfd_byte *contents,
  1985. bfd_boolean (*swap) (bfd *, asection *, void *, bfd_byte *, bfd_vma),
  1986. void * relocs,
  1987. bfd_vma **plabel,
  1988. bfd_vma *label_end,
  1989. bfd_vma start,
  1990. bfd_vma stop,
  1991. bfd_boolean *pswapped)
  1992. {
  1993. int dsp = (abfd->arch_info->mach == bfd_mach_sh_dsp
  1994. || abfd->arch_info->mach == bfd_mach_sh3_dsp);
  1995. bfd_vma i;
  1996. /* The SH4 has a Harvard architecture, hence aligning loads is not
  1997. desirable. In fact, it is counter-productive, since it interferes
  1998. with the schedules generated by the compiler. */
  1999. if (abfd->arch_info->mach == bfd_mach_sh4)
  2000. return TRUE;
  2001. /* If we are linking sh[3]-dsp code, swap the FPU instructions for DSP
  2002. instructions. */
  2003. if (dsp)
  2004. {
  2005. sh_opcodes[0xf].minor_opcodes = sh_dsp_opcodef;
  2006. sh_opcodes[0xf].count = sizeof sh_dsp_opcodef / sizeof sh_dsp_opcodef [0];
  2007. }
  2008. /* Instructions should be aligned on 2 byte boundaries. */
  2009. if ((start & 1) == 1)
  2010. ++start;
  2011. /* Now look through the unaligned addresses. */
  2012. i = start;
  2013. if ((i & 2) == 0)
  2014. i += 2;
  2015. for (; i < stop; i += 4)
  2016. {
  2017. unsigned int insn;
  2018. const struct sh_opcode *op;
  2019. unsigned int prev_insn = 0;
  2020. const struct sh_opcode *prev_op = NULL;
  2021. insn = bfd_get_16 (abfd, contents + i);
  2022. op = sh_insn_info (insn);
  2023. if (op == NULL
  2024. || (op->flags & (LOAD | STORE)) == 0)
  2025. continue;
  2026. /* This is a load or store which is not on a four byte boundary. */
  2027. while (*plabel < label_end && **plabel < i)
  2028. ++*plabel;
  2029. if (i > start)
  2030. {
  2031. prev_insn = bfd_get_16 (abfd, contents + i - 2);
  2032. /* If INSN is the field b of a parallel processing insn, it is not
  2033. a load / store after all. Note that the test here might mistake
  2034. the field_b of a pcopy insn for the starting code of a parallel
  2035. processing insn; this might miss a swapping opportunity, but at
  2036. least we're on the safe side. */
  2037. if (dsp && (prev_insn & 0xfc00) == 0xf800)
  2038. continue;
  2039. /* Check if prev_insn is actually the field b of a parallel
  2040. processing insn. Again, this can give a spurious match
  2041. after a pcopy. */
  2042. if (dsp && i - 2 > start)
  2043. {
  2044. unsigned pprev_insn = bfd_get_16 (abfd, contents + i - 4);
  2045. if ((pprev_insn & 0xfc00) == 0xf800)
  2046. prev_op = NULL;
  2047. else
  2048. prev_op = sh_insn_info (prev_insn);
  2049. }
  2050. else
  2051. prev_op = sh_insn_info (prev_insn);
  2052. /* If the load/store instruction is in a delay slot, we
  2053. can't swap. */
  2054. if (prev_op == NULL
  2055. || (prev_op->flags & DELAY) != 0)
  2056. continue;
  2057. }
  2058. if (i > start
  2059. && (*plabel >= label_end || **plabel != i)
  2060. && prev_op != NULL
  2061. && (prev_op->flags & (LOAD | STORE)) == 0
  2062. && ! sh_insns_conflict (prev_insn, prev_op, insn, op))
  2063. {
  2064. bfd_boolean ok;
  2065. /* The load/store instruction does not have a label, and
  2066. there is a previous instruction; PREV_INSN is not
  2067. itself a load/store instruction, and PREV_INSN and
  2068. INSN do not conflict. */
  2069. ok = TRUE;
  2070. if (i >= start + 4)
  2071. {
  2072. unsigned int prev2_insn;
  2073. const struct sh_opcode *prev2_op;
  2074. prev2_insn = bfd_get_16 (abfd, contents + i - 4);
  2075. prev2_op = sh_insn_info (prev2_insn);
  2076. /* If the instruction before PREV_INSN has a delay
  2077. slot--that is, PREV_INSN is in a delay slot--we
  2078. can not swap. */
  2079. if (prev2_op == NULL
  2080. || (prev2_op->flags & DELAY) != 0)
  2081. ok = FALSE;
  2082. /* If the instruction before PREV_INSN is a load,
  2083. and it sets a register which INSN uses, then
  2084. putting INSN immediately after PREV_INSN will
  2085. cause a pipeline bubble, so there is no point to
  2086. making the swap. */
  2087. if (ok
  2088. && (prev2_op->flags & LOAD) != 0
  2089. && sh_load_use (prev2_insn, prev2_op, insn, op))
  2090. ok = FALSE;
  2091. }
  2092. if (ok)
  2093. {
  2094. if (! (*swap) (abfd, sec, relocs, contents, i - 2))
  2095. return FALSE;
  2096. *pswapped = TRUE;
  2097. continue;
  2098. }
  2099. }
  2100. while (*plabel < label_end && **plabel < i + 2)
  2101. ++*plabel;
  2102. if (i + 2 < stop
  2103. && (*plabel >= label_end || **plabel != i + 2))
  2104. {
  2105. unsigned int next_insn;
  2106. const struct sh_opcode *next_op;
  2107. /* There is an instruction after the load/store
  2108. instruction, and it does not have a label. */
  2109. next_insn = bfd_get_16 (abfd, contents + i + 2);
  2110. next_op = sh_insn_info (next_insn);
  2111. if (next_op != NULL
  2112. && (next_op->flags & (LOAD | STORE)) == 0
  2113. && ! sh_insns_conflict (insn, op, next_insn, next_op))
  2114. {
  2115. bfd_boolean ok;
  2116. /* NEXT_INSN is not itself a load/store instruction,
  2117. and it does not conflict with INSN. */
  2118. ok = TRUE;
  2119. /* If PREV_INSN is a load, and it sets a register
  2120. which NEXT_INSN uses, then putting NEXT_INSN
  2121. immediately after PREV_INSN will cause a pipeline
  2122. bubble, so there is no reason to make this swap. */
  2123. if (prev_op != NULL
  2124. && (prev_op->flags & LOAD) != 0
  2125. && sh_load_use (prev_insn, prev_op, next_insn, next_op))
  2126. ok = FALSE;
  2127. /* If INSN is a load, and it sets a register which
  2128. the insn after NEXT_INSN uses, then doing the
  2129. swap will cause a pipeline bubble, so there is no
  2130. reason to make the swap. However, if the insn
  2131. after NEXT_INSN is itself a load or store
  2132. instruction, then it is misaligned, so
  2133. optimistically hope that it will be swapped
  2134. itself, and just live with the pipeline bubble if
  2135. it isn't. */
  2136. if (ok
  2137. && i + 4 < stop
  2138. && (op->flags & LOAD) != 0)
  2139. {
  2140. unsigned int next2_insn;
  2141. const struct sh_opcode *next2_op;
  2142. next2_insn = bfd_get_16 (abfd, contents + i + 4);
  2143. next2_op = sh_insn_info (next2_insn);
  2144. if (next2_op == NULL
  2145. || ((next2_op->flags & (LOAD | STORE)) == 0
  2146. && sh_load_use (insn, op, next2_insn, next2_op)))
  2147. ok = FALSE;
  2148. }
  2149. if (ok)
  2150. {
  2151. if (! (*swap) (abfd, sec, relocs, contents, i))
  2152. return FALSE;
  2153. *pswapped = TRUE;
  2154. continue;
  2155. }
  2156. }
  2157. }
  2158. }
  2159. return TRUE;
  2160. }
  2161. #endif /* not COFF_IMAGE_WITH_PE */
  2162. /* Swap two SH instructions. */
  2163. static bfd_boolean
  2164. sh_swap_insns (bfd * abfd,
  2165. asection * sec,
  2166. void * relocs,
  2167. bfd_byte * contents,
  2168. bfd_vma addr)
  2169. {
  2170. struct internal_reloc *internal_relocs = (struct internal_reloc *) relocs;
  2171. unsigned short i1, i2;
  2172. struct internal_reloc *irel, *irelend;
  2173. /* Swap the instructions themselves. */
  2174. i1 = bfd_get_16 (abfd, contents + addr);
  2175. i2 = bfd_get_16 (abfd, contents + addr + 2);
  2176. bfd_put_16 (abfd, (bfd_vma) i2, contents + addr);
  2177. bfd_put_16 (abfd, (bfd_vma) i1, contents + addr + 2);
  2178. /* Adjust all reloc addresses. */
  2179. irelend = internal_relocs + sec->reloc_count;
  2180. for (irel = internal_relocs; irel < irelend; irel++)
  2181. {
  2182. int type, add;
  2183. /* There are a few special types of relocs that we don't want to
  2184. adjust. These relocs do not apply to the instruction itself,
  2185. but are only associated with the address. */
  2186. type = irel->r_type;
  2187. if (type == R_SH_ALIGN
  2188. || type == R_SH_CODE
  2189. || type == R_SH_DATA
  2190. || type == R_SH_LABEL)
  2191. continue;
  2192. /* If an R_SH_USES reloc points to one of the addresses being
  2193. swapped, we must adjust it. It would be incorrect to do this
  2194. for a jump, though, since we want to execute both
  2195. instructions after the jump. (We have avoided swapping
  2196. around a label, so the jump will not wind up executing an
  2197. instruction it shouldn't). */
  2198. if (type == R_SH_USES)
  2199. {
  2200. bfd_vma off;
  2201. off = irel->r_vaddr - sec->vma + 4 + irel->r_offset;
  2202. if (off == addr)
  2203. irel->r_offset += 2;
  2204. else if (off == addr + 2)
  2205. irel->r_offset -= 2;
  2206. }
  2207. if (irel->r_vaddr - sec->vma == addr)
  2208. {
  2209. irel->r_vaddr += 2;
  2210. add = -2;
  2211. }
  2212. else if (irel->r_vaddr - sec->vma == addr + 2)
  2213. {
  2214. irel->r_vaddr -= 2;
  2215. add = 2;
  2216. }
  2217. else
  2218. add = 0;
  2219. if (add != 0)
  2220. {
  2221. bfd_byte *loc;
  2222. unsigned short insn, oinsn;
  2223. bfd_boolean overflow;
  2224. loc = contents + irel->r_vaddr - sec->vma;
  2225. overflow = FALSE;
  2226. switch (type)
  2227. {
  2228. default:
  2229. break;
  2230. case R_SH_PCDISP8BY2:
  2231. case R_SH_PCRELIMM8BY2:
  2232. insn = bfd_get_16 (abfd, loc);
  2233. oinsn = insn;
  2234. insn += add / 2;
  2235. if ((oinsn & 0xff00) != (insn & 0xff00))
  2236. overflow = TRUE;
  2237. bfd_put_16 (abfd, (bfd_vma) insn, loc);
  2238. break;
  2239. case R_SH_PCDISP:
  2240. insn = bfd_get_16 (abfd, loc);
  2241. oinsn = insn;
  2242. insn += add / 2;
  2243. if ((oinsn & 0xf000) != (insn & 0xf000))
  2244. overflow = TRUE;
  2245. bfd_put_16 (abfd, (bfd_vma) insn, loc);
  2246. break;
  2247. case R_SH_PCRELIMM8BY4:
  2248. /* This reloc ignores the least significant 3 bits of
  2249. the program counter before adding in the offset.
  2250. This means that if ADDR is at an even address, the
  2251. swap will not affect the offset. If ADDR is an at an
  2252. odd address, then the instruction will be crossing a
  2253. four byte boundary, and must be adjusted. */
  2254. if ((addr & 3) != 0)
  2255. {
  2256. insn = bfd_get_16 (abfd, loc);
  2257. oinsn = insn;
  2258. insn += add / 2;
  2259. if ((oinsn & 0xff00) != (insn & 0xff00))
  2260. overflow = TRUE;
  2261. bfd_put_16 (abfd, (bfd_vma) insn, loc);
  2262. }
  2263. break;
  2264. }
  2265. if (overflow)
  2266. {
  2267. ((*_bfd_error_handler)
  2268. ("%B: 0x%lx: fatal: reloc overflow while relaxing",
  2269. abfd, (unsigned long) irel->r_vaddr));
  2270. bfd_set_error (bfd_error_bad_value);
  2271. return FALSE;
  2272. }
  2273. }
  2274. }
  2275. return TRUE;
  2276. }
  2277. /* Look for loads and stores which we can align to four byte
  2278. boundaries. See the longer comment above sh_relax_section for why
  2279. this is desirable. This sets *PSWAPPED if some instruction was
  2280. swapped. */
  2281. static bfd_boolean
  2282. sh_align_loads (bfd *abfd,
  2283. asection *sec,
  2284. struct internal_reloc *internal_relocs,
  2285. bfd_byte *contents,
  2286. bfd_boolean *pswapped)
  2287. {
  2288. struct internal_reloc *irel, *irelend;
  2289. bfd_vma *labels = NULL;
  2290. bfd_vma *label, *label_end;
  2291. bfd_size_type amt;
  2292. *pswapped = FALSE;
  2293. irelend = internal_relocs + sec->reloc_count;
  2294. /* Get all the addresses with labels on them. */
  2295. amt = (bfd_size_type) sec->reloc_count * sizeof (bfd_vma);
  2296. labels = (bfd_vma *) bfd_malloc (amt);
  2297. if (labels == NULL)
  2298. goto error_return;
  2299. label_end = labels;
  2300. for (irel = internal_relocs; irel < irelend; irel++)
  2301. {
  2302. if (irel->r_type == R_SH_LABEL)
  2303. {
  2304. *label_end = irel->r_vaddr - sec->vma;
  2305. ++label_end;
  2306. }
  2307. }
  2308. /* Note that the assembler currently always outputs relocs in
  2309. address order. If that ever changes, this code will need to sort
  2310. the label values and the relocs. */
  2311. label = labels;
  2312. for (irel = internal_relocs; irel < irelend; irel++)
  2313. {
  2314. bfd_vma start, stop;
  2315. if (irel->r_type != R_SH_CODE)
  2316. continue;
  2317. start = irel->r_vaddr - sec->vma;
  2318. for (irel++; irel < irelend; irel++)
  2319. if (irel->r_type == R_SH_DATA)
  2320. break;
  2321. if (irel < irelend)
  2322. stop = irel->r_vaddr - sec->vma;
  2323. else
  2324. stop = sec->size;
  2325. if (! _bfd_sh_align_load_span (abfd, sec, contents, sh_swap_insns,
  2326. internal_relocs, &label,
  2327. label_end, start, stop, pswapped))
  2328. goto error_return;
  2329. }
  2330. free (labels);
  2331. return TRUE;
  2332. error_return:
  2333. if (labels != NULL)
  2334. free (labels);
  2335. return FALSE;
  2336. }
  2337. /* This is a modification of _bfd_coff_generic_relocate_section, which
  2338. will handle SH relaxing. */
  2339. static bfd_boolean
  2340. sh_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
  2341. struct bfd_link_info *info,
  2342. bfd *input_bfd,
  2343. asection *input_section,
  2344. bfd_byte *contents,
  2345. struct internal_reloc *relocs,
  2346. struct internal_syment *syms,
  2347. asection **sections)
  2348. {
  2349. struct internal_reloc *rel;
  2350. struct internal_reloc *relend;
  2351. rel = relocs;
  2352. relend = rel + input_section->reloc_count;
  2353. for (; rel < relend; rel++)
  2354. {
  2355. long symndx;
  2356. struct coff_link_hash_entry *h;
  2357. struct internal_syment *sym;
  2358. bfd_vma addend;
  2359. bfd_vma val;
  2360. reloc_howto_type *howto;
  2361. bfd_reloc_status_type rstat;
  2362. /* Almost all relocs have to do with relaxing. If any work must
  2363. be done for them, it has been done in sh_relax_section. */
  2364. if (rel->r_type != R_SH_IMM32
  2365. #ifdef COFF_WITH_PE
  2366. && rel->r_type != R_SH_IMM32CE
  2367. && rel->r_type != R_SH_IMAGEBASE
  2368. #endif
  2369. && rel->r_type != R_SH_PCDISP)
  2370. continue;
  2371. symndx = rel->r_symndx;
  2372. if (symndx == -1)
  2373. {
  2374. h = NULL;
  2375. sym = NULL;
  2376. }
  2377. else
  2378. {
  2379. if (symndx < 0
  2380. || (unsigned long) symndx >= obj_raw_syment_count (input_bfd))
  2381. {
  2382. (*_bfd_error_handler)
  2383. ("%B: illegal symbol index %ld in relocs",
  2384. input_bfd, symndx);
  2385. bfd_set_error (bfd_error_bad_value);
  2386. return FALSE;
  2387. }
  2388. h = obj_coff_sym_hashes (input_bfd)[symndx];
  2389. sym = syms + symndx;
  2390. }
  2391. if (sym != NULL && sym->n_scnum != 0)
  2392. addend = - sym->n_value;
  2393. else
  2394. addend = 0;
  2395. if (rel->r_type == R_SH_PCDISP)
  2396. addend -= 4;
  2397. if (rel->r_type >= SH_COFF_HOWTO_COUNT)
  2398. howto = NULL;
  2399. else
  2400. howto = &sh_coff_howtos[rel->r_type];
  2401. if (howto == NULL)
  2402. {
  2403. bfd_set_error (bfd_error_bad_value);
  2404. return FALSE;
  2405. }
  2406. #ifdef COFF_WITH_PE
  2407. if (rel->r_type == R_SH_IMAGEBASE)
  2408. addend -= pe_data (input_section->output_section->owner)->pe_opthdr.ImageBase;
  2409. #endif
  2410. val = 0;
  2411. if (h == NULL)
  2412. {
  2413. asection *sec;
  2414. /* There is nothing to do for an internal PCDISP reloc. */
  2415. if (rel->r_type == R_SH_PCDISP)
  2416. continue;
  2417. if (symndx == -1)
  2418. {
  2419. sec = bfd_abs_section_ptr;
  2420. val = 0;
  2421. }
  2422. else
  2423. {
  2424. sec = sections[symndx];
  2425. val = (sec->output_section->vma
  2426. + sec->output_offset
  2427. + sym->n_value
  2428. - sec->vma);
  2429. }
  2430. }
  2431. else
  2432. {
  2433. if (h->root.type == bfd_link_hash_defined
  2434. || h->root.type == bfd_link_hash_defweak)
  2435. {
  2436. asection *sec;
  2437. sec = h->root.u.def.section;
  2438. val = (h->root.u.def.value
  2439. + sec->output_section->vma
  2440. + sec->output_offset);
  2441. }
  2442. else if (! bfd_link_relocatable (info))
  2443. {
  2444. if (! ((*info->callbacks->undefined_symbol)
  2445. (info, h->root.root.string, input_bfd, input_section,
  2446. rel->r_vaddr - input_section->vma, TRUE)))
  2447. return FALSE;
  2448. }
  2449. }
  2450. rstat = _bfd_final_link_relocate (howto, input_bfd, input_section,
  2451. contents,
  2452. rel->r_vaddr - input_section->vma,
  2453. val, addend);
  2454. switch (rstat)
  2455. {
  2456. default:
  2457. abort ();
  2458. case bfd_reloc_ok:
  2459. break;
  2460. case bfd_reloc_overflow:
  2461. {
  2462. const char *name;
  2463. char buf[SYMNMLEN + 1];
  2464. if (symndx == -1)
  2465. name = "*ABS*";
  2466. else if (h != NULL)
  2467. name = NULL;
  2468. else if (sym->_n._n_n._n_zeroes == 0
  2469. && sym->_n._n_n._n_offset != 0)
  2470. name = obj_coff_strings (input_bfd) + sym->_n._n_n._n_offset;
  2471. else
  2472. {
  2473. strncpy (buf, sym->_n._n_name, SYMNMLEN);
  2474. buf[SYMNMLEN] = '\0';
  2475. name = buf;
  2476. }
  2477. if (! ((*info->callbacks->reloc_overflow)
  2478. (info, (h ? &h->root : NULL), name, howto->name,
  2479. (bfd_vma) 0, input_bfd, input_section,
  2480. rel->r_vaddr - input_section->vma)))
  2481. return FALSE;
  2482. }
  2483. }
  2484. }
  2485. return TRUE;
  2486. }
  2487. /* This is a version of bfd_generic_get_relocated_section_contents
  2488. which uses sh_relocate_section. */
  2489. static bfd_byte *
  2490. sh_coff_get_relocated_section_contents (bfd *output_bfd,
  2491. struct bfd_link_info *link_info,
  2492. struct bfd_link_order *link_order,
  2493. bfd_byte *data,
  2494. bfd_boolean relocatable,
  2495. asymbol **symbols)
  2496. {
  2497. asection *input_section = link_order->u.indirect.section;
  2498. bfd *input_bfd = input_section->owner;
  2499. asection **sections = NULL;
  2500. struct internal_reloc *internal_relocs = NULL;
  2501. struct internal_syment *internal_syms = NULL;
  2502. /* We only need to handle the case of relaxing, or of having a
  2503. particular set of section contents, specially. */
  2504. if (relocatable
  2505. || coff_section_data (input_bfd, input_section) == NULL
  2506. || coff_section_data (input_bfd, input_section)->contents == NULL)
  2507. return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
  2508. link_order, data,
  2509. relocatable,
  2510. symbols);
  2511. memcpy (data, coff_section_data (input_bfd, input_section)->contents,
  2512. (size_t) input_section->size);
  2513. if ((input_section->flags & SEC_RELOC) != 0
  2514. && input_section->reloc_count > 0)
  2515. {
  2516. bfd_size_type symesz = bfd_coff_symesz (input_bfd);
  2517. bfd_byte *esym, *esymend;
  2518. struct internal_syment *isymp;
  2519. asection **secpp;
  2520. bfd_size_type amt;
  2521. if (! _bfd_coff_get_external_symbols (input_bfd))
  2522. goto error_return;
  2523. internal_relocs = (_bfd_coff_read_internal_relocs
  2524. (input_bfd, input_section, FALSE, (bfd_byte *) NULL,
  2525. FALSE, (struct internal_reloc *) NULL));
  2526. if (internal_relocs == NULL)
  2527. goto error_return;
  2528. amt = obj_raw_syment_count (input_bfd);
  2529. amt *= sizeof (struct internal_syment);
  2530. internal_syms = (struct internal_syment *) bfd_malloc (amt);
  2531. if (internal_syms == NULL)
  2532. goto error_return;
  2533. amt = obj_raw_syment_count (input_bfd);
  2534. amt *= sizeof (asection *);
  2535. sections = (asection **) bfd_malloc (amt);
  2536. if (sections == NULL)
  2537. goto error_return;
  2538. isymp = internal_syms;
  2539. secpp = sections;
  2540. esym = (bfd_byte *) obj_coff_external_syms (input_bfd);
  2541. esymend = esym + obj_raw_syment_count (input_bfd) * symesz;
  2542. while (esym < esymend)
  2543. {
  2544. bfd_coff_swap_sym_in (input_bfd, esym, isymp);
  2545. if (isymp->n_scnum != 0)
  2546. *secpp = coff_section_from_bfd_index (input_bfd, isymp->n_scnum);
  2547. else
  2548. {
  2549. if (isymp->n_value == 0)
  2550. *secpp = bfd_und_section_ptr;
  2551. else
  2552. *secpp = bfd_com_section_ptr;
  2553. }
  2554. esym += (isymp->n_numaux + 1) * symesz;
  2555. secpp += isymp->n_numaux + 1;
  2556. isymp += isymp->n_numaux + 1;
  2557. }
  2558. if (! sh_relocate_section (output_bfd, link_info, input_bfd,
  2559. input_section, data, internal_relocs,
  2560. internal_syms, sections))
  2561. goto error_return;
  2562. free (sections);
  2563. sections = NULL;
  2564. free (internal_syms);
  2565. internal_syms = NULL;
  2566. free (internal_relocs);
  2567. internal_relocs = NULL;
  2568. }
  2569. return data;
  2570. error_return:
  2571. if (internal_relocs != NULL)
  2572. free (internal_relocs);
  2573. if (internal_syms != NULL)
  2574. free (internal_syms);
  2575. if (sections != NULL)
  2576. free (sections);
  2577. return NULL;
  2578. }
  2579. /* The target vectors. */
  2580. #ifndef TARGET_SHL_SYM
  2581. CREATE_BIG_COFF_TARGET_VEC (sh_coff_vec, "coff-sh", BFD_IS_RELAXABLE, 0, '_', NULL, COFF_SWAP_TABLE)
  2582. #endif
  2583. #ifdef TARGET_SHL_SYM
  2584. #define TARGET_SYM TARGET_SHL_SYM
  2585. #else
  2586. #define TARGET_SYM sh_coff_le_vec
  2587. #endif
  2588. #ifndef TARGET_SHL_NAME
  2589. #define TARGET_SHL_NAME "coff-shl"
  2590. #endif
  2591. #ifdef COFF_WITH_PE
  2592. CREATE_LITTLE_COFF_TARGET_VEC (TARGET_SYM, TARGET_SHL_NAME, BFD_IS_RELAXABLE,
  2593. SEC_CODE | SEC_DATA, '_', NULL, COFF_SWAP_TABLE);
  2594. #else
  2595. CREATE_LITTLE_COFF_TARGET_VEC (TARGET_SYM, TARGET_SHL_NAME, BFD_IS_RELAXABLE,
  2596. 0, '_', NULL, COFF_SWAP_TABLE)
  2597. #endif
  2598. #ifndef TARGET_SHL_SYM
  2599. /* Some people want versions of the SH COFF target which do not align
  2600. to 16 byte boundaries. We implement that by adding a couple of new
  2601. target vectors. These are just like the ones above, but they
  2602. change the default section alignment. To generate them in the
  2603. assembler, use -small. To use them in the linker, use -b
  2604. coff-sh{l}-small and -oformat coff-sh{l}-small.
  2605. Yes, this is a horrible hack. A general solution for setting
  2606. section alignment in COFF is rather complex. ELF handles this
  2607. correctly. */
  2608. /* Only recognize the small versions if the target was not defaulted.
  2609. Otherwise we won't recognize the non default endianness. */
  2610. static const bfd_target *
  2611. coff_small_object_p (bfd *abfd)
  2612. {
  2613. if (abfd->target_defaulted)
  2614. {
  2615. bfd_set_error (bfd_error_wrong_format);
  2616. return NULL;
  2617. }
  2618. return coff_object_p (abfd);
  2619. }
  2620. /* Set the section alignment for the small versions. */
  2621. static bfd_boolean
  2622. coff_small_new_section_hook (bfd *abfd, asection *section)
  2623. {
  2624. if (! coff_new_section_hook (abfd, section))
  2625. return FALSE;
  2626. /* We must align to at least a four byte boundary, because longword
  2627. accesses must be on a four byte boundary. */
  2628. if (section->alignment_power == COFF_DEFAULT_SECTION_ALIGNMENT_POWER)
  2629. section->alignment_power = 2;
  2630. return TRUE;
  2631. }
  2632. /* This is copied from bfd_coff_std_swap_table so that we can change
  2633. the default section alignment power. */
  2634. static bfd_coff_backend_data bfd_coff_small_swap_table =
  2635. {
  2636. coff_swap_aux_in, coff_swap_sym_in, coff_swap_lineno_in,
  2637. coff_swap_aux_out, coff_swap_sym_out,
  2638. coff_swap_lineno_out, coff_swap_reloc_out,
  2639. coff_swap_filehdr_out, coff_swap_aouthdr_out,
  2640. coff_swap_scnhdr_out,
  2641. FILHSZ, AOUTSZ, SCNHSZ, SYMESZ, AUXESZ, RELSZ, LINESZ, FILNMLEN,
  2642. #ifdef COFF_LONG_FILENAMES
  2643. TRUE,
  2644. #else
  2645. FALSE,
  2646. #endif
  2647. COFF_DEFAULT_LONG_SECTION_NAMES,
  2648. 2,
  2649. #ifdef COFF_FORCE_SYMBOLS_IN_STRINGS
  2650. TRUE,
  2651. #else
  2652. FALSE,
  2653. #endif
  2654. #ifdef COFF_DEBUG_STRING_WIDE_PREFIX
  2655. 4,
  2656. #else
  2657. 2,
  2658. #endif
  2659. 32768,
  2660. coff_swap_filehdr_in, coff_swap_aouthdr_in, coff_swap_scnhdr_in,
  2661. coff_swap_reloc_in, coff_bad_format_hook, coff_set_arch_mach_hook,
  2662. coff_mkobject_hook, styp_to_sec_flags, coff_set_alignment_hook,
  2663. coff_slurp_symbol_table, symname_in_debug_hook, coff_pointerize_aux_hook,
  2664. coff_print_aux, coff_reloc16_extra_cases, coff_reloc16_estimate,
  2665. coff_classify_symbol, coff_compute_section_file_positions,
  2666. coff_start_final_link, coff_relocate_section, coff_rtype_to_howto,
  2667. coff_adjust_symndx, coff_link_add_one_symbol,
  2668. coff_link_output_has_begun, coff_final_link_postscript,
  2669. bfd_pe_print_pdata
  2670. };
  2671. #define coff_small_close_and_cleanup \
  2672. coff_close_and_cleanup
  2673. #define coff_small_bfd_free_cached_info \
  2674. coff_bfd_free_cached_info
  2675. #define coff_small_get_section_contents \
  2676. coff_get_section_contents
  2677. #define coff_small_get_section_contents_in_window \
  2678. coff_get_section_contents_in_window
  2679. extern const bfd_target sh_coff_small_le_vec;
  2680. const bfd_target sh_coff_small_vec =
  2681. {
  2682. "coff-sh-small", /* name */
  2683. bfd_target_coff_flavour,
  2684. BFD_ENDIAN_BIG, /* data byte order is big */
  2685. BFD_ENDIAN_BIG, /* header byte order is big */
  2686. (HAS_RELOC | EXEC_P | /* object flags */
  2687. HAS_LINENO | HAS_DEBUG |
  2688. HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE),
  2689. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),
  2690. '_', /* leading symbol underscore */
  2691. '/', /* ar_pad_char */
  2692. 15, /* ar_max_namelen */
  2693. 0, /* match priority. */
  2694. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  2695. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  2696. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* data */
  2697. bfd_getb64, bfd_getb_signed_64, bfd_putb64,
  2698. bfd_getb32, bfd_getb_signed_32, bfd_putb32,
  2699. bfd_getb16, bfd_getb_signed_16, bfd_putb16, /* hdrs */
  2700. {_bfd_dummy_target, coff_small_object_p, /* bfd_check_format */
  2701. bfd_generic_archive_p, _bfd_dummy_target},
  2702. {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
  2703. bfd_false},
  2704. {bfd_false, coff_write_object_contents, /* bfd_write_contents */
  2705. _bfd_write_archive_contents, bfd_false},
  2706. BFD_JUMP_TABLE_GENERIC (coff_small),
  2707. BFD_JUMP_TABLE_COPY (coff),
  2708. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  2709. BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
  2710. BFD_JUMP_TABLE_SYMBOLS (coff),
  2711. BFD_JUMP_TABLE_RELOCS (coff),
  2712. BFD_JUMP_TABLE_WRITE (coff),
  2713. BFD_JUMP_TABLE_LINK (coff),
  2714. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  2715. & sh_coff_small_le_vec,
  2716. & bfd_coff_small_swap_table
  2717. };
  2718. const bfd_target sh_coff_small_le_vec =
  2719. {
  2720. "coff-shl-small", /* name */
  2721. bfd_target_coff_flavour,
  2722. BFD_ENDIAN_LITTLE, /* data byte order is little */
  2723. BFD_ENDIAN_LITTLE, /* header byte order is little endian too*/
  2724. (HAS_RELOC | EXEC_P | /* object flags */
  2725. HAS_LINENO | HAS_DEBUG |
  2726. HAS_SYMS | HAS_LOCALS | WP_TEXT | BFD_IS_RELAXABLE),
  2727. (SEC_HAS_CONTENTS | SEC_ALLOC | SEC_LOAD | SEC_RELOC),
  2728. '_', /* leading symbol underscore */
  2729. '/', /* ar_pad_char */
  2730. 15, /* ar_max_namelen */
  2731. 0, /* match priority. */
  2732. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  2733. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  2734. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* data */
  2735. bfd_getl64, bfd_getl_signed_64, bfd_putl64,
  2736. bfd_getl32, bfd_getl_signed_32, bfd_putl32,
  2737. bfd_getl16, bfd_getl_signed_16, bfd_putl16, /* hdrs */
  2738. {_bfd_dummy_target, coff_small_object_p, /* bfd_check_format */
  2739. bfd_generic_archive_p, _bfd_dummy_target},
  2740. {bfd_false, coff_mkobject, _bfd_generic_mkarchive, /* bfd_set_format */
  2741. bfd_false},
  2742. {bfd_false, coff_write_object_contents, /* bfd_write_contents */
  2743. _bfd_write_archive_contents, bfd_false},
  2744. BFD_JUMP_TABLE_GENERIC (coff_small),
  2745. BFD_JUMP_TABLE_COPY (coff),
  2746. BFD_JUMP_TABLE_CORE (_bfd_nocore),
  2747. BFD_JUMP_TABLE_ARCHIVE (_bfd_archive_coff),
  2748. BFD_JUMP_TABLE_SYMBOLS (coff),
  2749. BFD_JUMP_TABLE_RELOCS (coff),
  2750. BFD_JUMP_TABLE_WRITE (coff),
  2751. BFD_JUMP_TABLE_LINK (coff),
  2752. BFD_JUMP_TABLE_DYNAMIC (_bfd_nodynamic),
  2753. & sh_coff_small_vec,
  2754. & bfd_coff_small_swap_table
  2755. };
  2756. #endif