elf32-msp430.c 92 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994
  1. /* MSP430-specific support for 32-bit ELF
  2. Copyright (C) 2002-2022 Free Software Foundation, Inc.
  3. Contributed by Dmitry Diky <diwil@mail.ru>
  4. This file is part of BFD, the Binary File Descriptor library.
  5. This program is free software; you can redistribute it and/or modify
  6. it under the terms of the GNU General Public License as published by
  7. the Free Software Foundation; either version 3 of the License, or
  8. (at your option) any later version.
  9. This program is distributed in the hope that it will be useful,
  10. but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. GNU General Public License for more details.
  13. You should have received a copy of the GNU General Public License
  14. along with this program; if not, write to the Free Software
  15. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston,
  16. MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libiberty.h"
  20. #include "libbfd.h"
  21. #include "elf-bfd.h"
  22. #include "elf/msp430.h"
  23. static bool debug_relocs = 0;
  24. /* All users of this file have bfd_octets_per_byte (abfd, sec) == 1. */
  25. #define OCTETS_PER_BYTE(ABFD, SEC) 1
  26. static bfd_reloc_status_type
  27. rl78_sym_diff_handler (bfd * abfd,
  28. arelent * reloc,
  29. asymbol * sym ATTRIBUTE_UNUSED,
  30. void * addr ATTRIBUTE_UNUSED,
  31. asection * input_sec,
  32. bfd * out_bfd ATTRIBUTE_UNUSED,
  33. char ** error_message ATTRIBUTE_UNUSED)
  34. {
  35. bfd_size_type octets;
  36. octets = reloc->address * OCTETS_PER_BYTE (abfd, input_sec);
  37. /* Catch the case where bfd_install_relocation would return
  38. bfd_reloc_outofrange because the SYM_DIFF reloc is being used in a very
  39. small section. It does not actually matter if this happens because all
  40. that SYM_DIFF does is compute a (4-byte) value. A second reloc then uses
  41. this value, and it is that reloc that must fit into the section.
  42. This happens in eg, gcc/testsuite/gcc.c-torture/compile/labels-3.c. */
  43. if ((octets + bfd_get_reloc_size (reloc->howto))
  44. > bfd_get_section_limit_octets (abfd, input_sec))
  45. return bfd_reloc_ok;
  46. return bfd_reloc_continue;
  47. }
  48. /* Special handler for relocations which don't have to be relocated.
  49. This function just simply returns bfd_reloc_ok. */
  50. static bfd_reloc_status_type
  51. msp430_elf_ignore_reloc (bfd *abfd ATTRIBUTE_UNUSED, arelent *reloc_entry,
  52. asymbol *symbol ATTRIBUTE_UNUSED,
  53. void *data ATTRIBUTE_UNUSED, asection *input_section,
  54. bfd *output_bfd, char **error_message ATTRIBUTE_UNUSED)
  55. {
  56. if (output_bfd != NULL)
  57. reloc_entry->address += input_section->output_offset;
  58. return bfd_reloc_ok;
  59. }
  60. static reloc_howto_type elf_msp430_howto_table[] =
  61. {
  62. HOWTO (R_MSP430_NONE, /* type */
  63. 0, /* rightshift */
  64. 3, /* size (0 = byte, 1 = short, 2 = long) */
  65. 0, /* bitsize */
  66. false, /* pc_relative */
  67. 0, /* bitpos */
  68. complain_overflow_dont,/* complain_on_overflow */
  69. bfd_elf_generic_reloc, /* special_function */
  70. "R_MSP430_NONE", /* name */
  71. false, /* partial_inplace */
  72. 0, /* src_mask */
  73. 0, /* dst_mask */
  74. false), /* pcrel_offset */
  75. HOWTO (R_MSP430_32, /* type */
  76. 0, /* rightshift */
  77. 2, /* size (0 = byte, 1 = short, 2 = long) */
  78. 32, /* bitsize */
  79. false, /* pc_relative */
  80. 0, /* bitpos */
  81. complain_overflow_bitfield,/* complain_on_overflow */
  82. bfd_elf_generic_reloc, /* special_function */
  83. "R_MSP430_32", /* name */
  84. false, /* partial_inplace */
  85. 0xffffffff, /* src_mask */
  86. 0xffffffff, /* dst_mask */
  87. false), /* pcrel_offset */
  88. /* A 10 bit PC relative relocation. */
  89. HOWTO (R_MSP430_10_PCREL, /* type */
  90. 1, /* rightshift */
  91. 1, /* size (0 = byte, 1 = short, 2 = long) */
  92. 10, /* bitsize */
  93. true, /* pc_relative */
  94. 0, /* bitpos */
  95. complain_overflow_bitfield,/* complain_on_overflow */
  96. bfd_elf_generic_reloc, /* special_function */
  97. "R_MSP430_10_PCREL", /* name */
  98. false, /* partial_inplace */
  99. 0x3ff, /* src_mask */
  100. 0x3ff, /* dst_mask */
  101. true), /* pcrel_offset */
  102. /* A 16 bit absolute relocation. */
  103. HOWTO (R_MSP430_16, /* type */
  104. 0, /* rightshift */
  105. 1, /* size (0 = byte, 1 = short, 2 = long) */
  106. 16, /* bitsize */
  107. false, /* pc_relative */
  108. 0, /* bitpos */
  109. complain_overflow_dont,/* complain_on_overflow */
  110. bfd_elf_generic_reloc, /* special_function */
  111. "R_MSP430_16", /* name */
  112. false, /* partial_inplace */
  113. 0, /* src_mask */
  114. 0xffff, /* dst_mask */
  115. false), /* pcrel_offset */
  116. /* A 16 bit PC relative relocation for command address. */
  117. HOWTO (R_MSP430_16_PCREL, /* type */
  118. 1, /* rightshift */
  119. 1, /* size (0 = byte, 1 = short, 2 = long) */
  120. 16, /* bitsize */
  121. true, /* pc_relative */
  122. 0, /* bitpos */
  123. complain_overflow_dont,/* complain_on_overflow */
  124. bfd_elf_generic_reloc, /* special_function */
  125. "R_MSP430_16_PCREL", /* name */
  126. false, /* partial_inplace */
  127. 0, /* src_mask */
  128. 0xffff, /* dst_mask */
  129. true), /* pcrel_offset */
  130. /* A 16 bit absolute relocation, byte operations. */
  131. HOWTO (R_MSP430_16_BYTE, /* type */
  132. 0, /* rightshift */
  133. 1, /* size (0 = byte, 1 = short, 2 = long) */
  134. 16, /* bitsize */
  135. false, /* pc_relative */
  136. 0, /* bitpos */
  137. complain_overflow_dont,/* complain_on_overflow */
  138. bfd_elf_generic_reloc, /* special_function */
  139. "R_MSP430_16_BYTE", /* name */
  140. false, /* partial_inplace */
  141. 0xffff, /* src_mask */
  142. 0xffff, /* dst_mask */
  143. false), /* pcrel_offset */
  144. /* A 16 bit absolute relocation for command address. */
  145. HOWTO (R_MSP430_16_PCREL_BYTE,/* type */
  146. 1, /* rightshift */
  147. 1, /* size (0 = byte, 1 = short, 2 = long) */
  148. 16, /* bitsize */
  149. true, /* pc_relative */
  150. 0, /* bitpos */
  151. complain_overflow_dont,/* complain_on_overflow */
  152. bfd_elf_generic_reloc, /* special_function */
  153. "R_MSP430_16_PCREL_BYTE",/* name */
  154. false, /* partial_inplace */
  155. 0xffff, /* src_mask */
  156. 0xffff, /* dst_mask */
  157. true), /* pcrel_offset */
  158. /* A 10 bit PC relative relocation for complicated polymorphs. */
  159. HOWTO (R_MSP430_2X_PCREL, /* type */
  160. 1, /* rightshift */
  161. 2, /* size (0 = byte, 1 = short, 2 = long) */
  162. 10, /* bitsize */
  163. true, /* pc_relative */
  164. 0, /* bitpos */
  165. complain_overflow_bitfield,/* complain_on_overflow */
  166. bfd_elf_generic_reloc, /* special_function */
  167. "R_MSP430_2X_PCREL", /* name */
  168. false, /* partial_inplace */
  169. 0x3ff, /* src_mask */
  170. 0x3ff, /* dst_mask */
  171. true), /* pcrel_offset */
  172. /* A 16 bit relaxable relocation for command address. */
  173. HOWTO (R_MSP430_RL_PCREL, /* type */
  174. 1, /* rightshift */
  175. 1, /* size (0 = byte, 1 = short, 2 = long) */
  176. 16, /* bitsize */
  177. true, /* pc_relative */
  178. 0, /* bitpos */
  179. complain_overflow_dont,/* complain_on_overflow */
  180. bfd_elf_generic_reloc, /* special_function */
  181. "R_MSP430_RL_PCREL", /* name */
  182. false, /* partial_inplace */
  183. 0, /* src_mask */
  184. 0xffff, /* dst_mask */
  185. true) /* pcrel_offset */
  186. /* A 8-bit absolute relocation. */
  187. , HOWTO (R_MSP430_8, /* type */
  188. 0, /* rightshift */
  189. 0, /* size (0 = byte, 1 = short, 2 = long) */
  190. 8, /* bitsize */
  191. false, /* pc_relative */
  192. 0, /* bitpos */
  193. complain_overflow_dont,/* complain_on_overflow */
  194. bfd_elf_generic_reloc, /* special_function */
  195. "R_MSP430_8", /* name */
  196. false, /* partial_inplace */
  197. 0, /* src_mask */
  198. 0xffff, /* dst_mask */
  199. false), /* pcrel_offset */
  200. /* Together with a following reloc, allows for the difference
  201. between two symbols to be the real addend of the second reloc. */
  202. HOWTO (R_MSP430_SYM_DIFF, /* type */
  203. 0, /* rightshift */
  204. 2, /* size (0 = byte, 1 = short, 2 = long) */
  205. 32, /* bitsize */
  206. false, /* pc_relative */
  207. 0, /* bitpos */
  208. complain_overflow_dont,/* complain_on_overflow */
  209. rl78_sym_diff_handler, /* special handler. */
  210. "R_MSP430_SYM_DIFF", /* name */
  211. false, /* partial_inplace */
  212. 0xffffffff, /* src_mask */
  213. 0xffffffff, /* dst_mask */
  214. false), /* pcrel_offset */
  215. /* The length of unsigned-leb128 is variable, just assume the
  216. size is one byte here. */
  217. HOWTO (R_MSP430_GNU_SET_ULEB128, /* type */
  218. 0, /* rightshift */
  219. 0, /* size */
  220. 0, /* bitsize */
  221. false, /* pc_relative */
  222. 0, /* bitpos */
  223. complain_overflow_dont, /* complain_on_overflow */
  224. msp430_elf_ignore_reloc, /* special handler. */
  225. "R_MSP430_GNU_SET_ULEB128", /* name */
  226. false, /* partial_inplace */
  227. 0, /* src_mask */
  228. 0, /* dst_mask */
  229. false), /* pcrel_offset */
  230. /* The length of unsigned-leb128 is variable, just assume the
  231. size is one byte here. */
  232. HOWTO (R_MSP430_GNU_SUB_ULEB128, /* type */
  233. 0, /* rightshift */
  234. 0, /* size */
  235. 0, /* bitsize */
  236. false, /* pc_relative */
  237. 0, /* bitpos */
  238. complain_overflow_dont, /* complain_on_overflow */
  239. msp430_elf_ignore_reloc, /* special handler. */
  240. "R_MSP430_GNU_SUB_ULEB128", /* name */
  241. false, /* partial_inplace */
  242. 0, /* src_mask */
  243. 0, /* dst_mask */
  244. false), /* pcrel_offset */
  245. };
  246. static reloc_howto_type elf_msp430x_howto_table[] =
  247. {
  248. HOWTO (R_MSP430_NONE, /* type */
  249. 0, /* rightshift */
  250. 3, /* size (0 = byte, 1 = short, 2 = long) */
  251. 0, /* bitsize */
  252. false, /* pc_relative */
  253. 0, /* bitpos */
  254. complain_overflow_dont,/* complain_on_overflow */
  255. bfd_elf_generic_reloc, /* special_function */
  256. "R_MSP430_NONE", /* name */
  257. false, /* partial_inplace */
  258. 0, /* src_mask */
  259. 0, /* dst_mask */
  260. false), /* pcrel_offset */
  261. HOWTO (R_MSP430_ABS32, /* type */
  262. 0, /* rightshift */
  263. 2, /* size (0 = byte, 1 = short, 2 = long) */
  264. 32, /* bitsize */
  265. false, /* pc_relative */
  266. 0, /* bitpos */
  267. complain_overflow_bitfield,/* complain_on_overflow */
  268. bfd_elf_generic_reloc, /* special_function */
  269. "R_MSP430_ABS32", /* name */
  270. false, /* partial_inplace */
  271. 0xffffffff, /* src_mask */
  272. 0xffffffff, /* dst_mask */
  273. false), /* pcrel_offset */
  274. HOWTO (R_MSP430_ABS16, /* type */
  275. 0, /* rightshift */
  276. 1, /* size (0 = byte, 1 = short, 2 = long) */
  277. 16, /* bitsize */
  278. false, /* pc_relative */
  279. 0, /* bitpos */
  280. complain_overflow_dont,/* complain_on_overflow */
  281. bfd_elf_generic_reloc, /* special_function */
  282. "R_MSP430_ABS16", /* name */
  283. false, /* partial_inplace */
  284. 0, /* src_mask */
  285. 0xffff, /* dst_mask */
  286. false), /* pcrel_offset */
  287. HOWTO (R_MSP430_ABS8, /* type */
  288. 0, /* rightshift */
  289. 0, /* size (0 = byte, 1 = short, 2 = long) */
  290. 8, /* bitsize */
  291. false, /* pc_relative */
  292. 0, /* bitpos */
  293. complain_overflow_bitfield,/* complain_on_overflow */
  294. bfd_elf_generic_reloc, /* special_function */
  295. "R_MSP430_ABS8", /* name */
  296. false, /* partial_inplace */
  297. 0xff, /* src_mask */
  298. 0xff, /* dst_mask */
  299. false), /* pcrel_offset */
  300. HOWTO (R_MSP430_PCR16, /* type */
  301. 1, /* rightshift */
  302. 1, /* size (0 = byte, 1 = short, 2 = long) */
  303. 16, /* bitsize */
  304. true, /* pc_relative */
  305. 0, /* bitpos */
  306. complain_overflow_dont,/* complain_on_overflow */
  307. bfd_elf_generic_reloc, /* special_function */
  308. "R_MSP430_PCR16", /* name */
  309. false, /* partial_inplace */
  310. 0, /* src_mask */
  311. 0xffff, /* dst_mask */
  312. true), /* pcrel_offset */
  313. HOWTO (R_MSP430X_PCR20_EXT_SRC,/* type */
  314. 0, /* rightshift */
  315. 2, /* size (0 = byte, 1 = short, 2 = long) */
  316. 32, /* bitsize */
  317. true, /* pc_relative */
  318. 0, /* bitpos */
  319. complain_overflow_dont,/* complain_on_overflow */
  320. bfd_elf_generic_reloc, /* special_function */
  321. "R_MSP430X_PCR20_EXT_SRC",/* name */
  322. false, /* partial_inplace */
  323. 0, /* src_mask */
  324. 0xffff, /* dst_mask */
  325. true), /* pcrel_offset */
  326. HOWTO (R_MSP430X_PCR20_EXT_DST,/* type */
  327. 0, /* rightshift */
  328. 2, /* size (0 = byte, 1 = short, 2 = long) */
  329. 32, /* bitsize */
  330. true, /* pc_relative */
  331. 0, /* bitpos */
  332. complain_overflow_dont,/* complain_on_overflow */
  333. bfd_elf_generic_reloc, /* special_function */
  334. "R_MSP430X_PCR20_EXT_DST",/* name */
  335. false, /* partial_inplace */
  336. 0, /* src_mask */
  337. 0xffff, /* dst_mask */
  338. true), /* pcrel_offset */
  339. HOWTO (R_MSP430X_PCR20_EXT_ODST,/* type */
  340. 0, /* rightshift */
  341. 2, /* size (0 = byte, 1 = short, 2 = long) */
  342. 32, /* bitsize */
  343. true, /* pc_relative */
  344. 0, /* bitpos */
  345. complain_overflow_dont,/* complain_on_overflow */
  346. bfd_elf_generic_reloc, /* special_function */
  347. "R_MSP430X_PCR20_EXT_ODST",/* name */
  348. false, /* partial_inplace */
  349. 0, /* src_mask */
  350. 0xffff, /* dst_mask */
  351. true), /* pcrel_offset */
  352. HOWTO (R_MSP430X_ABS20_EXT_SRC,/* type */
  353. 0, /* rightshift */
  354. 2, /* size (0 = byte, 1 = short, 2 = long) */
  355. 32, /* bitsize */
  356. true, /* pc_relative */
  357. 0, /* bitpos */
  358. complain_overflow_dont,/* complain_on_overflow */
  359. bfd_elf_generic_reloc, /* special_function */
  360. "R_MSP430X_ABS20_EXT_SRC",/* name */
  361. false, /* partial_inplace */
  362. 0, /* src_mask */
  363. 0xffff, /* dst_mask */
  364. true), /* pcrel_offset */
  365. HOWTO (R_MSP430X_ABS20_EXT_DST,/* type */
  366. 0, /* rightshift */
  367. 2, /* size (0 = byte, 1 = short, 2 = long) */
  368. 32, /* bitsize */
  369. true, /* pc_relative */
  370. 0, /* bitpos */
  371. complain_overflow_dont,/* complain_on_overflow */
  372. bfd_elf_generic_reloc, /* special_function */
  373. "R_MSP430X_ABS20_EXT_DST",/* name */
  374. false, /* partial_inplace */
  375. 0, /* src_mask */
  376. 0xffff, /* dst_mask */
  377. true), /* pcrel_offset */
  378. HOWTO (R_MSP430X_ABS20_EXT_ODST,/* type */
  379. 0, /* rightshift */
  380. 2, /* size (0 = byte, 1 = short, 2 = long) */
  381. 32, /* bitsize */
  382. true, /* pc_relative */
  383. 0, /* bitpos */
  384. complain_overflow_dont,/* complain_on_overflow */
  385. bfd_elf_generic_reloc, /* special_function */
  386. "R_MSP430X_ABS20_EXT_ODST",/* name */
  387. false, /* partial_inplace */
  388. 0, /* src_mask */
  389. 0xffff, /* dst_mask */
  390. true), /* pcrel_offset */
  391. HOWTO (R_MSP430X_ABS20_ADR_SRC,/* type */
  392. 0, /* rightshift */
  393. 2, /* size (0 = byte, 1 = short, 2 = long) */
  394. 32, /* bitsize */
  395. true, /* pc_relative */
  396. 0, /* bitpos */
  397. complain_overflow_dont,/* complain_on_overflow */
  398. bfd_elf_generic_reloc, /* special_function */
  399. "R_MSP430X_ABS20_ADR_SRC",/* name */
  400. false, /* partial_inplace */
  401. 0, /* src_mask */
  402. 0xffff, /* dst_mask */
  403. true), /* pcrel_offset */
  404. HOWTO (R_MSP430X_ABS20_ADR_DST,/* type */
  405. 0, /* rightshift */
  406. 2, /* size (0 = byte, 1 = short, 2 = long) */
  407. 32, /* bitsize */
  408. true, /* pc_relative */
  409. 0, /* bitpos */
  410. complain_overflow_dont,/* complain_on_overflow */
  411. bfd_elf_generic_reloc, /* special_function */
  412. "R_MSP430X_ABS20_ADR_DST",/* name */
  413. false, /* partial_inplace */
  414. 0, /* src_mask */
  415. 0xffff, /* dst_mask */
  416. true), /* pcrel_offset */
  417. HOWTO (R_MSP430X_PCR16, /* type */
  418. 0, /* rightshift */
  419. 2, /* size (0 = byte, 1 = short, 2 = long) */
  420. 32, /* bitsize */
  421. true, /* pc_relative */
  422. 0, /* bitpos */
  423. complain_overflow_dont,/* complain_on_overflow */
  424. bfd_elf_generic_reloc, /* special_function */
  425. "R_MSP430X_PCR16", /* name */
  426. false, /* partial_inplace */
  427. 0, /* src_mask */
  428. 0xffff, /* dst_mask */
  429. true), /* pcrel_offset */
  430. HOWTO (R_MSP430X_PCR20_CALL, /* type */
  431. 0, /* rightshift */
  432. 2, /* size (0 = byte, 1 = short, 2 = long) */
  433. 32, /* bitsize */
  434. true, /* pc_relative */
  435. 0, /* bitpos */
  436. complain_overflow_dont,/* complain_on_overflow */
  437. bfd_elf_generic_reloc, /* special_function */
  438. "R_MSP430X_PCR20_CALL",/* name */
  439. false, /* partial_inplace */
  440. 0, /* src_mask */
  441. 0xffff, /* dst_mask */
  442. true), /* pcrel_offset */
  443. HOWTO (R_MSP430X_ABS16, /* type */
  444. 0, /* rightshift */
  445. 2, /* size (0 = byte, 1 = short, 2 = long) */
  446. 32, /* bitsize */
  447. true, /* pc_relative */
  448. 0, /* bitpos */
  449. complain_overflow_dont,/* complain_on_overflow */
  450. bfd_elf_generic_reloc, /* special_function */
  451. "R_MSP430X_ABS16", /* name */
  452. false, /* partial_inplace */
  453. 0, /* src_mask */
  454. 0xffff, /* dst_mask */
  455. true), /* pcrel_offset */
  456. HOWTO (R_MSP430_ABS_HI16, /* type */
  457. 0, /* rightshift */
  458. 2, /* size (0 = byte, 1 = short, 2 = long) */
  459. 32, /* bitsize */
  460. true, /* pc_relative */
  461. 0, /* bitpos */
  462. complain_overflow_dont,/* complain_on_overflow */
  463. bfd_elf_generic_reloc, /* special_function */
  464. "R_MSP430_ABS_HI16", /* name */
  465. false, /* partial_inplace */
  466. 0, /* src_mask */
  467. 0xffff, /* dst_mask */
  468. true), /* pcrel_offset */
  469. HOWTO (R_MSP430_PREL31, /* type */
  470. 0, /* rightshift */
  471. 2, /* size (0 = byte, 1 = short, 2 = long) */
  472. 32, /* bitsize */
  473. true, /* pc_relative */
  474. 0, /* bitpos */
  475. complain_overflow_dont,/* complain_on_overflow */
  476. bfd_elf_generic_reloc, /* special_function */
  477. "R_MSP430_PREL31", /* name */
  478. false, /* partial_inplace */
  479. 0, /* src_mask */
  480. 0xffff, /* dst_mask */
  481. true), /* pcrel_offset */
  482. EMPTY_HOWTO (R_MSP430_EHTYPE),
  483. /* A 10 bit PC relative relocation. */
  484. HOWTO (R_MSP430X_10_PCREL, /* type */
  485. 1, /* rightshift */
  486. 1, /* size (0 = byte, 1 = short, 2 = long) */
  487. 10, /* bitsize */
  488. true, /* pc_relative */
  489. 0, /* bitpos */
  490. complain_overflow_bitfield,/* complain_on_overflow */
  491. bfd_elf_generic_reloc, /* special_function */
  492. "R_MSP430X_10_PCREL", /* name */
  493. false, /* partial_inplace */
  494. 0x3ff, /* src_mask */
  495. 0x3ff, /* dst_mask */
  496. true), /* pcrel_offset */
  497. /* A 10 bit PC relative relocation for complicated polymorphs. */
  498. HOWTO (R_MSP430X_2X_PCREL, /* type */
  499. 1, /* rightshift */
  500. 2, /* size (0 = byte, 1 = short, 2 = long) */
  501. 10, /* bitsize */
  502. true, /* pc_relative */
  503. 0, /* bitpos */
  504. complain_overflow_bitfield,/* complain_on_overflow */
  505. bfd_elf_generic_reloc, /* special_function */
  506. "R_MSP430X_2X_PCREL", /* name */
  507. false, /* partial_inplace */
  508. 0x3ff, /* src_mask */
  509. 0x3ff, /* dst_mask */
  510. true), /* pcrel_offset */
  511. /* Together with a following reloc, allows for the difference
  512. between two symbols to be the real addend of the second reloc. */
  513. HOWTO (R_MSP430X_SYM_DIFF, /* type */
  514. 0, /* rightshift */
  515. 2, /* size (0 = byte, 1 = short, 2 = long) */
  516. 32, /* bitsize */
  517. false, /* pc_relative */
  518. 0, /* bitpos */
  519. complain_overflow_dont,/* complain_on_overflow */
  520. rl78_sym_diff_handler, /* special handler. */
  521. "R_MSP430X_SYM_DIFF", /* name */
  522. false, /* partial_inplace */
  523. 0xffffffff, /* src_mask */
  524. 0xffffffff, /* dst_mask */
  525. false), /* pcrel_offset */
  526. /* The length of unsigned-leb128 is variable, just assume the
  527. size is one byte here. */
  528. HOWTO (R_MSP430X_GNU_SET_ULEB128, /* type */
  529. 0, /* rightshift */
  530. 0, /* size */
  531. 0, /* bitsize */
  532. false, /* pc_relative */
  533. 0, /* bitpos */
  534. complain_overflow_dont, /* complain_on_overflow */
  535. msp430_elf_ignore_reloc, /* special handler. */
  536. "R_MSP430X_GNU_SET_ULEB128", /* name */
  537. false, /* partial_inplace */
  538. 0, /* src_mask */
  539. 0, /* dst_mask */
  540. false), /* pcrel_offset */
  541. /* The length of unsigned-leb128 is variable, just assume the
  542. size is one byte here. */
  543. HOWTO (R_MSP430X_GNU_SUB_ULEB128, /* type */
  544. 0, /* rightshift */
  545. 0, /* size */
  546. 0, /* bitsize */
  547. false, /* pc_relative */
  548. 0, /* bitpos */
  549. complain_overflow_dont, /* complain_on_overflow */
  550. msp430_elf_ignore_reloc, /* special handler. */
  551. "R_MSP430X_GNU_SUB_ULEB128", /* name */
  552. false, /* partial_inplace */
  553. 0, /* src_mask */
  554. 0, /* dst_mask */
  555. false), /* pcrel_offset */
  556. };
  557. /* Map BFD reloc types to MSP430 ELF reloc types. */
  558. struct msp430_reloc_map
  559. {
  560. bfd_reloc_code_real_type bfd_reloc_val;
  561. unsigned int elf_reloc_val;
  562. };
  563. static const struct msp430_reloc_map msp430_reloc_map[] =
  564. {
  565. {BFD_RELOC_NONE, R_MSP430_NONE},
  566. {BFD_RELOC_32, R_MSP430_32},
  567. {BFD_RELOC_MSP430_10_PCREL, R_MSP430_10_PCREL},
  568. {BFD_RELOC_16, R_MSP430_16_BYTE},
  569. {BFD_RELOC_MSP430_16_PCREL, R_MSP430_16_PCREL},
  570. {BFD_RELOC_MSP430_16, R_MSP430_16},
  571. {BFD_RELOC_MSP430_16_PCREL_BYTE, R_MSP430_16_PCREL_BYTE},
  572. {BFD_RELOC_MSP430_16_BYTE, R_MSP430_16_BYTE},
  573. {BFD_RELOC_MSP430_2X_PCREL, R_MSP430_2X_PCREL},
  574. {BFD_RELOC_MSP430_RL_PCREL, R_MSP430_RL_PCREL},
  575. {BFD_RELOC_8, R_MSP430_8},
  576. {BFD_RELOC_MSP430_SYM_DIFF, R_MSP430_SYM_DIFF},
  577. {BFD_RELOC_MSP430_SET_ULEB128, R_MSP430_GNU_SET_ULEB128 },
  578. {BFD_RELOC_MSP430_SUB_ULEB128, R_MSP430_GNU_SUB_ULEB128 }
  579. };
  580. static const struct msp430_reloc_map msp430x_reloc_map[] =
  581. {
  582. {BFD_RELOC_NONE, R_MSP430_NONE},
  583. {BFD_RELOC_32, R_MSP430_ABS32},
  584. {BFD_RELOC_16, R_MSP430_ABS16},
  585. {BFD_RELOC_8, R_MSP430_ABS8},
  586. {BFD_RELOC_MSP430_ABS8, R_MSP430_ABS8},
  587. {BFD_RELOC_MSP430X_PCR20_EXT_SRC, R_MSP430X_PCR20_EXT_SRC},
  588. {BFD_RELOC_MSP430X_PCR20_EXT_DST, R_MSP430X_PCR20_EXT_DST},
  589. {BFD_RELOC_MSP430X_PCR20_EXT_ODST, R_MSP430X_PCR20_EXT_ODST},
  590. {BFD_RELOC_MSP430X_ABS20_EXT_SRC, R_MSP430X_ABS20_EXT_SRC},
  591. {BFD_RELOC_MSP430X_ABS20_EXT_DST, R_MSP430X_ABS20_EXT_DST},
  592. {BFD_RELOC_MSP430X_ABS20_EXT_ODST, R_MSP430X_ABS20_EXT_ODST},
  593. {BFD_RELOC_MSP430X_ABS20_ADR_SRC, R_MSP430X_ABS20_ADR_SRC},
  594. {BFD_RELOC_MSP430X_ABS20_ADR_DST, R_MSP430X_ABS20_ADR_DST},
  595. {BFD_RELOC_MSP430X_PCR16, R_MSP430X_PCR16},
  596. {BFD_RELOC_MSP430X_PCR20_CALL, R_MSP430X_PCR20_CALL},
  597. {BFD_RELOC_MSP430X_ABS16, R_MSP430X_ABS16},
  598. {BFD_RELOC_MSP430_ABS_HI16, R_MSP430_ABS_HI16},
  599. {BFD_RELOC_MSP430_PREL31, R_MSP430_PREL31},
  600. {BFD_RELOC_MSP430_10_PCREL, R_MSP430X_10_PCREL},
  601. {BFD_RELOC_MSP430_2X_PCREL, R_MSP430X_2X_PCREL},
  602. {BFD_RELOC_MSP430_RL_PCREL, R_MSP430X_PCR16},
  603. {BFD_RELOC_MSP430_SYM_DIFF, R_MSP430X_SYM_DIFF},
  604. {BFD_RELOC_MSP430_SET_ULEB128, R_MSP430X_GNU_SET_ULEB128 },
  605. {BFD_RELOC_MSP430_SUB_ULEB128, R_MSP430X_GNU_SUB_ULEB128 }
  606. };
  607. static inline bool
  608. uses_msp430x_relocs (bfd * abfd)
  609. {
  610. extern const bfd_target msp430_elf32_ti_vec;
  611. return bfd_get_mach (abfd) == bfd_mach_msp430x
  612. || abfd->xvec == & msp430_elf32_ti_vec;
  613. }
  614. static reloc_howto_type *
  615. bfd_elf32_bfd_reloc_type_lookup (bfd * abfd ATTRIBUTE_UNUSED,
  616. bfd_reloc_code_real_type code)
  617. {
  618. unsigned int i;
  619. if (uses_msp430x_relocs (abfd))
  620. {
  621. for (i = ARRAY_SIZE (msp430x_reloc_map); i--;)
  622. if (msp430x_reloc_map[i].bfd_reloc_val == code)
  623. return elf_msp430x_howto_table + msp430x_reloc_map[i].elf_reloc_val;
  624. }
  625. else
  626. {
  627. for (i = 0; i < ARRAY_SIZE (msp430_reloc_map); i++)
  628. if (msp430_reloc_map[i].bfd_reloc_val == code)
  629. return &elf_msp430_howto_table[msp430_reloc_map[i].elf_reloc_val];
  630. }
  631. return NULL;
  632. }
  633. static reloc_howto_type *
  634. bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  635. const char *r_name)
  636. {
  637. unsigned int i;
  638. if (uses_msp430x_relocs (abfd))
  639. {
  640. for (i = ARRAY_SIZE (elf_msp430x_howto_table); i--;)
  641. if (elf_msp430x_howto_table[i].name != NULL
  642. && strcasecmp (elf_msp430x_howto_table[i].name, r_name) == 0)
  643. return elf_msp430x_howto_table + i;
  644. }
  645. else
  646. {
  647. for (i = 0;
  648. i < (sizeof (elf_msp430_howto_table)
  649. / sizeof (elf_msp430_howto_table[0]));
  650. i++)
  651. if (elf_msp430_howto_table[i].name != NULL
  652. && strcasecmp (elf_msp430_howto_table[i].name, r_name) == 0)
  653. return &elf_msp430_howto_table[i];
  654. }
  655. return NULL;
  656. }
  657. /* Set the howto pointer for an MSP430 ELF reloc. */
  658. static bool
  659. msp430_info_to_howto_rela (bfd * abfd,
  660. arelent * cache_ptr,
  661. Elf_Internal_Rela * dst)
  662. {
  663. unsigned int r_type;
  664. r_type = ELF32_R_TYPE (dst->r_info);
  665. if (uses_msp430x_relocs (abfd))
  666. {
  667. if (r_type >= (unsigned int) R_MSP430x_max)
  668. {
  669. /* xgettext:c-format */
  670. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  671. abfd, r_type);
  672. bfd_set_error (bfd_error_bad_value);
  673. return false;
  674. }
  675. cache_ptr->howto = elf_msp430x_howto_table + r_type;
  676. }
  677. else if (r_type >= (unsigned int) R_MSP430_max)
  678. {
  679. /* xgettext:c-format */
  680. _bfd_error_handler (_("%pB: unsupported relocation type %#x"),
  681. abfd, r_type);
  682. bfd_set_error (bfd_error_bad_value);
  683. return false;
  684. }
  685. else
  686. cache_ptr->howto = &elf_msp430_howto_table[r_type];
  687. return true;
  688. }
  689. /* Look through the relocs for a section during the first phase.
  690. Since we don't do .gots or .plts, we just need to consider the
  691. virtual table relocs for gc. */
  692. static bool
  693. elf32_msp430_check_relocs (bfd * abfd, struct bfd_link_info * info,
  694. asection * sec, const Elf_Internal_Rela * relocs)
  695. {
  696. Elf_Internal_Shdr *symtab_hdr;
  697. struct elf_link_hash_entry **sym_hashes;
  698. const Elf_Internal_Rela *rel;
  699. const Elf_Internal_Rela *rel_end;
  700. if (bfd_link_relocatable (info))
  701. return true;
  702. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  703. sym_hashes = elf_sym_hashes (abfd);
  704. rel_end = relocs + sec->reloc_count;
  705. for (rel = relocs; rel < rel_end; rel++)
  706. {
  707. struct elf_link_hash_entry *h;
  708. unsigned long r_symndx;
  709. r_symndx = ELF32_R_SYM (rel->r_info);
  710. if (r_symndx < symtab_hdr->sh_info)
  711. h = NULL;
  712. else
  713. {
  714. h = sym_hashes[r_symndx - symtab_hdr->sh_info];
  715. while (h->root.type == bfd_link_hash_indirect
  716. || h->root.type == bfd_link_hash_warning)
  717. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  718. }
  719. }
  720. return true;
  721. }
  722. /* Perform a single relocation. By default we use the standard BFD
  723. routines, but a few relocs, we have to do them ourselves. */
  724. static bfd_reloc_status_type
  725. msp430_final_link_relocate (reloc_howto_type * howto,
  726. bfd * input_bfd,
  727. asection * input_section,
  728. bfd_byte * contents,
  729. Elf_Internal_Rela * rel,
  730. bfd_vma relocation,
  731. struct bfd_link_info * info)
  732. {
  733. static asection * sym_diff_section;
  734. static bfd_vma sym_diff_value;
  735. struct bfd_elf_section_data * esd = elf_section_data (input_section);
  736. bfd_reloc_status_type r = bfd_reloc_ok;
  737. bfd_vma x;
  738. bfd_signed_vma srel;
  739. bool is_rel_reloc = false;
  740. if (uses_msp430x_relocs (input_bfd))
  741. {
  742. /* See if we have a REL type relocation. */
  743. is_rel_reloc = (esd->rel.hdr != NULL);
  744. /* Sanity check - only one type of relocation per section.
  745. FIXME: Theoretically it is possible to have both types,
  746. but if that happens how can we distinguish between the two ? */
  747. BFD_ASSERT (! is_rel_reloc || ! esd->rela.hdr);
  748. /* If we are using a REL relocation then the addend should be empty. */
  749. BFD_ASSERT (! is_rel_reloc || rel->r_addend == 0);
  750. }
  751. if (debug_relocs)
  752. printf ("writing relocation (%p) at 0x%lx type: %d\n", rel,
  753. (long) (input_section->output_section->vma + input_section->output_offset
  754. + rel->r_offset), howto->type);
  755. if (sym_diff_section != NULL)
  756. {
  757. BFD_ASSERT (sym_diff_section == input_section);
  758. if (uses_msp430x_relocs (input_bfd))
  759. switch (howto->type)
  760. {
  761. case R_MSP430X_GNU_SET_ULEB128:
  762. relocation += (!is_rel_reloc ? rel->r_addend : 0);
  763. /* Fall through. */
  764. case R_MSP430_ABS32:
  765. /* If we are computing a 32-bit value for the location lists
  766. and the result is 0 then we add one to the value. A zero
  767. value can result because of linker relaxation deleteing
  768. prologue instructions and using a value of 1 (for the begin
  769. and end offsets in the location list entry) results in a
  770. nul entry which does not prevent the following entries from
  771. being parsed. */
  772. if (relocation == sym_diff_value
  773. && strcmp (input_section->name, ".debug_loc") == 0)
  774. ++ relocation;
  775. /* Fall through. */
  776. case R_MSP430_ABS16:
  777. case R_MSP430X_ABS16:
  778. case R_MSP430_ABS8:
  779. BFD_ASSERT (! is_rel_reloc);
  780. relocation -= sym_diff_value;
  781. break;
  782. default:
  783. return bfd_reloc_dangerous;
  784. }
  785. else
  786. switch (howto->type)
  787. {
  788. case R_MSP430_GNU_SET_ULEB128:
  789. relocation += (!is_rel_reloc ? rel->r_addend : 0);
  790. /* Fall through. */
  791. case R_MSP430_32:
  792. case R_MSP430_16:
  793. case R_MSP430_16_BYTE:
  794. case R_MSP430_8:
  795. relocation -= sym_diff_value;
  796. break;
  797. default:
  798. return bfd_reloc_dangerous;
  799. }
  800. sym_diff_section = NULL;
  801. }
  802. if ((uses_msp430x_relocs (input_bfd)
  803. && howto->type == R_MSP430X_GNU_SET_ULEB128)
  804. || (!uses_msp430x_relocs (input_bfd)
  805. && howto->type == R_MSP430_GNU_SET_ULEB128))
  806. {
  807. unsigned int len, new_len = 0;
  808. bfd_byte *endp, *p;
  809. unsigned int val = relocation;
  810. _bfd_read_unsigned_leb128 (input_bfd, contents + rel->r_offset, &len);
  811. /* Clean the contents value to zero. Do not reduce the length. */
  812. p = contents + rel->r_offset;
  813. endp = (p + len) - 1;
  814. memset (p, 0x80, len - 1);
  815. *(endp) = 0;
  816. /* Get the length of the new uleb128 value. */
  817. do
  818. {
  819. new_len++;
  820. val >>= 7;
  821. } while (val);
  822. if (new_len > len)
  823. {
  824. _bfd_error_handler
  825. (_("error: final size of uleb128 value at offset 0x%lx in %pA "
  826. "from %pB exceeds available space"),
  827. (long) rel->r_offset, input_section, input_bfd);
  828. }
  829. else
  830. {
  831. /* If the number of bytes required to store the new value has
  832. decreased, "right align" the new value within the available space,
  833. so the MSB side is padded with uleb128 zeros (0x80). */
  834. p = _bfd_write_unsigned_leb128 (p + (len - new_len), endp,
  835. relocation);
  836. /* We checked there is enough space for the new value above, so this
  837. should never be NULL. */
  838. BFD_ASSERT (p);
  839. }
  840. return bfd_reloc_ok;
  841. }
  842. else if (uses_msp430x_relocs (input_bfd))
  843. switch (howto->type)
  844. {
  845. case R_MSP430X_SYM_DIFF:
  846. case R_MSP430X_GNU_SUB_ULEB128:
  847. /* Cache the input section and value.
  848. The offset is unreliable, since relaxation may
  849. have reduced the following reloc's offset. */
  850. BFD_ASSERT (! is_rel_reloc);
  851. sym_diff_section = input_section;
  852. sym_diff_value = relocation + (howto->type == R_MSP430X_GNU_SUB_ULEB128
  853. ? rel->r_addend : 0);
  854. return bfd_reloc_ok;
  855. case R_MSP430_ABS16:
  856. contents += rel->r_offset;
  857. srel = (bfd_signed_vma) relocation;
  858. if (is_rel_reloc)
  859. srel += bfd_get_16 (input_bfd, contents);
  860. else
  861. srel += rel->r_addend;
  862. bfd_put_16 (input_bfd, srel & 0xffff, contents);
  863. break;
  864. case R_MSP430X_10_PCREL:
  865. contents += rel->r_offset;
  866. srel = (bfd_signed_vma) relocation;
  867. if (is_rel_reloc)
  868. srel += bfd_get_16 (input_bfd, contents) & 0x3ff;
  869. else
  870. srel += rel->r_addend;
  871. srel -= rel->r_offset;
  872. srel -= 2; /* Branch instructions add 2 to the PC... */
  873. srel -= (input_section->output_section->vma +
  874. input_section->output_offset);
  875. if (srel & 1)
  876. return bfd_reloc_outofrange;
  877. /* MSP430 addresses commands as words. */
  878. srel >>= 1;
  879. /* Check for an overflow. */
  880. if (srel < -512 || srel > 511)
  881. {
  882. if (info->disable_target_specific_optimizations < 0)
  883. {
  884. static bool warned = false;
  885. if (! warned)
  886. {
  887. info->callbacks->warning
  888. (info,
  889. _("try enabling relaxation to avoid relocation truncations"),
  890. NULL, input_bfd, input_section, relocation);
  891. warned = true;
  892. }
  893. }
  894. return bfd_reloc_overflow;
  895. }
  896. x = bfd_get_16 (input_bfd, contents);
  897. x = (x & 0xfc00) | (srel & 0x3ff);
  898. bfd_put_16 (input_bfd, x, contents);
  899. break;
  900. case R_MSP430X_PCR20_EXT_ODST:
  901. /* [0,4]+[48,16] = ---F ---- ---- FFFF */
  902. contents += rel->r_offset;
  903. srel = (bfd_signed_vma) relocation;
  904. if (is_rel_reloc)
  905. {
  906. bfd_vma addend;
  907. addend = (bfd_get_16 (input_bfd, contents) & 0xf) << 16;
  908. addend |= bfd_get_16 (input_bfd, contents + 6);
  909. srel += addend;
  910. }
  911. else
  912. srel += rel->r_addend;
  913. srel -= rel->r_offset;
  914. srel -= (input_section->output_section->vma +
  915. input_section->output_offset);
  916. bfd_put_16 (input_bfd, (srel & 0xffff), contents + 6);
  917. x = bfd_get_16 (input_bfd, contents);
  918. x = (x & 0xfff0) | ((srel >> 16) & 0xf);
  919. bfd_put_16 (input_bfd, x, contents);
  920. break;
  921. case R_MSP430X_ABS20_EXT_SRC:
  922. /* [7,4]+[32,16] = -78- ---- FFFF */
  923. contents += rel->r_offset;
  924. srel = (bfd_signed_vma) relocation;
  925. if (is_rel_reloc)
  926. {
  927. bfd_vma addend;
  928. addend = (bfd_get_16 (input_bfd, contents) & 0x0780) << 9;
  929. addend |= bfd_get_16 (input_bfd, contents + 4);
  930. srel += addend;
  931. }
  932. else
  933. srel += rel->r_addend;
  934. bfd_put_16 (input_bfd, (srel & 0xffff), contents + 4);
  935. srel >>= 16;
  936. x = bfd_get_16 (input_bfd, contents);
  937. x = (x & 0xf87f) | ((srel << 7) & 0x0780);
  938. bfd_put_16 (input_bfd, x, contents);
  939. break;
  940. case R_MSP430_16_PCREL:
  941. contents += rel->r_offset;
  942. srel = (bfd_signed_vma) relocation;
  943. if (is_rel_reloc)
  944. srel += bfd_get_16 (input_bfd, contents);
  945. else
  946. srel += rel->r_addend;
  947. srel -= rel->r_offset;
  948. /* Only branch instructions add 2 to the PC... */
  949. srel -= (input_section->output_section->vma +
  950. input_section->output_offset);
  951. if (srel & 1)
  952. return bfd_reloc_outofrange;
  953. bfd_put_16 (input_bfd, srel & 0xffff, contents);
  954. break;
  955. case R_MSP430X_PCR20_EXT_DST:
  956. /* [0,4]+[32,16] = ---F ---- FFFF */
  957. contents += rel->r_offset;
  958. srel = (bfd_signed_vma) relocation;
  959. if (is_rel_reloc)
  960. {
  961. bfd_vma addend;
  962. addend = (bfd_get_16 (input_bfd, contents) & 0xf) << 16;
  963. addend |= bfd_get_16 (input_bfd, contents + 4);
  964. srel += addend;
  965. }
  966. else
  967. srel += rel->r_addend;
  968. srel -= rel->r_offset;
  969. srel -= (input_section->output_section->vma +
  970. input_section->output_offset);
  971. bfd_put_16 (input_bfd, (srel & 0xffff), contents + 4);
  972. srel >>= 16;
  973. x = bfd_get_16 (input_bfd, contents);
  974. x = (x & 0xfff0) | (srel & 0xf);
  975. bfd_put_16 (input_bfd, x, contents);
  976. break;
  977. case R_MSP430X_PCR20_EXT_SRC:
  978. /* [7,4]+[32,16] = -78- ---- FFFF */
  979. contents += rel->r_offset;
  980. srel = (bfd_signed_vma) relocation;
  981. if (is_rel_reloc)
  982. {
  983. bfd_vma addend;
  984. addend = ((bfd_get_16 (input_bfd, contents) & 0x0780) << 9);
  985. addend |= bfd_get_16 (input_bfd, contents + 4);
  986. srel += addend;;
  987. }
  988. else
  989. srel += rel->r_addend;
  990. srel -= rel->r_offset;
  991. /* Only branch instructions add 2 to the PC... */
  992. srel -= (input_section->output_section->vma +
  993. input_section->output_offset);
  994. bfd_put_16 (input_bfd, (srel & 0xffff), contents + 4);
  995. srel >>= 16;
  996. x = bfd_get_16 (input_bfd, contents);
  997. x = (x & 0xf87f) | ((srel << 7) & 0x0780);
  998. bfd_put_16 (input_bfd, x, contents);
  999. break;
  1000. case R_MSP430_ABS8:
  1001. contents += rel->r_offset;
  1002. srel = (bfd_signed_vma) relocation;
  1003. if (is_rel_reloc)
  1004. srel += bfd_get_8 (input_bfd, contents);
  1005. else
  1006. srel += rel->r_addend;
  1007. bfd_put_8 (input_bfd, srel & 0xff, contents);
  1008. break;
  1009. case R_MSP430X_ABS20_EXT_DST:
  1010. /* [0,4]+[32,16] = ---F ---- FFFF */
  1011. contents += rel->r_offset;
  1012. srel = (bfd_signed_vma) relocation;
  1013. if (is_rel_reloc)
  1014. {
  1015. bfd_vma addend;
  1016. addend = (bfd_get_16 (input_bfd, contents) & 0xf) << 16;
  1017. addend |= bfd_get_16 (input_bfd, contents + 4);
  1018. srel += addend;
  1019. }
  1020. else
  1021. srel += rel->r_addend;
  1022. bfd_put_16 (input_bfd, (srel & 0xffff), contents + 4);
  1023. srel >>= 16;
  1024. x = bfd_get_16 (input_bfd, contents);
  1025. x = (x & 0xfff0) | (srel & 0xf);
  1026. bfd_put_16 (input_bfd, x, contents);
  1027. break;
  1028. case R_MSP430X_ABS20_EXT_ODST:
  1029. /* [0,4]+[48,16] = ---F ---- ---- FFFF */
  1030. contents += rel->r_offset;
  1031. srel = (bfd_signed_vma) relocation;
  1032. if (is_rel_reloc)
  1033. {
  1034. bfd_vma addend;
  1035. addend = (bfd_get_16 (input_bfd, contents) & 0xf) << 16;
  1036. addend |= bfd_get_16 (input_bfd, contents + 6);
  1037. srel += addend;
  1038. }
  1039. else
  1040. srel += rel->r_addend;
  1041. bfd_put_16 (input_bfd, (srel & 0xffff), contents + 6);
  1042. srel >>= 16;
  1043. x = bfd_get_16 (input_bfd, contents);
  1044. x = (x & 0xfff0) | (srel & 0xf);
  1045. bfd_put_16 (input_bfd, x, contents);
  1046. break;
  1047. case R_MSP430X_ABS20_ADR_SRC:
  1048. /* [8,4]+[16,16] = -F-- FFFF */
  1049. contents += rel->r_offset;
  1050. srel = (bfd_signed_vma) relocation;
  1051. if (is_rel_reloc)
  1052. {
  1053. bfd_vma addend;
  1054. addend = ((bfd_get_16 (input_bfd, contents) & 0xf00) << 8);
  1055. addend |= bfd_get_16 (input_bfd, contents + 2);
  1056. srel += addend;
  1057. }
  1058. else
  1059. srel += rel->r_addend;
  1060. bfd_put_16 (input_bfd, (srel & 0xffff), contents + 2);
  1061. srel >>= 16;
  1062. x = bfd_get_16 (input_bfd, contents);
  1063. x = (x & 0xf0ff) | ((srel << 8) & 0x0f00);
  1064. bfd_put_16 (input_bfd, x, contents);
  1065. break;
  1066. case R_MSP430X_ABS20_ADR_DST:
  1067. /* [0,4]+[16,16] = ---F FFFF */
  1068. contents += rel->r_offset;
  1069. srel = (bfd_signed_vma) relocation;
  1070. if (is_rel_reloc)
  1071. {
  1072. bfd_vma addend;
  1073. addend = ((bfd_get_16 (input_bfd, contents) & 0xf) << 16);
  1074. addend |= bfd_get_16 (input_bfd, contents + 2);
  1075. srel += addend;
  1076. }
  1077. else
  1078. srel += rel->r_addend;
  1079. bfd_put_16 (input_bfd, (srel & 0xffff), contents + 2);
  1080. srel >>= 16;
  1081. x = bfd_get_16 (input_bfd, contents);
  1082. x = (x & 0xfff0) | (srel & 0xf);
  1083. bfd_put_16 (input_bfd, x, contents);
  1084. break;
  1085. case R_MSP430X_ABS16:
  1086. contents += rel->r_offset;
  1087. srel = (bfd_signed_vma) relocation;
  1088. if (is_rel_reloc)
  1089. srel += bfd_get_16 (input_bfd, contents);
  1090. else
  1091. srel += rel->r_addend;
  1092. x = srel;
  1093. if (x > 0xffff)
  1094. return bfd_reloc_overflow;
  1095. bfd_put_16 (input_bfd, srel & 0xffff, contents);
  1096. break;
  1097. case R_MSP430_ABS_HI16:
  1098. /* The EABI specifies that this must be a RELA reloc. */
  1099. BFD_ASSERT (! is_rel_reloc);
  1100. contents += rel->r_offset;
  1101. srel = (bfd_signed_vma) relocation;
  1102. srel += rel->r_addend;
  1103. bfd_put_16 (input_bfd, (srel >> 16) & 0xffff, contents);
  1104. break;
  1105. case R_MSP430X_PCR20_CALL:
  1106. /* [0,4]+[16,16] = ---F FFFF*/
  1107. contents += rel->r_offset;
  1108. srel = (bfd_signed_vma) relocation;
  1109. if (is_rel_reloc)
  1110. {
  1111. bfd_vma addend;
  1112. addend = (bfd_get_16 (input_bfd, contents) & 0xf) << 16;
  1113. addend |= bfd_get_16 (input_bfd, contents + 2);
  1114. srel += addend;
  1115. }
  1116. else
  1117. srel += rel->r_addend;
  1118. srel -= rel->r_offset;
  1119. srel -= (input_section->output_section->vma +
  1120. input_section->output_offset);
  1121. bfd_put_16 (input_bfd, srel & 0xffff, contents + 2);
  1122. srel >>= 16;
  1123. x = bfd_get_16 (input_bfd, contents);
  1124. x = (x & 0xfff0) | (srel & 0xf);
  1125. bfd_put_16 (input_bfd, x, contents);
  1126. break;
  1127. case R_MSP430X_PCR16:
  1128. contents += rel->r_offset;
  1129. srel = (bfd_signed_vma) relocation;
  1130. if (is_rel_reloc)
  1131. srel += bfd_get_16 (input_bfd, contents);
  1132. else
  1133. srel += rel->r_addend;
  1134. srel -= rel->r_offset;
  1135. srel -= (input_section->output_section->vma +
  1136. input_section->output_offset);
  1137. bfd_put_16 (input_bfd, srel & 0xffff, contents);
  1138. break;
  1139. case R_MSP430_PREL31:
  1140. contents += rel->r_offset;
  1141. srel = (bfd_signed_vma) relocation;
  1142. if (is_rel_reloc)
  1143. srel += (bfd_get_32 (input_bfd, contents) & 0x7fffffff);
  1144. else
  1145. srel += rel->r_addend;
  1146. srel += rel->r_addend;
  1147. x = bfd_get_32 (input_bfd, contents);
  1148. x = (x & 0x80000000) | ((srel >> 31) & 0x7fffffff);
  1149. bfd_put_32 (input_bfd, x, contents);
  1150. break;
  1151. default:
  1152. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  1153. contents, rel->r_offset,
  1154. relocation, rel->r_addend);
  1155. }
  1156. else
  1157. switch (howto->type)
  1158. {
  1159. case R_MSP430_10_PCREL:
  1160. contents += rel->r_offset;
  1161. srel = (bfd_signed_vma) relocation;
  1162. srel += rel->r_addend;
  1163. srel -= rel->r_offset;
  1164. srel -= 2; /* Branch instructions add 2 to the PC... */
  1165. srel -= (input_section->output_section->vma +
  1166. input_section->output_offset);
  1167. if (srel & 1)
  1168. return bfd_reloc_outofrange;
  1169. /* MSP430 addresses commands as words. */
  1170. srel >>= 1;
  1171. /* Check for an overflow. */
  1172. if (srel < -512 || srel > 511)
  1173. {
  1174. if (info->disable_target_specific_optimizations < 0)
  1175. {
  1176. static bool warned = false;
  1177. if (! warned)
  1178. {
  1179. info->callbacks->warning
  1180. (info,
  1181. _("try enabling relaxation to avoid relocation truncations"),
  1182. NULL, input_bfd, input_section, relocation);
  1183. warned = true;
  1184. }
  1185. }
  1186. return bfd_reloc_overflow;
  1187. }
  1188. x = bfd_get_16 (input_bfd, contents);
  1189. x = (x & 0xfc00) | (srel & 0x3ff);
  1190. bfd_put_16 (input_bfd, x, contents);
  1191. break;
  1192. case R_MSP430_2X_PCREL:
  1193. contents += rel->r_offset;
  1194. srel = (bfd_signed_vma) relocation;
  1195. srel += rel->r_addend;
  1196. srel -= rel->r_offset;
  1197. srel -= 2; /* Branch instructions add 2 to the PC... */
  1198. srel -= (input_section->output_section->vma +
  1199. input_section->output_offset);
  1200. if (srel & 1)
  1201. return bfd_reloc_outofrange;
  1202. /* MSP430 addresses commands as words. */
  1203. srel >>= 1;
  1204. /* Check for an overflow. */
  1205. if (srel < -512 || srel > 511)
  1206. return bfd_reloc_overflow;
  1207. x = bfd_get_16 (input_bfd, contents);
  1208. x = (x & 0xfc00) | (srel & 0x3ff);
  1209. bfd_put_16 (input_bfd, x, contents);
  1210. /* Handle second jump instruction. */
  1211. x = bfd_get_16 (input_bfd, contents - 2);
  1212. srel += 1;
  1213. x = (x & 0xfc00) | (srel & 0x3ff);
  1214. bfd_put_16 (input_bfd, x, contents - 2);
  1215. break;
  1216. case R_MSP430_RL_PCREL:
  1217. case R_MSP430_16_PCREL:
  1218. contents += rel->r_offset;
  1219. srel = (bfd_signed_vma) relocation;
  1220. srel += rel->r_addend;
  1221. srel -= rel->r_offset;
  1222. /* Only branch instructions add 2 to the PC... */
  1223. srel -= (input_section->output_section->vma +
  1224. input_section->output_offset);
  1225. if (srel & 1)
  1226. return bfd_reloc_outofrange;
  1227. bfd_put_16 (input_bfd, srel & 0xffff, contents);
  1228. break;
  1229. case R_MSP430_16_PCREL_BYTE:
  1230. contents += rel->r_offset;
  1231. srel = (bfd_signed_vma) relocation;
  1232. srel += rel->r_addend;
  1233. srel -= rel->r_offset;
  1234. /* Only branch instructions add 2 to the PC... */
  1235. srel -= (input_section->output_section->vma +
  1236. input_section->output_offset);
  1237. bfd_put_16 (input_bfd, srel & 0xffff, contents);
  1238. break;
  1239. case R_MSP430_16_BYTE:
  1240. contents += rel->r_offset;
  1241. srel = (bfd_signed_vma) relocation;
  1242. srel += rel->r_addend;
  1243. bfd_put_16 (input_bfd, srel & 0xffff, contents);
  1244. break;
  1245. case R_MSP430_16:
  1246. contents += rel->r_offset;
  1247. srel = (bfd_signed_vma) relocation;
  1248. srel += rel->r_addend;
  1249. if (srel & 1)
  1250. return bfd_reloc_notsupported;
  1251. bfd_put_16 (input_bfd, srel & 0xffff, contents);
  1252. break;
  1253. case R_MSP430_8:
  1254. contents += rel->r_offset;
  1255. srel = (bfd_signed_vma) relocation;
  1256. srel += rel->r_addend;
  1257. bfd_put_8 (input_bfd, srel & 0xff, contents);
  1258. break;
  1259. case R_MSP430_SYM_DIFF:
  1260. case R_MSP430_GNU_SUB_ULEB128:
  1261. /* Cache the input section and value.
  1262. The offset is unreliable, since relaxation may
  1263. have reduced the following reloc's offset. */
  1264. sym_diff_section = input_section;
  1265. sym_diff_value = relocation + (howto->type == R_MSP430_GNU_SUB_ULEB128
  1266. ? rel->r_addend : 0);
  1267. return bfd_reloc_ok;
  1268. default:
  1269. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  1270. contents, rel->r_offset,
  1271. relocation, rel->r_addend);
  1272. }
  1273. return r;
  1274. }
  1275. /* Relocate an MSP430 ELF section. */
  1276. static int
  1277. elf32_msp430_relocate_section (bfd * output_bfd ATTRIBUTE_UNUSED,
  1278. struct bfd_link_info * info,
  1279. bfd * input_bfd,
  1280. asection * input_section,
  1281. bfd_byte * contents,
  1282. Elf_Internal_Rela * relocs,
  1283. Elf_Internal_Sym * local_syms,
  1284. asection ** local_sections)
  1285. {
  1286. Elf_Internal_Shdr *symtab_hdr;
  1287. struct elf_link_hash_entry **sym_hashes;
  1288. Elf_Internal_Rela *rel;
  1289. Elf_Internal_Rela *relend;
  1290. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  1291. sym_hashes = elf_sym_hashes (input_bfd);
  1292. relend = relocs + input_section->reloc_count;
  1293. for (rel = relocs; rel < relend; rel++)
  1294. {
  1295. reloc_howto_type *howto;
  1296. unsigned long r_symndx;
  1297. Elf_Internal_Sym *sym;
  1298. asection *sec;
  1299. struct elf_link_hash_entry *h;
  1300. bfd_vma relocation;
  1301. bfd_reloc_status_type r;
  1302. const char *name = NULL;
  1303. int r_type;
  1304. r_type = ELF32_R_TYPE (rel->r_info);
  1305. r_symndx = ELF32_R_SYM (rel->r_info);
  1306. if (uses_msp430x_relocs (input_bfd))
  1307. howto = elf_msp430x_howto_table + r_type;
  1308. else
  1309. howto = elf_msp430_howto_table + r_type;
  1310. h = NULL;
  1311. sym = NULL;
  1312. sec = NULL;
  1313. if (r_symndx < symtab_hdr->sh_info)
  1314. {
  1315. sym = local_syms + r_symndx;
  1316. sec = local_sections[r_symndx];
  1317. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  1318. name = bfd_elf_string_from_elf_section
  1319. (input_bfd, symtab_hdr->sh_link, sym->st_name);
  1320. name = name == NULL || *name == 0 ? bfd_section_name (sec) : name;
  1321. }
  1322. else
  1323. {
  1324. bool unresolved_reloc, warned, ignored;
  1325. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  1326. r_symndx, symtab_hdr, sym_hashes,
  1327. h, sec, relocation,
  1328. unresolved_reloc, warned, ignored);
  1329. name = h->root.root.string;
  1330. }
  1331. if (sec != NULL && discarded_section (sec))
  1332. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  1333. rel, 1, relend, howto, 0, contents);
  1334. if (bfd_link_relocatable (info))
  1335. continue;
  1336. r = msp430_final_link_relocate (howto, input_bfd, input_section,
  1337. contents, rel, relocation, info);
  1338. if (r != bfd_reloc_ok)
  1339. {
  1340. const char *msg = (const char *) NULL;
  1341. switch (r)
  1342. {
  1343. case bfd_reloc_overflow:
  1344. (*info->callbacks->reloc_overflow)
  1345. (info, (h ? &h->root : NULL), name, howto->name,
  1346. (bfd_vma) 0, input_bfd, input_section, rel->r_offset);
  1347. break;
  1348. case bfd_reloc_undefined:
  1349. (*info->callbacks->undefined_symbol)
  1350. (info, name, input_bfd, input_section, rel->r_offset, true);
  1351. break;
  1352. case bfd_reloc_outofrange:
  1353. msg = _("internal error: branch/jump to an odd address detected");
  1354. break;
  1355. case bfd_reloc_notsupported:
  1356. msg = _("internal error: unsupported relocation error");
  1357. break;
  1358. case bfd_reloc_dangerous:
  1359. msg = _("internal error: dangerous relocation");
  1360. break;
  1361. default:
  1362. msg = _("internal error: unknown error");
  1363. break;
  1364. }
  1365. if (msg)
  1366. (*info->callbacks->warning) (info, msg, name, input_bfd,
  1367. input_section, rel->r_offset);
  1368. }
  1369. }
  1370. return true;
  1371. }
  1372. /* The final processing done just before writing out a MSP430 ELF object
  1373. file. This gets the MSP430 architecture right based on the machine
  1374. number. */
  1375. static bool
  1376. bfd_elf_msp430_final_write_processing (bfd *abfd)
  1377. {
  1378. unsigned long val;
  1379. switch (bfd_get_mach (abfd))
  1380. {
  1381. default:
  1382. case bfd_mach_msp110: val = E_MSP430_MACH_MSP430x11x1; break;
  1383. case bfd_mach_msp11: val = E_MSP430_MACH_MSP430x11; break;
  1384. case bfd_mach_msp12: val = E_MSP430_MACH_MSP430x12; break;
  1385. case bfd_mach_msp13: val = E_MSP430_MACH_MSP430x13; break;
  1386. case bfd_mach_msp14: val = E_MSP430_MACH_MSP430x14; break;
  1387. case bfd_mach_msp15: val = E_MSP430_MACH_MSP430x15; break;
  1388. case bfd_mach_msp16: val = E_MSP430_MACH_MSP430x16; break;
  1389. case bfd_mach_msp31: val = E_MSP430_MACH_MSP430x31; break;
  1390. case bfd_mach_msp32: val = E_MSP430_MACH_MSP430x32; break;
  1391. case bfd_mach_msp33: val = E_MSP430_MACH_MSP430x33; break;
  1392. case bfd_mach_msp41: val = E_MSP430_MACH_MSP430x41; break;
  1393. case bfd_mach_msp42: val = E_MSP430_MACH_MSP430x42; break;
  1394. case bfd_mach_msp43: val = E_MSP430_MACH_MSP430x43; break;
  1395. case bfd_mach_msp44: val = E_MSP430_MACH_MSP430x44; break;
  1396. case bfd_mach_msp20: val = E_MSP430_MACH_MSP430x20; break;
  1397. case bfd_mach_msp22: val = E_MSP430_MACH_MSP430x22; break;
  1398. case bfd_mach_msp23: val = E_MSP430_MACH_MSP430x23; break;
  1399. case bfd_mach_msp24: val = E_MSP430_MACH_MSP430x24; break;
  1400. case bfd_mach_msp26: val = E_MSP430_MACH_MSP430x26; break;
  1401. case bfd_mach_msp46: val = E_MSP430_MACH_MSP430x46; break;
  1402. case bfd_mach_msp47: val = E_MSP430_MACH_MSP430x47; break;
  1403. case bfd_mach_msp54: val = E_MSP430_MACH_MSP430x54; break;
  1404. case bfd_mach_msp430x: val = E_MSP430_MACH_MSP430X; break;
  1405. }
  1406. elf_elfheader (abfd)->e_machine = EM_MSP430;
  1407. elf_elfheader (abfd)->e_flags &= ~EF_MSP430_MACH;
  1408. elf_elfheader (abfd)->e_flags |= val;
  1409. return _bfd_elf_final_write_processing (abfd);
  1410. }
  1411. /* Set the right machine number. */
  1412. static bool
  1413. elf32_msp430_object_p (bfd * abfd)
  1414. {
  1415. int e_set = bfd_mach_msp14;
  1416. if (elf_elfheader (abfd)->e_machine == EM_MSP430
  1417. || elf_elfheader (abfd)->e_machine == EM_MSP430_OLD)
  1418. {
  1419. int e_mach = elf_elfheader (abfd)->e_flags & EF_MSP430_MACH;
  1420. switch (e_mach)
  1421. {
  1422. default:
  1423. case E_MSP430_MACH_MSP430x11: e_set = bfd_mach_msp11; break;
  1424. case E_MSP430_MACH_MSP430x11x1: e_set = bfd_mach_msp110; break;
  1425. case E_MSP430_MACH_MSP430x12: e_set = bfd_mach_msp12; break;
  1426. case E_MSP430_MACH_MSP430x13: e_set = bfd_mach_msp13; break;
  1427. case E_MSP430_MACH_MSP430x14: e_set = bfd_mach_msp14; break;
  1428. case E_MSP430_MACH_MSP430x15: e_set = bfd_mach_msp15; break;
  1429. case E_MSP430_MACH_MSP430x16: e_set = bfd_mach_msp16; break;
  1430. case E_MSP430_MACH_MSP430x31: e_set = bfd_mach_msp31; break;
  1431. case E_MSP430_MACH_MSP430x32: e_set = bfd_mach_msp32; break;
  1432. case E_MSP430_MACH_MSP430x33: e_set = bfd_mach_msp33; break;
  1433. case E_MSP430_MACH_MSP430x41: e_set = bfd_mach_msp41; break;
  1434. case E_MSP430_MACH_MSP430x42: e_set = bfd_mach_msp42; break;
  1435. case E_MSP430_MACH_MSP430x43: e_set = bfd_mach_msp43; break;
  1436. case E_MSP430_MACH_MSP430x44: e_set = bfd_mach_msp44; break;
  1437. case E_MSP430_MACH_MSP430x20: e_set = bfd_mach_msp20; break;
  1438. case E_MSP430_MACH_MSP430x22: e_set = bfd_mach_msp22; break;
  1439. case E_MSP430_MACH_MSP430x23: e_set = bfd_mach_msp23; break;
  1440. case E_MSP430_MACH_MSP430x24: e_set = bfd_mach_msp24; break;
  1441. case E_MSP430_MACH_MSP430x26: e_set = bfd_mach_msp26; break;
  1442. case E_MSP430_MACH_MSP430x46: e_set = bfd_mach_msp46; break;
  1443. case E_MSP430_MACH_MSP430x47: e_set = bfd_mach_msp47; break;
  1444. case E_MSP430_MACH_MSP430x54: e_set = bfd_mach_msp54; break;
  1445. case E_MSP430_MACH_MSP430X: e_set = bfd_mach_msp430x; break;
  1446. }
  1447. }
  1448. return bfd_default_set_arch_mach (abfd, bfd_arch_msp430, e_set);
  1449. }
  1450. /* These functions handle relaxing for the msp430.
  1451. Relaxation required only in two cases:
  1452. - Bad hand coding like jumps from one section to another or
  1453. from file to file.
  1454. - Sibling calls. This will affect only 'jump label' polymorph. Without
  1455. relaxing this enlarges code by 2 bytes. Sibcalls implemented but
  1456. do not work in gcc's port by the reason I do not know.
  1457. - To convert out of range conditional jump instructions (found inside
  1458. a function) into inverted jumps over an unconditional branch instruction.
  1459. Anyway, if a relaxation required, user should pass -relax option to the
  1460. linker.
  1461. There are quite a few relaxing opportunities available on the msp430:
  1462. ================================================================
  1463. 1. 3 words -> 1 word
  1464. eq == jeq label jne +4; br lab
  1465. ne != jne label jeq +4; br lab
  1466. lt < jl label jge +4; br lab
  1467. ltu < jlo label lhs +4; br lab
  1468. ge >= jge label jl +4; br lab
  1469. geu >= jhs label jlo +4; br lab
  1470. 2. 4 words -> 1 word
  1471. ltn < jn jn +2; jmp +4; br lab
  1472. 3. 4 words -> 2 words
  1473. gt > jeq +2; jge label jeq +6; jl +4; br label
  1474. gtu > jeq +2; jhs label jeq +6; jlo +4; br label
  1475. 4. 4 words -> 2 words and 2 labels
  1476. leu <= jeq label; jlo label jeq +2; jhs +4; br label
  1477. le <= jeq label; jl label jeq +2; jge +4; br label
  1478. =================================================================
  1479. codemap for first cases is (labels masked ):
  1480. eq: 0x2002,0x4010,0x0000 -> 0x2400
  1481. ne: 0x2402,0x4010,0x0000 -> 0x2000
  1482. lt: 0x3402,0x4010,0x0000 -> 0x3800
  1483. ltu: 0x2c02,0x4010,0x0000 -> 0x2800
  1484. ge: 0x3802,0x4010,0x0000 -> 0x3400
  1485. geu: 0x2802,0x4010,0x0000 -> 0x2c00
  1486. second case:
  1487. ltn: 0x3001,0x3c02,0x4010,0x0000 -> 0x3000
  1488. third case:
  1489. gt: 0x2403,0x3802,0x4010,0x0000 -> 0x2401,0x3400
  1490. gtu: 0x2403,0x2802,0x4010,0x0000 -> 0x2401,0x2c00
  1491. fourth case:
  1492. leu: 0x2401,0x2c02,0x4010,0x0000 -> 0x2400,0x2800
  1493. le: 0x2401,0x3402,0x4010,0x0000 -> 0x2400,0x3800
  1494. Unspecified case :)
  1495. jump: 0x4010,0x0000 -> 0x3c00. */
  1496. #define NUMB_RELAX_CODES 12
  1497. static struct rcodes_s
  1498. {
  1499. int f0, f1; /* From code. */
  1500. int t0, t1; /* To code. */
  1501. int labels; /* Position of labels: 1 - one label at first
  1502. word, 2 - one at second word, 3 - two
  1503. labels at both. */
  1504. int cdx; /* Words to match. */
  1505. int bs; /* Shrink bytes. */
  1506. int off; /* Offset from old label for new code. */
  1507. int ncl; /* New code length. */
  1508. } rcode[] =
  1509. {/* lab,cdx,bs,off,ncl */
  1510. { 0x0000, 0x0000, 0x3c00, 0x0000, 1, 0, 2, 2, 2}, /* jump */
  1511. { 0x0000, 0x2002, 0x2400, 0x0000, 1, 1, 4, 4, 2}, /* eq */
  1512. { 0x0000, 0x2402, 0x2000, 0x0000, 1, 1, 4, 4, 2}, /* ne */
  1513. { 0x0000, 0x3402, 0x3800, 0x0000, 1, 1, 4, 4, 2}, /* lt */
  1514. { 0x0000, 0x2c02, 0x2800, 0x0000, 1, 1, 4, 4, 2}, /* ltu */
  1515. { 0x0000, 0x3802, 0x3400, 0x0000, 1, 1, 4, 4, 2}, /* ge */
  1516. { 0x0000, 0x2802, 0x2c00, 0x0000, 1, 1, 4, 4, 2}, /* geu */
  1517. { 0x3001, 0x3c02, 0x3000, 0x0000, 1, 2, 6, 6, 2}, /* ltn */
  1518. { 0x2403, 0x3802, 0x2401, 0x3400, 2, 2, 4, 6, 4}, /* gt */
  1519. { 0x2403, 0x2802, 0x2401, 0x2c00, 2, 2, 4, 6, 4}, /* gtu */
  1520. { 0x2401, 0x2c02, 0x2400, 0x2800, 3, 2, 4, 6, 4}, /* leu , 2 labels */
  1521. { 0x2401, 0x2c02, 0x2400, 0x2800, 3, 2, 4, 6, 4}, /* le , 2 labels */
  1522. { 0, 0, 0, 0, 0, 0, 0, 0, 0}
  1523. };
  1524. /* Return TRUE if a symbol exists at the given address. */
  1525. static bool
  1526. msp430_elf_symbol_address_p (bfd * abfd,
  1527. asection * sec,
  1528. Elf_Internal_Sym * isym,
  1529. bfd_vma addr)
  1530. {
  1531. Elf_Internal_Shdr *symtab_hdr;
  1532. unsigned int sec_shndx;
  1533. Elf_Internal_Sym *isymend;
  1534. struct elf_link_hash_entry **sym_hashes;
  1535. struct elf_link_hash_entry **end_hashes;
  1536. unsigned int symcount;
  1537. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  1538. /* Examine all the local symbols. */
  1539. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1540. for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
  1541. if (isym->st_shndx == sec_shndx && isym->st_value == addr)
  1542. return true;
  1543. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  1544. - symtab_hdr->sh_info);
  1545. sym_hashes = elf_sym_hashes (abfd);
  1546. end_hashes = sym_hashes + symcount;
  1547. for (; sym_hashes < end_hashes; sym_hashes++)
  1548. {
  1549. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  1550. if ((sym_hash->root.type == bfd_link_hash_defined
  1551. || sym_hash->root.type == bfd_link_hash_defweak)
  1552. && sym_hash->root.u.def.section == sec
  1553. && sym_hash->root.u.def.value == addr)
  1554. return true;
  1555. }
  1556. return false;
  1557. }
  1558. /* Adjust all local symbols defined as '.section + 0xXXXX' (.section has
  1559. sec_shndx) referenced from current and other sections. */
  1560. static bool
  1561. msp430_elf_relax_adjust_locals (bfd * abfd, asection * sec, bfd_vma addr,
  1562. int count, unsigned int sec_shndx,
  1563. bfd_vma toaddr)
  1564. {
  1565. Elf_Internal_Shdr *symtab_hdr;
  1566. Elf_Internal_Rela *irel;
  1567. Elf_Internal_Rela *irelend;
  1568. Elf_Internal_Sym *isym;
  1569. irel = elf_section_data (sec)->relocs;
  1570. if (irel == NULL)
  1571. return true;
  1572. irelend = irel + sec->reloc_count;
  1573. symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
  1574. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  1575. for (;irel < irelend; irel++)
  1576. {
  1577. unsigned int sidx = ELF32_R_SYM(irel->r_info);
  1578. Elf_Internal_Sym *lsym = isym + sidx;
  1579. /* Adjust symbols referenced by .sec+0xXX. */
  1580. if (irel->r_addend > addr && irel->r_addend < toaddr
  1581. && sidx < symtab_hdr->sh_info
  1582. && lsym->st_shndx == sec_shndx)
  1583. irel->r_addend -= count;
  1584. }
  1585. return true;
  1586. }
  1587. /* Delete some bytes from a section while relaxing. */
  1588. static bool
  1589. msp430_elf_relax_delete_bytes (bfd * abfd, asection * sec, bfd_vma addr,
  1590. int count)
  1591. {
  1592. Elf_Internal_Shdr *symtab_hdr;
  1593. unsigned int sec_shndx;
  1594. bfd_byte *contents;
  1595. Elf_Internal_Rela *irel;
  1596. Elf_Internal_Rela *irelend;
  1597. bfd_vma toaddr;
  1598. Elf_Internal_Sym *isym;
  1599. Elf_Internal_Sym *isymend;
  1600. struct elf_link_hash_entry **sym_hashes;
  1601. struct elf_link_hash_entry **end_hashes;
  1602. unsigned int symcount;
  1603. asection *p;
  1604. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  1605. contents = elf_section_data (sec)->this_hdr.contents;
  1606. toaddr = sec->size;
  1607. if (debug_relocs)
  1608. printf (" deleting %d bytes between 0x%lx to 0x%lx\n",
  1609. count, (long) addr, (long) toaddr);
  1610. irel = elf_section_data (sec)->relocs;
  1611. irelend = irel + sec->reloc_count;
  1612. /* Actually delete the bytes. */
  1613. memmove (contents + addr, contents + addr + count,
  1614. (size_t) (toaddr - addr - count));
  1615. sec->size -= count;
  1616. /* Adjust all the relocs. */
  1617. symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
  1618. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  1619. for (; irel < irelend; irel++)
  1620. {
  1621. /* Get the new reloc address. */
  1622. if ((irel->r_offset > addr && irel->r_offset < toaddr))
  1623. irel->r_offset -= count;
  1624. }
  1625. for (p = abfd->sections; p != NULL; p = p->next)
  1626. msp430_elf_relax_adjust_locals (abfd,p,addr,count,sec_shndx,toaddr);
  1627. /* Adjust the local symbols defined in this section. */
  1628. symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
  1629. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  1630. for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
  1631. {
  1632. const char * name;
  1633. name = bfd_elf_string_from_elf_section
  1634. (abfd, symtab_hdr->sh_link, isym->st_name);
  1635. name = name == NULL || *name == 0 ? bfd_section_name (sec) : name;
  1636. if (isym->st_shndx != sec_shndx)
  1637. continue;
  1638. if (isym->st_value > addr
  1639. && (isym->st_value < toaddr
  1640. /* We also adjust a symbol at the end of the section if its name is
  1641. on the list below. These symbols are used for debug info
  1642. generation and they refer to the end of the current section, not
  1643. the start of the next section. */
  1644. || (isym->st_value == toaddr
  1645. && name != NULL
  1646. && (startswith (name, ".Letext")
  1647. || startswith (name, ".LFE")))))
  1648. {
  1649. if (debug_relocs)
  1650. printf (" adjusting value of local symbol %s from 0x%lx ",
  1651. name, (long) isym->st_value);
  1652. if (isym->st_value < addr + count)
  1653. isym->st_value = addr;
  1654. else
  1655. isym->st_value -= count;
  1656. if (debug_relocs)
  1657. printf ("to 0x%lx\n", (long) isym->st_value);
  1658. }
  1659. /* Adjust the function symbol's size as well. */
  1660. else if (ELF_ST_TYPE (isym->st_info) == STT_FUNC
  1661. && isym->st_value + isym->st_size > addr
  1662. && isym->st_value + isym->st_size < toaddr)
  1663. isym->st_size -= count;
  1664. }
  1665. /* Now adjust the global symbols defined in this section. */
  1666. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  1667. - symtab_hdr->sh_info);
  1668. sym_hashes = elf_sym_hashes (abfd);
  1669. end_hashes = sym_hashes + symcount;
  1670. for (; sym_hashes < end_hashes; sym_hashes++)
  1671. {
  1672. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  1673. if ((sym_hash->root.type == bfd_link_hash_defined
  1674. || sym_hash->root.type == bfd_link_hash_defweak)
  1675. && sym_hash->root.u.def.section == sec
  1676. && sym_hash->root.u.def.value > addr
  1677. && sym_hash->root.u.def.value < toaddr)
  1678. {
  1679. if (sym_hash->root.u.def.value < addr + count)
  1680. sym_hash->root.u.def.value = addr;
  1681. else
  1682. sym_hash->root.u.def.value -= count;
  1683. }
  1684. /* Adjust the function symbol's size as well. */
  1685. else if (sym_hash->root.type == bfd_link_hash_defined
  1686. && sym_hash->root.u.def.section == sec
  1687. && sym_hash->type == STT_FUNC
  1688. && sym_hash->root.u.def.value + sym_hash->size > addr
  1689. && sym_hash->root.u.def.value + sym_hash->size < toaddr)
  1690. sym_hash->size -= count;
  1691. }
  1692. return true;
  1693. }
  1694. /* Insert one or two words into a section whilst relaxing. */
  1695. static bfd_byte *
  1696. msp430_elf_relax_add_words (bfd * abfd, asection * sec, bfd_vma addr,
  1697. int num_words, int word1, int word2)
  1698. {
  1699. Elf_Internal_Shdr *symtab_hdr;
  1700. unsigned int sec_shndx;
  1701. bfd_byte *contents;
  1702. Elf_Internal_Rela *irel;
  1703. Elf_Internal_Rela *irelend;
  1704. Elf_Internal_Sym *isym;
  1705. Elf_Internal_Sym *isymend;
  1706. struct elf_link_hash_entry **sym_hashes;
  1707. struct elf_link_hash_entry **end_hashes;
  1708. unsigned int symcount;
  1709. bfd_vma sec_end;
  1710. asection *p;
  1711. if (debug_relocs)
  1712. printf (" adding %d words at 0x%lx\n", num_words,
  1713. (long) (sec->output_section->vma + sec->output_offset + addr));
  1714. contents = elf_section_data (sec)->this_hdr.contents;
  1715. sec_end = sec->size;
  1716. int num_bytes = num_words * 2;
  1717. /* Make space for the new words. */
  1718. contents = bfd_realloc (contents, sec_end + num_bytes);
  1719. memmove (contents + addr + num_bytes, contents + addr, sec_end - addr);
  1720. /* Insert the new words. */
  1721. bfd_put_16 (abfd, word1, contents + addr);
  1722. if (num_words == 2)
  1723. bfd_put_16 (abfd, word2, contents + addr + 2);
  1724. /* Update the section information. */
  1725. sec->size += num_bytes;
  1726. elf_section_data (sec)->this_hdr.contents = contents;
  1727. /* Adjust all the relocs. */
  1728. irel = elf_section_data (sec)->relocs;
  1729. irelend = irel + sec->reloc_count;
  1730. for (; irel < irelend; irel++)
  1731. if ((irel->r_offset >= addr && irel->r_offset < sec_end))
  1732. irel->r_offset += num_bytes;
  1733. /* Adjust the local symbols defined in this section. */
  1734. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  1735. for (p = abfd->sections; p != NULL; p = p->next)
  1736. msp430_elf_relax_adjust_locals (abfd, p, addr, -num_bytes,
  1737. sec_shndx, sec_end);
  1738. /* Adjust the global symbols affected by the move. */
  1739. symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
  1740. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  1741. for (isymend = isym + symtab_hdr->sh_info; isym < isymend; isym++)
  1742. if (isym->st_shndx == sec_shndx
  1743. && isym->st_value >= addr && isym->st_value < sec_end)
  1744. {
  1745. if (debug_relocs)
  1746. printf (" adjusting value of local symbol %s from 0x%lx to "
  1747. "0x%lx\n", bfd_elf_string_from_elf_section
  1748. (abfd, symtab_hdr->sh_link, isym->st_name),
  1749. (long) isym->st_value, (long)(isym->st_value + num_bytes));
  1750. isym->st_value += num_bytes;
  1751. }
  1752. /* Now adjust the global symbols defined in this section. */
  1753. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  1754. - symtab_hdr->sh_info);
  1755. sym_hashes = elf_sym_hashes (abfd);
  1756. end_hashes = sym_hashes + symcount;
  1757. for (; sym_hashes < end_hashes; sym_hashes++)
  1758. {
  1759. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  1760. if ((sym_hash->root.type == bfd_link_hash_defined
  1761. || sym_hash->root.type == bfd_link_hash_defweak)
  1762. && sym_hash->root.u.def.section == sec
  1763. && sym_hash->root.u.def.value >= addr
  1764. && sym_hash->root.u.def.value < sec_end)
  1765. sym_hash->root.u.def.value += num_bytes;
  1766. }
  1767. return contents;
  1768. }
  1769. static bool
  1770. msp430_elf_relax_section (bfd * abfd, asection * sec,
  1771. struct bfd_link_info * link_info,
  1772. bool * again)
  1773. {
  1774. Elf_Internal_Shdr * symtab_hdr;
  1775. Elf_Internal_Rela * internal_relocs;
  1776. Elf_Internal_Rela * irel;
  1777. Elf_Internal_Rela * irelend;
  1778. bfd_byte * contents = NULL;
  1779. Elf_Internal_Sym * isymbuf = NULL;
  1780. /* Assume nothing changes. */
  1781. *again = false;
  1782. /* We don't have to do anything for a relocatable link, if
  1783. this section does not have relocs, or if this is not a
  1784. code section. */
  1785. if (bfd_link_relocatable (link_info)
  1786. || (sec->flags & SEC_RELOC) == 0
  1787. || sec->reloc_count == 0 || (sec->flags & SEC_CODE) == 0)
  1788. return true;
  1789. if (debug_relocs)
  1790. printf ("Relaxing %s (%p), output_offset: 0x%lx sec size: 0x%lx\n",
  1791. sec->name, sec, (long) sec->output_offset, (long) sec->size);
  1792. symtab_hdr = & elf_tdata (abfd)->symtab_hdr;
  1793. /* Get a copy of the native relocations. */
  1794. internal_relocs =
  1795. _bfd_elf_link_read_relocs (abfd, sec, NULL, NULL, link_info->keep_memory);
  1796. if (internal_relocs == NULL)
  1797. goto error_return;
  1798. /* Walk through them looking for relaxing opportunities. */
  1799. irelend = internal_relocs + sec->reloc_count;
  1800. if (debug_relocs)
  1801. printf (" trying code size growing relocs\n");
  1802. /* Do code size growing relocs first. */
  1803. for (irel = internal_relocs; irel < irelend; irel++)
  1804. {
  1805. bfd_vma symval;
  1806. /* If this isn't something that can be relaxed, then ignore
  1807. this reloc. */
  1808. if (uses_msp430x_relocs (abfd)
  1809. && ELF32_R_TYPE (irel->r_info) == (int) R_MSP430X_10_PCREL)
  1810. ;
  1811. else if (! uses_msp430x_relocs (abfd)
  1812. && ELF32_R_TYPE (irel->r_info) == (int) R_MSP430_10_PCREL)
  1813. ;
  1814. else
  1815. continue;
  1816. /* Get the section contents if we haven't done so already. */
  1817. if (contents == NULL)
  1818. {
  1819. /* Get cached copy if it exists. */
  1820. if (elf_section_data (sec)->this_hdr.contents != NULL)
  1821. contents = elf_section_data (sec)->this_hdr.contents;
  1822. else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
  1823. goto error_return;
  1824. }
  1825. /* Read this BFD's local symbols if we haven't done so already. */
  1826. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  1827. {
  1828. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  1829. if (isymbuf == NULL)
  1830. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  1831. symtab_hdr->sh_info, 0,
  1832. NULL, NULL, NULL);
  1833. if (isymbuf == NULL)
  1834. goto error_return;
  1835. }
  1836. /* Get the value of the symbol referred to by the reloc. */
  1837. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  1838. {
  1839. /* A local symbol. */
  1840. Elf_Internal_Sym *isym;
  1841. asection *sym_sec;
  1842. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  1843. if (isym->st_shndx == SHN_UNDEF)
  1844. sym_sec = bfd_und_section_ptr;
  1845. else if (isym->st_shndx == SHN_ABS)
  1846. sym_sec = bfd_abs_section_ptr;
  1847. else if (isym->st_shndx == SHN_COMMON)
  1848. sym_sec = bfd_com_section_ptr;
  1849. else
  1850. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  1851. symval = (isym->st_value
  1852. + sym_sec->output_section->vma + sym_sec->output_offset);
  1853. if (debug_relocs)
  1854. printf (" processing reloc at 0x%lx for local sym: %s "
  1855. "st_value: 0x%lx adj value: 0x%lx\n",
  1856. (long) (sec->output_offset + sec->output_section->vma
  1857. + irel->r_offset),
  1858. bfd_elf_string_from_elf_section (abfd, symtab_hdr->sh_link,
  1859. isym->st_name),
  1860. (long) isym->st_value, (long) symval);
  1861. }
  1862. else
  1863. {
  1864. unsigned long indx;
  1865. struct elf_link_hash_entry *h;
  1866. /* An external symbol. */
  1867. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  1868. h = elf_sym_hashes (abfd)[indx];
  1869. BFD_ASSERT (h != NULL);
  1870. if (h->root.type != bfd_link_hash_defined
  1871. && h->root.type != bfd_link_hash_defweak)
  1872. /* This appears to be a reference to an undefined
  1873. symbol. Just ignore it--it will be caught by the
  1874. regular reloc processing. */
  1875. continue;
  1876. symval = (h->root.u.def.value
  1877. + h->root.u.def.section->output_section->vma
  1878. + h->root.u.def.section->output_offset);
  1879. if (debug_relocs)
  1880. printf (" processing reloc at 0x%lx for global sym: %s "
  1881. "st_value: 0x%lx adj value: 0x%lx\n",
  1882. (long) (sec->output_offset + sec->output_section->vma
  1883. + irel->r_offset),
  1884. h->root.root.string, (long) h->root.u.def.value,
  1885. (long) symval);
  1886. }
  1887. /* For simplicity of coding, we are going to modify the section
  1888. contents, the section relocs, and the BFD symbol table. We
  1889. must tell the rest of the code not to free up this
  1890. information. It would be possible to instead create a table
  1891. of changes which have to be made, as is done in coff-mips.c;
  1892. that would be more work, but would require less memory when
  1893. the linker is run. */
  1894. bfd_signed_vma value = symval;
  1895. int opcode;
  1896. /* Compute the value that will be relocated. */
  1897. value += irel->r_addend;
  1898. /* Convert to PC relative. */
  1899. value -= (sec->output_section->vma + sec->output_offset);
  1900. value -= irel->r_offset;
  1901. value -= 2;
  1902. /* Scale. */
  1903. value >>= 1;
  1904. /* If it is in range then no modifications are needed. */
  1905. if (value >= -512 && value <= 511)
  1906. continue;
  1907. /* Get the opcode. */
  1908. opcode = bfd_get_16 (abfd, contents + irel->r_offset);
  1909. /* Compute the new opcode. We are going to convert:
  1910. JMP label
  1911. into:
  1912. BR[A] label
  1913. or
  1914. J<cond> label
  1915. into:
  1916. J<inv-cond> 1f
  1917. BR[A] #label
  1918. 1: */
  1919. switch (opcode & 0xfc00)
  1920. {
  1921. case 0x3800: opcode = 0x3402; break; /* Jl -> Jge +2 */
  1922. case 0x3400: opcode = 0x3802; break; /* Jge -> Jl +2 */
  1923. case 0x2c00: opcode = 0x2802; break; /* Jhs -> Jlo +2 */
  1924. case 0x2800: opcode = 0x2c02; break; /* Jlo -> Jhs +2 */
  1925. case 0x2400: opcode = 0x2002; break; /* Jeq -> Jne +2 */
  1926. case 0x2000: opcode = 0x2402; break; /* jne -> Jeq +2 */
  1927. case 0x3000: /* jn */
  1928. /* There is no direct inverse of the Jn insn.
  1929. FIXME: we could do this as:
  1930. Jn 1f
  1931. br 2f
  1932. 1: br label
  1933. 2: */
  1934. continue;
  1935. case 0x3c00:
  1936. if (uses_msp430x_relocs (abfd))
  1937. opcode = 0x0080; /* JMP -> BRA */
  1938. else
  1939. opcode = 0x4030; /* JMP -> BR */
  1940. break;
  1941. default:
  1942. /* Unhandled branch instruction. */
  1943. /* fprintf (stderr, "unrecog: %x\n", opcode); */
  1944. continue;
  1945. }
  1946. /* Note that we've changed the relocs, section contents, etc. */
  1947. elf_section_data (sec)->relocs = internal_relocs;
  1948. elf_section_data (sec)->this_hdr.contents = contents;
  1949. symtab_hdr->contents = (unsigned char *) isymbuf;
  1950. /* Install the new opcode. */
  1951. bfd_put_16 (abfd, opcode, contents + irel->r_offset);
  1952. /* Insert the new branch instruction. */
  1953. if (uses_msp430x_relocs (abfd))
  1954. {
  1955. if (debug_relocs)
  1956. printf (" R_MSP430X_10_PCREL -> R_MSP430X_ABS20_ADR_SRC "
  1957. "(growing with new opcode 0x%x)\n", opcode);
  1958. /* Insert an absolute branch (aka MOVA) instruction.
  1959. Note that bits 19:16 of the address are stored in the first word
  1960. of the insn, so this is where r_offset will point to. */
  1961. if (opcode == 0x0080)
  1962. {
  1963. /* If we're inserting a BRA because we are converting from a JMP,
  1964. then only add one word for destination address; the BRA opcode
  1965. has already been written. */
  1966. contents = msp430_elf_relax_add_words
  1967. (abfd, sec, irel->r_offset + 2, 1, 0x0000, 0);
  1968. }
  1969. else
  1970. {
  1971. contents = msp430_elf_relax_add_words
  1972. (abfd, sec, irel->r_offset + 2, 2, 0x0080, 0x0000);
  1973. /* Update the relocation to point to the inserted branch
  1974. instruction. Note - we are changing a PC-relative reloc
  1975. into an absolute reloc, but this is OK because we have
  1976. arranged with the assembler to have the reloc's value be
  1977. a (local) symbol, not a section+offset value. */
  1978. irel->r_offset += 2;
  1979. }
  1980. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  1981. R_MSP430X_ABS20_ADR_SRC);
  1982. }
  1983. else
  1984. {
  1985. if (debug_relocs)
  1986. printf (" R_MSP430_10_PCREL -> R_MSP430_16 "
  1987. "(growing with new opcode 0x%x)\n", opcode);
  1988. if (opcode == 0x4030)
  1989. {
  1990. /* If we're inserting a BR because we are converting from a JMP,
  1991. then only add one word for destination address; the BR opcode
  1992. has already been written. */
  1993. contents = msp430_elf_relax_add_words
  1994. (abfd, sec, irel->r_offset + 2, 1, 0x0000, 0);
  1995. irel->r_offset += 2;
  1996. }
  1997. else
  1998. {
  1999. contents = msp430_elf_relax_add_words
  2000. (abfd, sec, irel->r_offset + 2, 2, 0x4030, 0x0000);
  2001. /* See comment above about converting a 10-bit PC-rel
  2002. relocation into a 16-bit absolute relocation. */
  2003. irel->r_offset += 4;
  2004. }
  2005. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  2006. R_MSP430_16);
  2007. }
  2008. /* Growing the section may mean that other
  2009. conditional branches need to be fixed. */
  2010. *again = true;
  2011. }
  2012. if (debug_relocs)
  2013. printf (" trying code size shrinking relocs\n");
  2014. for (irel = internal_relocs; irel < irelend; irel++)
  2015. {
  2016. bfd_vma symval;
  2017. /* Get the section contents if we haven't done so already. */
  2018. if (contents == NULL)
  2019. {
  2020. /* Get cached copy if it exists. */
  2021. if (elf_section_data (sec)->this_hdr.contents != NULL)
  2022. contents = elf_section_data (sec)->this_hdr.contents;
  2023. else if (! bfd_malloc_and_get_section (abfd, sec, &contents))
  2024. goto error_return;
  2025. }
  2026. /* Read this BFD's local symbols if we haven't done so already. */
  2027. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  2028. {
  2029. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  2030. if (isymbuf == NULL)
  2031. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  2032. symtab_hdr->sh_info, 0,
  2033. NULL, NULL, NULL);
  2034. if (isymbuf == NULL)
  2035. goto error_return;
  2036. }
  2037. /* Get the value of the symbol referred to by the reloc. */
  2038. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  2039. {
  2040. /* A local symbol. */
  2041. Elf_Internal_Sym *isym;
  2042. asection *sym_sec;
  2043. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  2044. if (isym->st_shndx == SHN_UNDEF)
  2045. sym_sec = bfd_und_section_ptr;
  2046. else if (isym->st_shndx == SHN_ABS)
  2047. sym_sec = bfd_abs_section_ptr;
  2048. else if (isym->st_shndx == SHN_COMMON)
  2049. sym_sec = bfd_com_section_ptr;
  2050. else
  2051. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  2052. symval = (isym->st_value
  2053. + sym_sec->output_section->vma + sym_sec->output_offset);
  2054. if (debug_relocs)
  2055. printf (" processing reloc at 0x%lx for local sym: %s "
  2056. "st_value: 0x%lx adj value: 0x%lx\n",
  2057. (long) (sec->output_offset + sec->output_section->vma
  2058. + irel->r_offset),
  2059. bfd_elf_string_from_elf_section
  2060. (abfd, symtab_hdr->sh_link, isym->st_name),
  2061. (long) isym->st_value, (long) symval);
  2062. }
  2063. else
  2064. {
  2065. unsigned long indx;
  2066. struct elf_link_hash_entry *h;
  2067. /* An external symbol. */
  2068. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  2069. h = elf_sym_hashes (abfd)[indx];
  2070. BFD_ASSERT (h != NULL);
  2071. if (h->root.type != bfd_link_hash_defined
  2072. && h->root.type != bfd_link_hash_defweak)
  2073. /* This appears to be a reference to an undefined
  2074. symbol. Just ignore it--it will be caught by the
  2075. regular reloc processing. */
  2076. continue;
  2077. symval = (h->root.u.def.value
  2078. + h->root.u.def.section->output_section->vma
  2079. + h->root.u.def.section->output_offset);
  2080. if (debug_relocs)
  2081. printf (" processing reloc at 0x%lx for global sym: %s "
  2082. "st_value: 0x%lx adj value: 0x%lx\n", (long)
  2083. (sec->output_offset + sec->output_section->vma
  2084. + irel->r_offset),
  2085. h->root.root.string, (long) h->root.u.def.value,
  2086. (long) symval);
  2087. }
  2088. /* For simplicity of coding, we are going to modify the section
  2089. contents, the section relocs, and the BFD symbol table. We
  2090. must tell the rest of the code not to free up this
  2091. information. It would be possible to instead create a table
  2092. of changes which have to be made, as is done in coff-mips.c;
  2093. that would be more work, but would require less memory when
  2094. the linker is run. */
  2095. /* Try to turn a 16bit pc-relative branch into a 10bit pc-relative
  2096. branch. */
  2097. /* Paranoia? paranoia... */
  2098. if (! uses_msp430x_relocs (abfd)
  2099. && ELF32_R_TYPE (irel->r_info) == (int) R_MSP430_RL_PCREL)
  2100. {
  2101. bfd_vma value = symval;
  2102. /* Deal with pc-relative gunk. */
  2103. value -= (sec->output_section->vma + sec->output_offset);
  2104. value -= irel->r_offset;
  2105. value += irel->r_addend;
  2106. /* See if the value will fit in 10 bits, note the high value is
  2107. 1016 as the target will be two bytes closer if we are
  2108. able to relax. */
  2109. if ((long) value < 1016 && (long) value > -1016)
  2110. {
  2111. int code0 = 0, code1 = 0, code2 = 0;
  2112. int i;
  2113. struct rcodes_s *rx;
  2114. /* Get the opcode. */
  2115. if (irel->r_offset >= 6)
  2116. code0 = bfd_get_16 (abfd, contents + irel->r_offset - 6);
  2117. if (irel->r_offset >= 4)
  2118. code1 = bfd_get_16 (abfd, contents + irel->r_offset - 4);
  2119. code2 = bfd_get_16 (abfd, contents + irel->r_offset - 2);
  2120. if (code2 != 0x4010)
  2121. continue;
  2122. /* Check r4 and r3. */
  2123. for (i = NUMB_RELAX_CODES - 1; i >= 0; i--)
  2124. {
  2125. rx = &rcode[i];
  2126. if (rx->cdx == 2 && rx->f0 == code0 && rx->f1 == code1)
  2127. break;
  2128. else if (rx->cdx == 1 && rx->f1 == code1)
  2129. break;
  2130. else if (rx->cdx == 0) /* This is an unconditional jump. */
  2131. break;
  2132. }
  2133. /* Check labels:
  2134. .Label0: ; we do not care about this label
  2135. jeq +6
  2136. .Label1: ; make sure there is no label here
  2137. jl +4
  2138. .Label2: ; make sure there is no label here
  2139. br .Label_dst
  2140. So, if there is .Label1 or .Label2 we cannot relax this code.
  2141. This actually should not happen, cause for relaxable
  2142. instructions we use RL_PCREL reloc instead of 16_PCREL.
  2143. Will change this in the future. */
  2144. if (rx->cdx > 0
  2145. && msp430_elf_symbol_address_p (abfd, sec, isymbuf,
  2146. irel->r_offset - 2))
  2147. continue;
  2148. if (rx->cdx > 1
  2149. && msp430_elf_symbol_address_p (abfd, sec, isymbuf,
  2150. irel->r_offset - 4))
  2151. continue;
  2152. /* Note that we've changed the relocs, section contents, etc. */
  2153. elf_section_data (sec)->relocs = internal_relocs;
  2154. elf_section_data (sec)->this_hdr.contents = contents;
  2155. symtab_hdr->contents = (unsigned char *) isymbuf;
  2156. if (debug_relocs)
  2157. printf (" R_MSP430_RL_PCREL -> ");
  2158. /* Fix the relocation's type. */
  2159. if (uses_msp430x_relocs (abfd))
  2160. {
  2161. if (rx->labels == 3) /* Handle special cases. */
  2162. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  2163. R_MSP430X_2X_PCREL);
  2164. else
  2165. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  2166. R_MSP430X_10_PCREL);
  2167. }
  2168. else
  2169. {
  2170. if (rx->labels == 3) /* Handle special cases. */
  2171. {
  2172. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  2173. R_MSP430_2X_PCREL);
  2174. if (debug_relocs)
  2175. printf ("R_MSP430_2X_PCREL (shrinking with new opcode"
  2176. " 0x%x)\n", rx->t0);
  2177. }
  2178. else
  2179. {
  2180. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  2181. R_MSP430_10_PCREL);
  2182. if (debug_relocs)
  2183. printf ("R_MSP430_10_PCREL (shrinking with new opcode"
  2184. " 0x%x)\n", rx->t0);
  2185. }
  2186. }
  2187. /* Fix the opcode right way. */
  2188. bfd_put_16 (abfd, rx->t0, contents + irel->r_offset - rx->off);
  2189. if (rx->t1)
  2190. bfd_put_16 (abfd, rx->t1,
  2191. contents + irel->r_offset - rx->off + 2);
  2192. /* Delete bytes. */
  2193. if (!msp430_elf_relax_delete_bytes (abfd, sec,
  2194. irel->r_offset - rx->off +
  2195. rx->ncl, rx->bs))
  2196. goto error_return;
  2197. /* Handle unconditional jumps. */
  2198. if (rx->cdx == 0)
  2199. irel->r_offset -= 2;
  2200. /* That will change things, so, we should relax again.
  2201. Note that this is not required, and it may be slow. */
  2202. *again = true;
  2203. }
  2204. }
  2205. /* Try to turn a 16-bit absolute branch into a 10-bit pc-relative
  2206. branch. */
  2207. if ((uses_msp430x_relocs (abfd)
  2208. && ELF32_R_TYPE (irel->r_info) == R_MSP430X_ABS16)
  2209. || (! uses_msp430x_relocs (abfd)
  2210. && ELF32_R_TYPE (irel->r_info) == R_MSP430_16))
  2211. {
  2212. bfd_vma value = symval;
  2213. value -= (sec->output_section->vma + sec->output_offset);
  2214. value -= irel->r_offset;
  2215. value += irel->r_addend;
  2216. /* See if the value will fit in 10 bits, note the high value is
  2217. 1016 as the target will be two bytes closer if we are
  2218. able to relax. */
  2219. if ((long) value < 1016 && (long) value > -1016)
  2220. {
  2221. int code1, code2, opcode;
  2222. /* Get the opcode. */
  2223. code2 = bfd_get_16 (abfd, contents + irel->r_offset - 2);
  2224. if (code2 != 0x4030) /* BR -> JMP */
  2225. continue;
  2226. /* FIXME: check r4 and r3 ? */
  2227. /* FIXME: Handle 0x4010 as well ? */
  2228. /* Note that we've changed the relocs, section contents, etc. */
  2229. elf_section_data (sec)->relocs = internal_relocs;
  2230. elf_section_data (sec)->this_hdr.contents = contents;
  2231. symtab_hdr->contents = (unsigned char *) isymbuf;
  2232. /* Fix the relocation's type. */
  2233. if (uses_msp430x_relocs (abfd))
  2234. {
  2235. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  2236. R_MSP430X_10_PCREL);
  2237. if (debug_relocs)
  2238. printf (" R_MSP430X_16 -> R_MSP430X_10_PCREL ");
  2239. }
  2240. else
  2241. {
  2242. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  2243. R_MSP430_10_PCREL);
  2244. if (debug_relocs)
  2245. printf (" R_MSP430_16 -> R_MSP430_10_PCREL ");
  2246. }
  2247. /* If we're trying to shrink a BR[A] after previously having
  2248. grown a JMP for this reloc, then we have a sequence like
  2249. this:
  2250. J<cond> 1f
  2251. BR[A]
  2252. 1:
  2253. The opcode for J<cond> has the target hard-coded as 2 words
  2254. ahead of the insn, instead of using a reloc.
  2255. This means we cannot rely on any of the helper functions to
  2256. update this hard-coded jump destination if we remove the
  2257. BR[A] insn, so we must explicitly update it here.
  2258. This does mean that we can remove the entire branch
  2259. instruction, and invert the conditional jump, saving us 4
  2260. bytes rather than only 2 if we detected this in the normal
  2261. way. */
  2262. code1 = bfd_get_16 (abfd, contents + irel->r_offset - 4);
  2263. switch (code1)
  2264. {
  2265. case 0x3802: opcode = 0x3401; break; /* Jl +2 -> Jge +1 */
  2266. case 0x3402: opcode = 0x3801; break; /* Jge +2 -> Jl +1 */
  2267. case 0x2c02: opcode = 0x2801; break; /* Jhs +2 -> Jlo +1 */
  2268. case 0x2802: opcode = 0x2c01; break; /* Jlo +2 -> Jhs +1 */
  2269. case 0x2402: opcode = 0x2001; break; /* Jeq +2 -> Jne +1 */
  2270. case 0x2002: opcode = 0x2401; break; /* jne +2 -> Jeq +1 */
  2271. case 0x3002: /* jn +2 */
  2272. /* FIXME: There is no direct inverse of the Jn insn. */
  2273. continue;
  2274. default:
  2275. /* The previous opcode does not have a hard-coded jump
  2276. that we added when previously relaxing, so relax the
  2277. current branch as normal. */
  2278. opcode = 0x3c00;
  2279. break;
  2280. }
  2281. if (debug_relocs)
  2282. printf ("(shrinking with new opcode 0x%x)\n", opcode);
  2283. if (opcode != 0x3c00)
  2284. {
  2285. /* Invert the opcode of the conditional jump. */
  2286. bfd_put_16 (abfd, opcode, contents + irel->r_offset - 4);
  2287. irel->r_offset -= 4;
  2288. /* Delete 4 bytes - the full BR insn. */
  2289. if (!msp430_elf_relax_delete_bytes (abfd, sec,
  2290. irel->r_offset + 2, 4))
  2291. goto error_return;
  2292. }
  2293. else
  2294. {
  2295. /* Fix the opcode right way. */
  2296. bfd_put_16 (abfd, opcode, contents + irel->r_offset - 2);
  2297. irel->r_offset -= 2;
  2298. /* Delete bytes. */
  2299. if (!msp430_elf_relax_delete_bytes (abfd, sec,
  2300. irel->r_offset + 2, 2))
  2301. goto error_return;
  2302. }
  2303. /* That will change things, so, we should relax again.
  2304. Note that this is not required, and it may be slow. */
  2305. *again = true;
  2306. }
  2307. }
  2308. }
  2309. if (isymbuf != NULL && symtab_hdr->contents != (unsigned char *) isymbuf)
  2310. {
  2311. if (!link_info->keep_memory)
  2312. free (isymbuf);
  2313. else
  2314. {
  2315. /* Cache the symbols for elf_link_input_bfd. */
  2316. symtab_hdr->contents = (unsigned char *) isymbuf;
  2317. }
  2318. }
  2319. if (contents != NULL
  2320. && elf_section_data (sec)->this_hdr.contents != contents)
  2321. {
  2322. if (!link_info->keep_memory)
  2323. free (contents);
  2324. else
  2325. {
  2326. /* Cache the section contents for elf_link_input_bfd. */
  2327. elf_section_data (sec)->this_hdr.contents = contents;
  2328. }
  2329. }
  2330. if (elf_section_data (sec)->relocs != internal_relocs)
  2331. free (internal_relocs);
  2332. return true;
  2333. error_return:
  2334. if (symtab_hdr->contents != (unsigned char *) isymbuf)
  2335. free (isymbuf);
  2336. if (elf_section_data (sec)->this_hdr.contents != contents)
  2337. free (contents);
  2338. if (elf_section_data (sec)->relocs != internal_relocs)
  2339. free (internal_relocs);
  2340. return false;
  2341. }
  2342. /* Handle an MSP430 specific section when reading an object file.
  2343. This is called when bfd_section_from_shdr finds a section with
  2344. an unknown type. */
  2345. static bool
  2346. elf32_msp430_section_from_shdr (bfd *abfd,
  2347. Elf_Internal_Shdr * hdr,
  2348. const char *name,
  2349. int shindex)
  2350. {
  2351. switch (hdr->sh_type)
  2352. {
  2353. case SHT_MSP430_SEC_FLAGS:
  2354. case SHT_MSP430_SYM_ALIASES:
  2355. case SHT_MSP430_ATTRIBUTES:
  2356. return _bfd_elf_make_section_from_shdr (abfd, hdr, name, shindex);
  2357. default:
  2358. return false;
  2359. }
  2360. }
  2361. static bool
  2362. elf32_msp430_obj_attrs_handle_unknown (bfd *abfd, int tag)
  2363. {
  2364. _bfd_error_handler
  2365. /* xgettext:c-format */
  2366. (_("warning: %pB: unknown MSPABI object attribute %d"),
  2367. abfd, tag);
  2368. return true;
  2369. }
  2370. /* Determine whether an object attribute tag takes an integer, a
  2371. string or both. */
  2372. static int
  2373. elf32_msp430_obj_attrs_arg_type (int tag)
  2374. {
  2375. if (tag == Tag_compatibility)
  2376. return ATTR_TYPE_FLAG_INT_VAL | ATTR_TYPE_FLAG_STR_VAL;
  2377. if (tag < 32)
  2378. return ATTR_TYPE_FLAG_INT_VAL;
  2379. return (tag & 1) != 0 ? ATTR_TYPE_FLAG_STR_VAL : ATTR_TYPE_FLAG_INT_VAL;
  2380. }
  2381. static inline const char *
  2382. isa_type (int isa)
  2383. {
  2384. switch (isa)
  2385. {
  2386. case 1: return "MSP430";
  2387. case 2: return "MSP430X";
  2388. default: return "unknown";
  2389. }
  2390. }
  2391. static inline const char *
  2392. code_model (int model)
  2393. {
  2394. switch (model)
  2395. {
  2396. case 1: return "small";
  2397. case 2: return "large";
  2398. default: return "unknown";
  2399. }
  2400. }
  2401. static inline const char *
  2402. data_model (int model)
  2403. {
  2404. switch (model)
  2405. {
  2406. case 1: return "small";
  2407. case 2: return "large";
  2408. case 3: return "restricted large";
  2409. default: return "unknown";
  2410. }
  2411. }
  2412. /* Merge MSPABI and GNU object attributes from IBFD into OBFD.
  2413. Raise an error if there are conflicting attributes. */
  2414. static bool
  2415. elf32_msp430_merge_msp430_attributes (bfd *ibfd, struct bfd_link_info *info)
  2416. {
  2417. bfd *obfd = info->output_bfd;
  2418. obj_attribute *in_msp_attr, *in_gnu_attr;
  2419. obj_attribute *out_msp_attr, *out_gnu_attr;
  2420. bool result = true;
  2421. static bfd * first_input_bfd = NULL;
  2422. /* Skip linker created files. */
  2423. if (ibfd->flags & BFD_LINKER_CREATED)
  2424. return true;
  2425. /* LTO can create temporary files for linking which may not have an attribute
  2426. section. */
  2427. if (ibfd->lto_output
  2428. && bfd_get_section_by_name (ibfd, ".MSP430.attributes") == NULL)
  2429. return true;
  2430. /* If this is the first real object just copy the attributes. */
  2431. if (!elf_known_obj_attributes_proc (obfd)[0].i)
  2432. {
  2433. _bfd_elf_copy_obj_attributes (ibfd, obfd);
  2434. out_msp_attr = elf_known_obj_attributes_proc (obfd);
  2435. /* Use the Tag_null value to indicate that
  2436. the attributes have been initialized. */
  2437. out_msp_attr[0].i = 1;
  2438. first_input_bfd = ibfd;
  2439. return true;
  2440. }
  2441. in_msp_attr = elf_known_obj_attributes_proc (ibfd);
  2442. out_msp_attr = elf_known_obj_attributes_proc (obfd);
  2443. in_gnu_attr = elf_known_obj_attributes (ibfd) [OBJ_ATTR_GNU];
  2444. out_gnu_attr = elf_known_obj_attributes (obfd) [OBJ_ATTR_GNU];
  2445. /* The ISAs must be the same. */
  2446. if (in_msp_attr[OFBA_MSPABI_Tag_ISA].i != out_msp_attr[OFBA_MSPABI_Tag_ISA].i)
  2447. {
  2448. _bfd_error_handler
  2449. /* xgettext:c-format */
  2450. (_("error: %pB uses %s instructions but %pB uses %s"),
  2451. ibfd, isa_type (in_msp_attr[OFBA_MSPABI_Tag_ISA].i),
  2452. first_input_bfd, isa_type (out_msp_attr[OFBA_MSPABI_Tag_ISA].i));
  2453. result = false;
  2454. }
  2455. /* The code models must be the same. */
  2456. if (in_msp_attr[OFBA_MSPABI_Tag_Code_Model].i
  2457. != out_msp_attr[OFBA_MSPABI_Tag_Code_Model].i)
  2458. {
  2459. _bfd_error_handler
  2460. /* xgettext:c-format */
  2461. (_("error: %pB uses the %s code model whereas %pB uses the %s code model"),
  2462. ibfd, code_model (in_msp_attr[OFBA_MSPABI_Tag_Code_Model].i),
  2463. first_input_bfd,
  2464. code_model (out_msp_attr[OFBA_MSPABI_Tag_Code_Model].i));
  2465. result = false;
  2466. }
  2467. /* The large code model is only supported by the MSP430X. */
  2468. if (in_msp_attr[OFBA_MSPABI_Tag_Code_Model].i == 2
  2469. && out_msp_attr[OFBA_MSPABI_Tag_ISA].i != 2)
  2470. {
  2471. _bfd_error_handler
  2472. /* xgettext:c-format */
  2473. (_("error: %pB uses the large code model but %pB uses MSP430 instructions"),
  2474. ibfd, first_input_bfd);
  2475. result = false;
  2476. }
  2477. /* The data models must be the same. */
  2478. if (in_msp_attr[OFBA_MSPABI_Tag_Data_Model].i
  2479. != out_msp_attr[OFBA_MSPABI_Tag_Data_Model].i)
  2480. {
  2481. _bfd_error_handler
  2482. /* xgettext:c-format */
  2483. (_("error: %pB uses the %s data model whereas %pB uses the %s data model"),
  2484. ibfd, data_model (in_msp_attr[OFBA_MSPABI_Tag_Data_Model].i),
  2485. first_input_bfd,
  2486. data_model (out_msp_attr[OFBA_MSPABI_Tag_Data_Model].i));
  2487. result = false;
  2488. }
  2489. /* The small code model requires the use of the small data model. */
  2490. if (in_msp_attr[OFBA_MSPABI_Tag_Code_Model].i == 1
  2491. && out_msp_attr[OFBA_MSPABI_Tag_Data_Model].i != 1)
  2492. {
  2493. _bfd_error_handler
  2494. /* xgettext:c-format */
  2495. (_("error: %pB uses the small code model but %pB uses the %s data model"),
  2496. ibfd, first_input_bfd,
  2497. data_model (out_msp_attr[OFBA_MSPABI_Tag_Data_Model].i));
  2498. result = false;
  2499. }
  2500. /* The large data models are only supported by the MSP430X. */
  2501. if (in_msp_attr[OFBA_MSPABI_Tag_Data_Model].i > 1
  2502. && out_msp_attr[OFBA_MSPABI_Tag_ISA].i != 2)
  2503. {
  2504. _bfd_error_handler
  2505. /* xgettext:c-format */
  2506. (_("error: %pB uses the %s data model but %pB only uses MSP430 instructions"),
  2507. ibfd, data_model (in_msp_attr[OFBA_MSPABI_Tag_Data_Model].i),
  2508. first_input_bfd);
  2509. result = false;
  2510. }
  2511. /* Just ignore the data region unless the large memory model is in use.
  2512. We have already checked that ibfd and obfd use the same memory model. */
  2513. if ((in_msp_attr[OFBA_MSPABI_Tag_Code_Model].i
  2514. == OFBA_MSPABI_Val_Code_Model_LARGE)
  2515. && (in_msp_attr[OFBA_MSPABI_Tag_Data_Model].i
  2516. == OFBA_MSPABI_Val_Data_Model_LARGE))
  2517. {
  2518. /* We cannot allow "lower region only" to be linked with any other
  2519. values (i.e. ANY or NONE).
  2520. Before this attribute existed, "ANY" region was the default. */
  2521. bool ibfd_lower_region_used
  2522. = (in_gnu_attr[Tag_GNU_MSP430_Data_Region].i
  2523. == Val_GNU_MSP430_Data_Region_Lower);
  2524. bool obfd_lower_region_used
  2525. = (out_gnu_attr[Tag_GNU_MSP430_Data_Region].i
  2526. == Val_GNU_MSP430_Data_Region_Lower);
  2527. if (ibfd_lower_region_used != obfd_lower_region_used)
  2528. {
  2529. _bfd_error_handler
  2530. (_("error: %pB can use the upper region for data, "
  2531. "but %pB assumes data is exclusively in lower memory"),
  2532. ibfd_lower_region_used ? obfd : ibfd,
  2533. ibfd_lower_region_used ? ibfd : obfd);
  2534. result = false;
  2535. }
  2536. }
  2537. return result;
  2538. }
  2539. /* Merge backend specific data from an object file to the output
  2540. object file when linking. */
  2541. static bool
  2542. elf32_msp430_merge_private_bfd_data (bfd *ibfd, struct bfd_link_info *info)
  2543. {
  2544. bfd *obfd = info->output_bfd;
  2545. /* Make sure that the machine number reflects the most
  2546. advanced version of the MSP architecture required. */
  2547. #define max(a,b) ((a) > (b) ? (a) : (b))
  2548. if (bfd_get_mach (ibfd) != bfd_get_mach (obfd))
  2549. bfd_default_set_arch_mach (obfd, bfd_get_arch (obfd),
  2550. max (bfd_get_mach (ibfd), bfd_get_mach (obfd)));
  2551. #undef max
  2552. return elf32_msp430_merge_msp430_attributes (ibfd, info);
  2553. }
  2554. static bool
  2555. msp430_elf_is_target_special_symbol (bfd *abfd, asymbol *sym)
  2556. {
  2557. return _bfd_elf_is_local_label_name (abfd, sym->name);
  2558. }
  2559. static bool
  2560. uses_large_model (bfd *abfd)
  2561. {
  2562. obj_attribute * attr;
  2563. if (abfd->flags & BFD_LINKER_CREATED)
  2564. return false;
  2565. attr = elf_known_obj_attributes_proc (abfd);
  2566. if (attr == NULL)
  2567. return false;
  2568. return attr[OFBA_MSPABI_Tag_Code_Model].i == 2;
  2569. }
  2570. static unsigned int
  2571. elf32_msp430_eh_frame_address_size (bfd *abfd,
  2572. const asection *sec ATTRIBUTE_UNUSED)
  2573. {
  2574. return uses_large_model (abfd) ? 4 : 2;
  2575. }
  2576. /* This is gross. The MSP430 EABI says that (sec 11.5):
  2577. "An implementation may choose to use Rel or Rela
  2578. type relocations for other relocations."
  2579. But it also says that:
  2580. "Certain relocations are identified as Rela only. [snip]
  2581. Where Rela is specified, an implementation must honor
  2582. this requirement."
  2583. There is one relocation marked as requiring RELA - R_MSP430_ABS_HI16 - but
  2584. to keep things simple we choose to use RELA relocations throughout. The
  2585. problem is that the TI compiler generates REL relocations, so we have to
  2586. be able to accept those as well. */
  2587. #define elf_backend_may_use_rel_p 1
  2588. #define elf_backend_may_use_rela_p 1
  2589. #define elf_backend_default_use_rela_p 1
  2590. #undef elf_backend_obj_attrs_vendor
  2591. #define elf_backend_obj_attrs_vendor "mspabi"
  2592. #undef elf_backend_obj_attrs_section
  2593. #define elf_backend_obj_attrs_section ".MSP430.attributes"
  2594. #undef elf_backend_obj_attrs_section_type
  2595. #define elf_backend_obj_attrs_section_type SHT_MSP430_ATTRIBUTES
  2596. #define elf_backend_section_from_shdr elf32_msp430_section_from_shdr
  2597. #define elf_backend_obj_attrs_handle_unknown elf32_msp430_obj_attrs_handle_unknown
  2598. #undef elf_backend_obj_attrs_arg_type
  2599. #define elf_backend_obj_attrs_arg_type elf32_msp430_obj_attrs_arg_type
  2600. #define bfd_elf32_bfd_merge_private_bfd_data elf32_msp430_merge_private_bfd_data
  2601. #define elf_backend_eh_frame_address_size elf32_msp430_eh_frame_address_size
  2602. #define ELF_ARCH bfd_arch_msp430
  2603. #define ELF_MACHINE_CODE EM_MSP430
  2604. #define ELF_MACHINE_ALT1 EM_MSP430_OLD
  2605. #define ELF_MAXPAGESIZE 4
  2606. #define ELF_OSABI ELFOSABI_STANDALONE
  2607. #define TARGET_LITTLE_SYM msp430_elf32_vec
  2608. #define TARGET_LITTLE_NAME "elf32-msp430"
  2609. #define elf_info_to_howto msp430_info_to_howto_rela
  2610. #define elf_info_to_howto_rel NULL
  2611. #define elf_backend_relocate_section elf32_msp430_relocate_section
  2612. #define elf_backend_check_relocs elf32_msp430_check_relocs
  2613. #define elf_backend_can_gc_sections 1
  2614. #define elf_backend_final_write_processing bfd_elf_msp430_final_write_processing
  2615. #define elf_backend_object_p elf32_msp430_object_p
  2616. #define bfd_elf32_bfd_relax_section msp430_elf_relax_section
  2617. #define bfd_elf32_bfd_is_target_special_symbol msp430_elf_is_target_special_symbol
  2618. #undef elf32_bed
  2619. #define elf32_bed elf32_msp430_bed
  2620. #include "elf32-target.h"
  2621. /* The TI compiler sets the OSABI field to ELFOSABI_NONE. */
  2622. #undef TARGET_LITTLE_SYM
  2623. #define TARGET_LITTLE_SYM msp430_elf32_ti_vec
  2624. #undef elf32_bed
  2625. #define elf32_bed elf32_msp430_ti_bed
  2626. #undef ELF_OSABI
  2627. #define ELF_OSABI ELFOSABI_NONE
  2628. static const struct bfd_elf_special_section msp430_ti_elf_special_sections[] =
  2629. {
  2630. /* prefix, prefix_length, suffix_len, type, attributes. */
  2631. { STRING_COMMA_LEN (".TI.symbol.alias"), 0, SHT_MSP430_SYM_ALIASES, 0 },
  2632. { STRING_COMMA_LEN (".TI.section.flags"), 0, SHT_MSP430_SEC_FLAGS, 0 },
  2633. { STRING_COMMA_LEN ("_TI_build_attrib"), 0, SHT_MSP430_ATTRIBUTES, 0 },
  2634. { NULL, 0, 0, 0, 0 }
  2635. };
  2636. #undef elf_backend_special_sections
  2637. #define elf_backend_special_sections msp430_ti_elf_special_sections
  2638. #include "elf32-target.h"