elf32-avr.c 132 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948394939503951395239533954395539563957395839593960396139623963396439653966396739683969397039713972397339743975397639773978397939803981398239833984398539863987398839893990399139923993399439953996399739983999400040014002400340044005400640074008400940104011401240134014401540164017401840194020402140224023402440254026402740284029403040314032403340344035403640374038403940404041404240434044404540464047404840494050405140524053405440554056405740584059406040614062406340644065406640674068406940704071407240734074407540764077407840794080408140824083408440854086408740884089409040914092409340944095409640974098409941004101410241034104410541064107410841094110411141124113411441154116411741184119412041214122412341244125412641274128412941304131413241334134413541364137413841394140414141424143414441454146414741484149
  1. /* AVR-specific support for 32-bit ELF
  2. Copyright (C) 1999-2015 Free Software Foundation, Inc.
  3. Contributed by Denis Chertykov <denisc@overta.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,
  16. Boston, MA 02110-1301, USA. */
  17. #include "sysdep.h"
  18. #include "bfd.h"
  19. #include "libbfd.h"
  20. #include "elf-bfd.h"
  21. #include "elf/avr.h"
  22. #include "elf32-avr.h"
  23. #include "bfd_stdint.h"
  24. /* Enable debugging printout at stdout with this variable. */
  25. static bfd_boolean debug_relax = FALSE;
  26. /* Enable debugging printout at stdout with this variable. */
  27. static bfd_boolean debug_stubs = FALSE;
  28. static bfd_reloc_status_type
  29. bfd_elf_avr_diff_reloc (bfd *, arelent *, asymbol *, void *,
  30. asection *, bfd *, char **);
  31. /* Hash table initialization and handling. Code is taken from the hppa port
  32. and adapted to the needs of AVR. */
  33. /* We use two hash tables to hold information for linking avr objects.
  34. The first is the elf32_avr_link_hash_table which is derived from the
  35. stanard ELF linker hash table. We use this as a place to attach the other
  36. hash table and some static information.
  37. The second is the stub hash table which is derived from the base BFD
  38. hash table. The stub hash table holds the information on the linker
  39. stubs. */
  40. struct elf32_avr_stub_hash_entry
  41. {
  42. /* Base hash table entry structure. */
  43. struct bfd_hash_entry bh_root;
  44. /* Offset within stub_sec of the beginning of this stub. */
  45. bfd_vma stub_offset;
  46. /* Given the symbol's value and its section we can determine its final
  47. value when building the stubs (so the stub knows where to jump). */
  48. bfd_vma target_value;
  49. /* This way we could mark stubs to be no longer necessary. */
  50. bfd_boolean is_actually_needed;
  51. };
  52. struct elf32_avr_link_hash_table
  53. {
  54. /* The main hash table. */
  55. struct elf_link_hash_table etab;
  56. /* The stub hash table. */
  57. struct bfd_hash_table bstab;
  58. bfd_boolean no_stubs;
  59. /* Linker stub bfd. */
  60. bfd *stub_bfd;
  61. /* The stub section. */
  62. asection *stub_sec;
  63. /* Usually 0, unless we are generating code for a bootloader. Will
  64. be initialized by elf32_avr_size_stubs to the vma offset of the
  65. output section associated with the stub section. */
  66. bfd_vma vector_base;
  67. /* Assorted information used by elf32_avr_size_stubs. */
  68. unsigned int bfd_count;
  69. unsigned int top_index;
  70. asection ** input_list;
  71. Elf_Internal_Sym ** all_local_syms;
  72. /* Tables for mapping vma beyond the 128k boundary to the address of the
  73. corresponding stub. (AMT)
  74. "amt_max_entry_cnt" reflects the number of entries that memory is allocated
  75. for in the "amt_stub_offsets" and "amt_destination_addr" arrays.
  76. "amt_entry_cnt" informs how many of these entries actually contain
  77. useful data. */
  78. unsigned int amt_entry_cnt;
  79. unsigned int amt_max_entry_cnt;
  80. bfd_vma * amt_stub_offsets;
  81. bfd_vma * amt_destination_addr;
  82. };
  83. /* Various hash macros and functions. */
  84. #define avr_link_hash_table(p) \
  85. /* PR 3874: Check that we have an AVR style hash table before using it. */\
  86. (elf_hash_table_id ((struct elf_link_hash_table *) ((p)->hash)) \
  87. == AVR_ELF_DATA ? ((struct elf32_avr_link_hash_table *) ((p)->hash)) : NULL)
  88. #define avr_stub_hash_entry(ent) \
  89. ((struct elf32_avr_stub_hash_entry *)(ent))
  90. #define avr_stub_hash_lookup(table, string, create, copy) \
  91. ((struct elf32_avr_stub_hash_entry *) \
  92. bfd_hash_lookup ((table), (string), (create), (copy)))
  93. static reloc_howto_type elf_avr_howto_table[] =
  94. {
  95. HOWTO (R_AVR_NONE, /* type */
  96. 0, /* rightshift */
  97. 3, /* size (0 = byte, 1 = short, 2 = long) */
  98. 0, /* bitsize */
  99. FALSE, /* pc_relative */
  100. 0, /* bitpos */
  101. complain_overflow_dont, /* complain_on_overflow */
  102. bfd_elf_generic_reloc, /* special_function */
  103. "R_AVR_NONE", /* name */
  104. FALSE, /* partial_inplace */
  105. 0, /* src_mask */
  106. 0, /* dst_mask */
  107. FALSE), /* pcrel_offset */
  108. HOWTO (R_AVR_32, /* type */
  109. 0, /* rightshift */
  110. 2, /* size (0 = byte, 1 = short, 2 = long) */
  111. 32, /* bitsize */
  112. FALSE, /* pc_relative */
  113. 0, /* bitpos */
  114. complain_overflow_bitfield, /* complain_on_overflow */
  115. bfd_elf_generic_reloc, /* special_function */
  116. "R_AVR_32", /* name */
  117. FALSE, /* partial_inplace */
  118. 0xffffffff, /* src_mask */
  119. 0xffffffff, /* dst_mask */
  120. FALSE), /* pcrel_offset */
  121. /* A 7 bit PC relative relocation. */
  122. HOWTO (R_AVR_7_PCREL, /* type */
  123. 1, /* rightshift */
  124. 1, /* size (0 = byte, 1 = short, 2 = long) */
  125. 7, /* bitsize */
  126. TRUE, /* pc_relative */
  127. 3, /* bitpos */
  128. complain_overflow_bitfield, /* complain_on_overflow */
  129. bfd_elf_generic_reloc, /* special_function */
  130. "R_AVR_7_PCREL", /* name */
  131. FALSE, /* partial_inplace */
  132. 0xffff, /* src_mask */
  133. 0xffff, /* dst_mask */
  134. TRUE), /* pcrel_offset */
  135. /* A 13 bit PC relative relocation. */
  136. HOWTO (R_AVR_13_PCREL, /* type */
  137. 1, /* rightshift */
  138. 1, /* size (0 = byte, 1 = short, 2 = long) */
  139. 13, /* bitsize */
  140. TRUE, /* pc_relative */
  141. 0, /* bitpos */
  142. complain_overflow_bitfield, /* complain_on_overflow */
  143. bfd_elf_generic_reloc, /* special_function */
  144. "R_AVR_13_PCREL", /* name */
  145. FALSE, /* partial_inplace */
  146. 0xfff, /* src_mask */
  147. 0xfff, /* dst_mask */
  148. TRUE), /* pcrel_offset */
  149. /* A 16 bit absolute relocation. */
  150. HOWTO (R_AVR_16, /* type */
  151. 0, /* rightshift */
  152. 1, /* size (0 = byte, 1 = short, 2 = long) */
  153. 16, /* bitsize */
  154. FALSE, /* pc_relative */
  155. 0, /* bitpos */
  156. complain_overflow_dont, /* complain_on_overflow */
  157. bfd_elf_generic_reloc, /* special_function */
  158. "R_AVR_16", /* name */
  159. FALSE, /* partial_inplace */
  160. 0xffff, /* src_mask */
  161. 0xffff, /* dst_mask */
  162. FALSE), /* pcrel_offset */
  163. /* A 16 bit absolute relocation for command address
  164. Will be changed when linker stubs are needed. */
  165. HOWTO (R_AVR_16_PM, /* type */
  166. 1, /* rightshift */
  167. 1, /* size (0 = byte, 1 = short, 2 = long) */
  168. 16, /* bitsize */
  169. FALSE, /* pc_relative */
  170. 0, /* bitpos */
  171. complain_overflow_bitfield, /* complain_on_overflow */
  172. bfd_elf_generic_reloc, /* special_function */
  173. "R_AVR_16_PM", /* name */
  174. FALSE, /* partial_inplace */
  175. 0xffff, /* src_mask */
  176. 0xffff, /* dst_mask */
  177. FALSE), /* pcrel_offset */
  178. /* A low 8 bit absolute relocation of 16 bit address.
  179. For LDI command. */
  180. HOWTO (R_AVR_LO8_LDI, /* type */
  181. 0, /* rightshift */
  182. 1, /* size (0 = byte, 1 = short, 2 = long) */
  183. 8, /* bitsize */
  184. FALSE, /* pc_relative */
  185. 0, /* bitpos */
  186. complain_overflow_dont, /* complain_on_overflow */
  187. bfd_elf_generic_reloc, /* special_function */
  188. "R_AVR_LO8_LDI", /* name */
  189. FALSE, /* partial_inplace */
  190. 0xffff, /* src_mask */
  191. 0xffff, /* dst_mask */
  192. FALSE), /* pcrel_offset */
  193. /* A high 8 bit absolute relocation of 16 bit address.
  194. For LDI command. */
  195. HOWTO (R_AVR_HI8_LDI, /* type */
  196. 8, /* rightshift */
  197. 1, /* size (0 = byte, 1 = short, 2 = long) */
  198. 8, /* bitsize */
  199. FALSE, /* pc_relative */
  200. 0, /* bitpos */
  201. complain_overflow_dont, /* complain_on_overflow */
  202. bfd_elf_generic_reloc, /* special_function */
  203. "R_AVR_HI8_LDI", /* name */
  204. FALSE, /* partial_inplace */
  205. 0xffff, /* src_mask */
  206. 0xffff, /* dst_mask */
  207. FALSE), /* pcrel_offset */
  208. /* A high 6 bit absolute relocation of 22 bit address.
  209. For LDI command. As well second most significant 8 bit value of
  210. a 32 bit link-time constant. */
  211. HOWTO (R_AVR_HH8_LDI, /* type */
  212. 16, /* rightshift */
  213. 1, /* size (0 = byte, 1 = short, 2 = long) */
  214. 8, /* bitsize */
  215. FALSE, /* pc_relative */
  216. 0, /* bitpos */
  217. complain_overflow_dont, /* complain_on_overflow */
  218. bfd_elf_generic_reloc, /* special_function */
  219. "R_AVR_HH8_LDI", /* name */
  220. FALSE, /* partial_inplace */
  221. 0xffff, /* src_mask */
  222. 0xffff, /* dst_mask */
  223. FALSE), /* pcrel_offset */
  224. /* A negative low 8 bit absolute relocation of 16 bit address.
  225. For LDI command. */
  226. HOWTO (R_AVR_LO8_LDI_NEG, /* type */
  227. 0, /* rightshift */
  228. 1, /* size (0 = byte, 1 = short, 2 = long) */
  229. 8, /* bitsize */
  230. FALSE, /* pc_relative */
  231. 0, /* bitpos */
  232. complain_overflow_dont, /* complain_on_overflow */
  233. bfd_elf_generic_reloc, /* special_function */
  234. "R_AVR_LO8_LDI_NEG", /* name */
  235. FALSE, /* partial_inplace */
  236. 0xffff, /* src_mask */
  237. 0xffff, /* dst_mask */
  238. FALSE), /* pcrel_offset */
  239. /* A negative high 8 bit absolute relocation of 16 bit address.
  240. For LDI command. */
  241. HOWTO (R_AVR_HI8_LDI_NEG, /* type */
  242. 8, /* rightshift */
  243. 1, /* size (0 = byte, 1 = short, 2 = long) */
  244. 8, /* bitsize */
  245. FALSE, /* pc_relative */
  246. 0, /* bitpos */
  247. complain_overflow_dont, /* complain_on_overflow */
  248. bfd_elf_generic_reloc, /* special_function */
  249. "R_AVR_HI8_LDI_NEG", /* name */
  250. FALSE, /* partial_inplace */
  251. 0xffff, /* src_mask */
  252. 0xffff, /* dst_mask */
  253. FALSE), /* pcrel_offset */
  254. /* A negative high 6 bit absolute relocation of 22 bit address.
  255. For LDI command. */
  256. HOWTO (R_AVR_HH8_LDI_NEG, /* type */
  257. 16, /* rightshift */
  258. 1, /* size (0 = byte, 1 = short, 2 = long) */
  259. 8, /* bitsize */
  260. FALSE, /* pc_relative */
  261. 0, /* bitpos */
  262. complain_overflow_dont, /* complain_on_overflow */
  263. bfd_elf_generic_reloc, /* special_function */
  264. "R_AVR_HH8_LDI_NEG", /* name */
  265. FALSE, /* partial_inplace */
  266. 0xffff, /* src_mask */
  267. 0xffff, /* dst_mask */
  268. FALSE), /* pcrel_offset */
  269. /* A low 8 bit absolute relocation of 24 bit program memory address.
  270. For LDI command. Will not be changed when linker stubs are needed. */
  271. HOWTO (R_AVR_LO8_LDI_PM, /* type */
  272. 1, /* rightshift */
  273. 1, /* size (0 = byte, 1 = short, 2 = long) */
  274. 8, /* bitsize */
  275. FALSE, /* pc_relative */
  276. 0, /* bitpos */
  277. complain_overflow_dont, /* complain_on_overflow */
  278. bfd_elf_generic_reloc, /* special_function */
  279. "R_AVR_LO8_LDI_PM", /* name */
  280. FALSE, /* partial_inplace */
  281. 0xffff, /* src_mask */
  282. 0xffff, /* dst_mask */
  283. FALSE), /* pcrel_offset */
  284. /* A low 8 bit absolute relocation of 24 bit program memory address.
  285. For LDI command. Will not be changed when linker stubs are needed. */
  286. HOWTO (R_AVR_HI8_LDI_PM, /* type */
  287. 9, /* rightshift */
  288. 1, /* size (0 = byte, 1 = short, 2 = long) */
  289. 8, /* bitsize */
  290. FALSE, /* pc_relative */
  291. 0, /* bitpos */
  292. complain_overflow_dont, /* complain_on_overflow */
  293. bfd_elf_generic_reloc, /* special_function */
  294. "R_AVR_HI8_LDI_PM", /* name */
  295. FALSE, /* partial_inplace */
  296. 0xffff, /* src_mask */
  297. 0xffff, /* dst_mask */
  298. FALSE), /* pcrel_offset */
  299. /* A low 8 bit absolute relocation of 24 bit program memory address.
  300. For LDI command. Will not be changed when linker stubs are needed. */
  301. HOWTO (R_AVR_HH8_LDI_PM, /* type */
  302. 17, /* rightshift */
  303. 1, /* size (0 = byte, 1 = short, 2 = long) */
  304. 8, /* bitsize */
  305. FALSE, /* pc_relative */
  306. 0, /* bitpos */
  307. complain_overflow_dont, /* complain_on_overflow */
  308. bfd_elf_generic_reloc, /* special_function */
  309. "R_AVR_HH8_LDI_PM", /* name */
  310. FALSE, /* partial_inplace */
  311. 0xffff, /* src_mask */
  312. 0xffff, /* dst_mask */
  313. FALSE), /* pcrel_offset */
  314. /* A low 8 bit absolute relocation of 24 bit program memory address.
  315. For LDI command. Will not be changed when linker stubs are needed. */
  316. HOWTO (R_AVR_LO8_LDI_PM_NEG, /* type */
  317. 1, /* rightshift */
  318. 1, /* size (0 = byte, 1 = short, 2 = long) */
  319. 8, /* bitsize */
  320. FALSE, /* pc_relative */
  321. 0, /* bitpos */
  322. complain_overflow_dont, /* complain_on_overflow */
  323. bfd_elf_generic_reloc, /* special_function */
  324. "R_AVR_LO8_LDI_PM_NEG", /* name */
  325. FALSE, /* partial_inplace */
  326. 0xffff, /* src_mask */
  327. 0xffff, /* dst_mask */
  328. FALSE), /* pcrel_offset */
  329. /* A low 8 bit absolute relocation of 24 bit program memory address.
  330. For LDI command. Will not be changed when linker stubs are needed. */
  331. HOWTO (R_AVR_HI8_LDI_PM_NEG, /* type */
  332. 9, /* rightshift */
  333. 1, /* size (0 = byte, 1 = short, 2 = long) */
  334. 8, /* bitsize */
  335. FALSE, /* pc_relative */
  336. 0, /* bitpos */
  337. complain_overflow_dont, /* complain_on_overflow */
  338. bfd_elf_generic_reloc, /* special_function */
  339. "R_AVR_HI8_LDI_PM_NEG", /* name */
  340. FALSE, /* partial_inplace */
  341. 0xffff, /* src_mask */
  342. 0xffff, /* dst_mask */
  343. FALSE), /* pcrel_offset */
  344. /* A low 8 bit absolute relocation of 24 bit program memory address.
  345. For LDI command. Will not be changed when linker stubs are needed. */
  346. HOWTO (R_AVR_HH8_LDI_PM_NEG, /* type */
  347. 17, /* rightshift */
  348. 1, /* size (0 = byte, 1 = short, 2 = long) */
  349. 8, /* bitsize */
  350. FALSE, /* pc_relative */
  351. 0, /* bitpos */
  352. complain_overflow_dont, /* complain_on_overflow */
  353. bfd_elf_generic_reloc, /* special_function */
  354. "R_AVR_HH8_LDI_PM_NEG", /* name */
  355. FALSE, /* partial_inplace */
  356. 0xffff, /* src_mask */
  357. 0xffff, /* dst_mask */
  358. FALSE), /* pcrel_offset */
  359. /* Relocation for CALL command in ATmega. */
  360. HOWTO (R_AVR_CALL, /* type */
  361. 1, /* rightshift */
  362. 2, /* size (0 = byte, 1 = short, 2 = long) */
  363. 23, /* bitsize */
  364. FALSE, /* pc_relative */
  365. 0, /* bitpos */
  366. complain_overflow_dont,/* complain_on_overflow */
  367. bfd_elf_generic_reloc, /* special_function */
  368. "R_AVR_CALL", /* name */
  369. FALSE, /* partial_inplace */
  370. 0xffffffff, /* src_mask */
  371. 0xffffffff, /* dst_mask */
  372. FALSE), /* pcrel_offset */
  373. /* A 16 bit absolute relocation of 16 bit address.
  374. For LDI command. */
  375. HOWTO (R_AVR_LDI, /* type */
  376. 0, /* rightshift */
  377. 1, /* size (0 = byte, 1 = short, 2 = long) */
  378. 16, /* bitsize */
  379. FALSE, /* pc_relative */
  380. 0, /* bitpos */
  381. complain_overflow_dont,/* complain_on_overflow */
  382. bfd_elf_generic_reloc, /* special_function */
  383. "R_AVR_LDI", /* name */
  384. FALSE, /* partial_inplace */
  385. 0xffff, /* src_mask */
  386. 0xffff, /* dst_mask */
  387. FALSE), /* pcrel_offset */
  388. /* A 6 bit absolute relocation of 6 bit offset.
  389. For ldd/sdd command. */
  390. HOWTO (R_AVR_6, /* type */
  391. 0, /* rightshift */
  392. 0, /* size (0 = byte, 1 = short, 2 = long) */
  393. 6, /* bitsize */
  394. FALSE, /* pc_relative */
  395. 0, /* bitpos */
  396. complain_overflow_dont,/* complain_on_overflow */
  397. bfd_elf_generic_reloc, /* special_function */
  398. "R_AVR_6", /* name */
  399. FALSE, /* partial_inplace */
  400. 0xffff, /* src_mask */
  401. 0xffff, /* dst_mask */
  402. FALSE), /* pcrel_offset */
  403. /* A 6 bit absolute relocation of 6 bit offset.
  404. For sbiw/adiw command. */
  405. HOWTO (R_AVR_6_ADIW, /* type */
  406. 0, /* rightshift */
  407. 0, /* size (0 = byte, 1 = short, 2 = long) */
  408. 6, /* bitsize */
  409. FALSE, /* pc_relative */
  410. 0, /* bitpos */
  411. complain_overflow_dont,/* complain_on_overflow */
  412. bfd_elf_generic_reloc, /* special_function */
  413. "R_AVR_6_ADIW", /* name */
  414. FALSE, /* partial_inplace */
  415. 0xffff, /* src_mask */
  416. 0xffff, /* dst_mask */
  417. FALSE), /* pcrel_offset */
  418. /* Most significant 8 bit value of a 32 bit link-time constant. */
  419. HOWTO (R_AVR_MS8_LDI, /* type */
  420. 24, /* rightshift */
  421. 1, /* size (0 = byte, 1 = short, 2 = long) */
  422. 8, /* bitsize */
  423. FALSE, /* pc_relative */
  424. 0, /* bitpos */
  425. complain_overflow_dont, /* complain_on_overflow */
  426. bfd_elf_generic_reloc, /* special_function */
  427. "R_AVR_MS8_LDI", /* name */
  428. FALSE, /* partial_inplace */
  429. 0xffff, /* src_mask */
  430. 0xffff, /* dst_mask */
  431. FALSE), /* pcrel_offset */
  432. /* Negative most significant 8 bit value of a 32 bit link-time constant. */
  433. HOWTO (R_AVR_MS8_LDI_NEG, /* type */
  434. 24, /* rightshift */
  435. 1, /* size (0 = byte, 1 = short, 2 = long) */
  436. 8, /* bitsize */
  437. FALSE, /* pc_relative */
  438. 0, /* bitpos */
  439. complain_overflow_dont, /* complain_on_overflow */
  440. bfd_elf_generic_reloc, /* special_function */
  441. "R_AVR_MS8_LDI_NEG", /* name */
  442. FALSE, /* partial_inplace */
  443. 0xffff, /* src_mask */
  444. 0xffff, /* dst_mask */
  445. FALSE), /* pcrel_offset */
  446. /* A low 8 bit absolute relocation of 24 bit program memory address.
  447. For LDI command. Will be changed when linker stubs are needed. */
  448. HOWTO (R_AVR_LO8_LDI_GS, /* type */
  449. 1, /* rightshift */
  450. 1, /* size (0 = byte, 1 = short, 2 = long) */
  451. 8, /* bitsize */
  452. FALSE, /* pc_relative */
  453. 0, /* bitpos */
  454. complain_overflow_dont, /* complain_on_overflow */
  455. bfd_elf_generic_reloc, /* special_function */
  456. "R_AVR_LO8_LDI_GS", /* name */
  457. FALSE, /* partial_inplace */
  458. 0xffff, /* src_mask */
  459. 0xffff, /* dst_mask */
  460. FALSE), /* pcrel_offset */
  461. /* A low 8 bit absolute relocation of 24 bit program memory address.
  462. For LDI command. Will be changed when linker stubs are needed. */
  463. HOWTO (R_AVR_HI8_LDI_GS, /* type */
  464. 9, /* rightshift */
  465. 1, /* size (0 = byte, 1 = short, 2 = long) */
  466. 8, /* bitsize */
  467. FALSE, /* pc_relative */
  468. 0, /* bitpos */
  469. complain_overflow_dont, /* complain_on_overflow */
  470. bfd_elf_generic_reloc, /* special_function */
  471. "R_AVR_HI8_LDI_GS", /* name */
  472. FALSE, /* partial_inplace */
  473. 0xffff, /* src_mask */
  474. 0xffff, /* dst_mask */
  475. FALSE), /* pcrel_offset */
  476. /* 8 bit offset. */
  477. HOWTO (R_AVR_8, /* type */
  478. 0, /* rightshift */
  479. 0, /* size (0 = byte, 1 = short, 2 = long) */
  480. 8, /* bitsize */
  481. FALSE, /* pc_relative */
  482. 0, /* bitpos */
  483. complain_overflow_bitfield,/* complain_on_overflow */
  484. bfd_elf_generic_reloc, /* special_function */
  485. "R_AVR_8", /* name */
  486. FALSE, /* partial_inplace */
  487. 0x000000ff, /* src_mask */
  488. 0x000000ff, /* dst_mask */
  489. FALSE), /* pcrel_offset */
  490. /* lo8-part to use in .byte lo8(sym). */
  491. HOWTO (R_AVR_8_LO8, /* type */
  492. 0, /* rightshift */
  493. 0, /* size (0 = byte, 1 = short, 2 = long) */
  494. 8, /* bitsize */
  495. FALSE, /* pc_relative */
  496. 0, /* bitpos */
  497. complain_overflow_dont,/* complain_on_overflow */
  498. bfd_elf_generic_reloc, /* special_function */
  499. "R_AVR_8_LO8", /* name */
  500. FALSE, /* partial_inplace */
  501. 0xffffff, /* src_mask */
  502. 0xffffff, /* dst_mask */
  503. FALSE), /* pcrel_offset */
  504. /* hi8-part to use in .byte hi8(sym). */
  505. HOWTO (R_AVR_8_HI8, /* type */
  506. 8, /* rightshift */
  507. 0, /* size (0 = byte, 1 = short, 2 = long) */
  508. 8, /* bitsize */
  509. FALSE, /* pc_relative */
  510. 0, /* bitpos */
  511. complain_overflow_dont,/* complain_on_overflow */
  512. bfd_elf_generic_reloc, /* special_function */
  513. "R_AVR_8_HI8", /* name */
  514. FALSE, /* partial_inplace */
  515. 0xffffff, /* src_mask */
  516. 0xffffff, /* dst_mask */
  517. FALSE), /* pcrel_offset */
  518. /* hlo8-part to use in .byte hlo8(sym). */
  519. HOWTO (R_AVR_8_HLO8, /* type */
  520. 16, /* rightshift */
  521. 0, /* size (0 = byte, 1 = short, 2 = long) */
  522. 8, /* bitsize */
  523. FALSE, /* pc_relative */
  524. 0, /* bitpos */
  525. complain_overflow_dont,/* complain_on_overflow */
  526. bfd_elf_generic_reloc, /* special_function */
  527. "R_AVR_8_HLO8", /* name */
  528. FALSE, /* partial_inplace */
  529. 0xffffff, /* src_mask */
  530. 0xffffff, /* dst_mask */
  531. FALSE), /* pcrel_offset */
  532. HOWTO (R_AVR_DIFF8, /* type */
  533. 0, /* rightshift */
  534. 0, /* size (0 = byte, 1 = short, 2 = long) */
  535. 8, /* bitsize */
  536. FALSE, /* pc_relative */
  537. 0, /* bitpos */
  538. complain_overflow_bitfield, /* complain_on_overflow */
  539. bfd_elf_avr_diff_reloc, /* special_function */
  540. "R_AVR_DIFF8", /* name */
  541. FALSE, /* partial_inplace */
  542. 0, /* src_mask */
  543. 0xff, /* dst_mask */
  544. FALSE), /* pcrel_offset */
  545. HOWTO (R_AVR_DIFF16, /* type */
  546. 0, /* rightshift */
  547. 1, /* size (0 = byte, 1 = short, 2 = long) */
  548. 16, /* bitsize */
  549. FALSE, /* pc_relative */
  550. 0, /* bitpos */
  551. complain_overflow_bitfield, /* complain_on_overflow */
  552. bfd_elf_avr_diff_reloc,/* special_function */
  553. "R_AVR_DIFF16", /* name */
  554. FALSE, /* partial_inplace */
  555. 0, /* src_mask */
  556. 0xffff, /* dst_mask */
  557. FALSE), /* pcrel_offset */
  558. HOWTO (R_AVR_DIFF32, /* type */
  559. 0, /* rightshift */
  560. 2, /* size (0 = byte, 1 = short, 2 = long) */
  561. 32, /* bitsize */
  562. FALSE, /* pc_relative */
  563. 0, /* bitpos */
  564. complain_overflow_bitfield, /* complain_on_overflow */
  565. bfd_elf_avr_diff_reloc,/* special_function */
  566. "R_AVR_DIFF32", /* name */
  567. FALSE, /* partial_inplace */
  568. 0, /* src_mask */
  569. 0xffffffff, /* dst_mask */
  570. FALSE), /* pcrel_offset */
  571. /* 7 bit immediate for LDS/STS in Tiny core. */
  572. HOWTO (R_AVR_LDS_STS_16, /* type */
  573. 0, /* rightshift */
  574. 1, /* size (0 = byte, 1 = short, 2 = long) */
  575. 7, /* bitsize */
  576. FALSE, /* pc_relative */
  577. 0, /* bitpos */
  578. complain_overflow_dont,/* complain_on_overflow */
  579. bfd_elf_generic_reloc, /* special_function */
  580. "R_AVR_LDS_STS_16", /* name */
  581. FALSE, /* partial_inplace */
  582. 0xffff, /* src_mask */
  583. 0xffff, /* dst_mask */
  584. FALSE), /* pcrel_offset */
  585. HOWTO (R_AVR_PORT6, /* type */
  586. 0, /* rightshift */
  587. 0, /* size (0 = byte, 1 = short, 2 = long) */
  588. 6, /* bitsize */
  589. FALSE, /* pc_relative */
  590. 0, /* bitpos */
  591. complain_overflow_dont,/* complain_on_overflow */
  592. bfd_elf_generic_reloc, /* special_function */
  593. "R_AVR_PORT6", /* name */
  594. FALSE, /* partial_inplace */
  595. 0xffffff, /* src_mask */
  596. 0xffffff, /* dst_mask */
  597. FALSE), /* pcrel_offset */
  598. HOWTO (R_AVR_PORT5, /* type */
  599. 0, /* rightshift */
  600. 0, /* size (0 = byte, 1 = short, 2 = long) */
  601. 5, /* bitsize */
  602. FALSE, /* pc_relative */
  603. 0, /* bitpos */
  604. complain_overflow_dont,/* complain_on_overflow */
  605. bfd_elf_generic_reloc, /* special_function */
  606. "R_AVR_PORT5", /* name */
  607. FALSE, /* partial_inplace */
  608. 0xffffff, /* src_mask */
  609. 0xffffff, /* dst_mask */
  610. FALSE), /* pcrel_offset */
  611. /* A 32 bit PC relative relocation. */
  612. HOWTO (R_AVR_32_PCREL, /* type */
  613. 0, /* rightshift */
  614. 2, /* size (0 = byte, 1 = short, 2 = long) */
  615. 32, /* bitsize */
  616. TRUE, /* pc_relative */
  617. 0, /* bitpos */
  618. complain_overflow_bitfield, /* complain_on_overflow */
  619. bfd_elf_generic_reloc, /* special_function */
  620. "R_AVR_32_PCREL", /* name */
  621. FALSE, /* partial_inplace */
  622. 0xffffffff, /* src_mask */
  623. 0xffffffff, /* dst_mask */
  624. TRUE), /* pcrel_offset */
  625. };
  626. /* Map BFD reloc types to AVR ELF reloc types. */
  627. struct avr_reloc_map
  628. {
  629. bfd_reloc_code_real_type bfd_reloc_val;
  630. unsigned int elf_reloc_val;
  631. };
  632. static const struct avr_reloc_map avr_reloc_map[] =
  633. {
  634. { BFD_RELOC_NONE, R_AVR_NONE },
  635. { BFD_RELOC_32, R_AVR_32 },
  636. { BFD_RELOC_AVR_7_PCREL, R_AVR_7_PCREL },
  637. { BFD_RELOC_AVR_13_PCREL, R_AVR_13_PCREL },
  638. { BFD_RELOC_16, R_AVR_16 },
  639. { BFD_RELOC_AVR_16_PM, R_AVR_16_PM },
  640. { BFD_RELOC_AVR_LO8_LDI, R_AVR_LO8_LDI},
  641. { BFD_RELOC_AVR_HI8_LDI, R_AVR_HI8_LDI },
  642. { BFD_RELOC_AVR_HH8_LDI, R_AVR_HH8_LDI },
  643. { BFD_RELOC_AVR_MS8_LDI, R_AVR_MS8_LDI },
  644. { BFD_RELOC_AVR_LO8_LDI_NEG, R_AVR_LO8_LDI_NEG },
  645. { BFD_RELOC_AVR_HI8_LDI_NEG, R_AVR_HI8_LDI_NEG },
  646. { BFD_RELOC_AVR_HH8_LDI_NEG, R_AVR_HH8_LDI_NEG },
  647. { BFD_RELOC_AVR_MS8_LDI_NEG, R_AVR_MS8_LDI_NEG },
  648. { BFD_RELOC_AVR_LO8_LDI_PM, R_AVR_LO8_LDI_PM },
  649. { BFD_RELOC_AVR_LO8_LDI_GS, R_AVR_LO8_LDI_GS },
  650. { BFD_RELOC_AVR_HI8_LDI_PM, R_AVR_HI8_LDI_PM },
  651. { BFD_RELOC_AVR_HI8_LDI_GS, R_AVR_HI8_LDI_GS },
  652. { BFD_RELOC_AVR_HH8_LDI_PM, R_AVR_HH8_LDI_PM },
  653. { BFD_RELOC_AVR_LO8_LDI_PM_NEG, R_AVR_LO8_LDI_PM_NEG },
  654. { BFD_RELOC_AVR_HI8_LDI_PM_NEG, R_AVR_HI8_LDI_PM_NEG },
  655. { BFD_RELOC_AVR_HH8_LDI_PM_NEG, R_AVR_HH8_LDI_PM_NEG },
  656. { BFD_RELOC_AVR_CALL, R_AVR_CALL },
  657. { BFD_RELOC_AVR_LDI, R_AVR_LDI },
  658. { BFD_RELOC_AVR_6, R_AVR_6 },
  659. { BFD_RELOC_AVR_6_ADIW, R_AVR_6_ADIW },
  660. { BFD_RELOC_8, R_AVR_8 },
  661. { BFD_RELOC_AVR_8_LO, R_AVR_8_LO8 },
  662. { BFD_RELOC_AVR_8_HI, R_AVR_8_HI8 },
  663. { BFD_RELOC_AVR_8_HLO, R_AVR_8_HLO8 },
  664. { BFD_RELOC_AVR_DIFF8, R_AVR_DIFF8 },
  665. { BFD_RELOC_AVR_DIFF16, R_AVR_DIFF16 },
  666. { BFD_RELOC_AVR_DIFF32, R_AVR_DIFF32 },
  667. { BFD_RELOC_AVR_LDS_STS_16, R_AVR_LDS_STS_16},
  668. { BFD_RELOC_AVR_PORT6, R_AVR_PORT6},
  669. { BFD_RELOC_AVR_PORT5, R_AVR_PORT5},
  670. { BFD_RELOC_32_PCREL, R_AVR_32_PCREL}
  671. };
  672. /* Meant to be filled one day with the wrap around address for the
  673. specific device. I.e. should get the value 0x4000 for 16k devices,
  674. 0x8000 for 32k devices and so on.
  675. We initialize it here with a value of 0x1000000 resulting in
  676. that we will never suggest a wrap-around jump during relaxation.
  677. The logic of the source code later on assumes that in
  678. avr_pc_wrap_around one single bit is set. */
  679. static bfd_vma avr_pc_wrap_around = 0x10000000;
  680. /* If this variable holds a value different from zero, the linker relaxation
  681. machine will try to optimize call/ret sequences by a single jump
  682. instruction. This option could be switched off by a linker switch. */
  683. static int avr_replace_call_ret_sequences = 1;
  684. /* Per-section relaxation related information for avr. */
  685. struct avr_relax_info
  686. {
  687. /* Track the avr property records that apply to this section. */
  688. struct
  689. {
  690. /* Number of records in the list. */
  691. unsigned count;
  692. /* How many records worth of space have we allocated. */
  693. unsigned allocated;
  694. /* The records, only COUNT records are initialised. */
  695. struct avr_property_record *items;
  696. } records;
  697. };
  698. /* Per section data, specialised for avr. */
  699. struct elf_avr_section_data
  700. {
  701. /* The standard data must appear first. */
  702. struct bfd_elf_section_data elf;
  703. /* Relaxation related information. */
  704. struct avr_relax_info relax_info;
  705. };
  706. /* Possibly initialise avr specific data for new section SEC from ABFD. */
  707. static bfd_boolean
  708. elf_avr_new_section_hook (bfd *abfd, asection *sec)
  709. {
  710. if (!sec->used_by_bfd)
  711. {
  712. struct elf_avr_section_data *sdata;
  713. bfd_size_type amt = sizeof (*sdata);
  714. sdata = bfd_zalloc (abfd, amt);
  715. if (sdata == NULL)
  716. return FALSE;
  717. sec->used_by_bfd = sdata;
  718. }
  719. return _bfd_elf_new_section_hook (abfd, sec);
  720. }
  721. /* Return a pointer to the relaxation information for SEC. */
  722. static struct avr_relax_info *
  723. get_avr_relax_info (asection *sec)
  724. {
  725. struct elf_avr_section_data *section_data;
  726. /* No info available if no section or if it is an output section. */
  727. if (!sec || sec == sec->output_section)
  728. return NULL;
  729. section_data = (struct elf_avr_section_data *) elf_section_data (sec);
  730. return &section_data->relax_info;
  731. }
  732. /* Initialise the per section relaxation information for SEC. */
  733. static void
  734. init_avr_relax_info (asection *sec)
  735. {
  736. struct avr_relax_info *relax_info = get_avr_relax_info (sec);
  737. relax_info->records.count = 0;
  738. relax_info->records.allocated = 0;
  739. relax_info->records.items = NULL;
  740. }
  741. /* Initialize an entry in the stub hash table. */
  742. static struct bfd_hash_entry *
  743. stub_hash_newfunc (struct bfd_hash_entry *entry,
  744. struct bfd_hash_table *table,
  745. const char *string)
  746. {
  747. /* Allocate the structure if it has not already been allocated by a
  748. subclass. */
  749. if (entry == NULL)
  750. {
  751. entry = bfd_hash_allocate (table,
  752. sizeof (struct elf32_avr_stub_hash_entry));
  753. if (entry == NULL)
  754. return entry;
  755. }
  756. /* Call the allocation method of the superclass. */
  757. entry = bfd_hash_newfunc (entry, table, string);
  758. if (entry != NULL)
  759. {
  760. struct elf32_avr_stub_hash_entry *hsh;
  761. /* Initialize the local fields. */
  762. hsh = avr_stub_hash_entry (entry);
  763. hsh->stub_offset = 0;
  764. hsh->target_value = 0;
  765. }
  766. return entry;
  767. }
  768. /* This function is just a straight passthrough to the real
  769. function in linker.c. Its prupose is so that its address
  770. can be compared inside the avr_link_hash_table macro. */
  771. static struct bfd_hash_entry *
  772. elf32_avr_link_hash_newfunc (struct bfd_hash_entry * entry,
  773. struct bfd_hash_table * table,
  774. const char * string)
  775. {
  776. return _bfd_elf_link_hash_newfunc (entry, table, string);
  777. }
  778. /* Free the derived linker hash table. */
  779. static void
  780. elf32_avr_link_hash_table_free (bfd *obfd)
  781. {
  782. struct elf32_avr_link_hash_table *htab
  783. = (struct elf32_avr_link_hash_table *) obfd->link.hash;
  784. /* Free the address mapping table. */
  785. if (htab->amt_stub_offsets != NULL)
  786. free (htab->amt_stub_offsets);
  787. if (htab->amt_destination_addr != NULL)
  788. free (htab->amt_destination_addr);
  789. bfd_hash_table_free (&htab->bstab);
  790. _bfd_elf_link_hash_table_free (obfd);
  791. }
  792. /* Create the derived linker hash table. The AVR ELF port uses the derived
  793. hash table to keep information specific to the AVR ELF linker (without
  794. using static variables). */
  795. static struct bfd_link_hash_table *
  796. elf32_avr_link_hash_table_create (bfd *abfd)
  797. {
  798. struct elf32_avr_link_hash_table *htab;
  799. bfd_size_type amt = sizeof (*htab);
  800. htab = bfd_zmalloc (amt);
  801. if (htab == NULL)
  802. return NULL;
  803. if (!_bfd_elf_link_hash_table_init (&htab->etab, abfd,
  804. elf32_avr_link_hash_newfunc,
  805. sizeof (struct elf_link_hash_entry),
  806. AVR_ELF_DATA))
  807. {
  808. free (htab);
  809. return NULL;
  810. }
  811. /* Init the stub hash table too. */
  812. if (!bfd_hash_table_init (&htab->bstab, stub_hash_newfunc,
  813. sizeof (struct elf32_avr_stub_hash_entry)))
  814. {
  815. _bfd_elf_link_hash_table_free (abfd);
  816. return NULL;
  817. }
  818. htab->etab.root.hash_table_free = elf32_avr_link_hash_table_free;
  819. return &htab->etab.root;
  820. }
  821. /* Calculates the effective distance of a pc relative jump/call. */
  822. static int
  823. avr_relative_distance_considering_wrap_around (unsigned int distance)
  824. {
  825. unsigned int wrap_around_mask = avr_pc_wrap_around - 1;
  826. int dist_with_wrap_around = distance & wrap_around_mask;
  827. if (dist_with_wrap_around > ((int) (avr_pc_wrap_around >> 1)))
  828. dist_with_wrap_around -= avr_pc_wrap_around;
  829. return dist_with_wrap_around;
  830. }
  831. static reloc_howto_type *
  832. bfd_elf32_bfd_reloc_type_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  833. bfd_reloc_code_real_type code)
  834. {
  835. unsigned int i;
  836. for (i = 0;
  837. i < sizeof (avr_reloc_map) / sizeof (struct avr_reloc_map);
  838. i++)
  839. if (avr_reloc_map[i].bfd_reloc_val == code)
  840. return &elf_avr_howto_table[avr_reloc_map[i].elf_reloc_val];
  841. return NULL;
  842. }
  843. static reloc_howto_type *
  844. bfd_elf32_bfd_reloc_name_lookup (bfd *abfd ATTRIBUTE_UNUSED,
  845. const char *r_name)
  846. {
  847. unsigned int i;
  848. for (i = 0;
  849. i < sizeof (elf_avr_howto_table) / sizeof (elf_avr_howto_table[0]);
  850. i++)
  851. if (elf_avr_howto_table[i].name != NULL
  852. && strcasecmp (elf_avr_howto_table[i].name, r_name) == 0)
  853. return &elf_avr_howto_table[i];
  854. return NULL;
  855. }
  856. /* Set the howto pointer for an AVR ELF reloc. */
  857. static void
  858. avr_info_to_howto_rela (bfd *abfd ATTRIBUTE_UNUSED,
  859. arelent *cache_ptr,
  860. Elf_Internal_Rela *dst)
  861. {
  862. unsigned int r_type;
  863. r_type = ELF32_R_TYPE (dst->r_info);
  864. if (r_type >= (unsigned int) R_AVR_max)
  865. {
  866. _bfd_error_handler (_("%B: invalid AVR reloc number: %d"), abfd, r_type);
  867. r_type = 0;
  868. }
  869. cache_ptr->howto = &elf_avr_howto_table[r_type];
  870. }
  871. static bfd_boolean
  872. avr_stub_is_required_for_16_bit_reloc (bfd_vma relocation)
  873. {
  874. return (relocation >= 0x020000);
  875. }
  876. /* Returns the address of the corresponding stub if there is one.
  877. Returns otherwise an address above 0x020000. This function
  878. could also be used, if there is no knowledge on the section where
  879. the destination is found. */
  880. static bfd_vma
  881. avr_get_stub_addr (bfd_vma srel,
  882. struct elf32_avr_link_hash_table *htab)
  883. {
  884. unsigned int sindex;
  885. bfd_vma stub_sec_addr =
  886. (htab->stub_sec->output_section->vma +
  887. htab->stub_sec->output_offset);
  888. for (sindex = 0; sindex < htab->amt_max_entry_cnt; sindex ++)
  889. if (htab->amt_destination_addr[sindex] == srel)
  890. return htab->amt_stub_offsets[sindex] + stub_sec_addr;
  891. /* Return an address that could not be reached by 16 bit relocs. */
  892. return 0x020000;
  893. }
  894. /* Perform a diff relocation. Nothing to do, as the difference value is already
  895. written into the section's contents. */
  896. static bfd_reloc_status_type
  897. bfd_elf_avr_diff_reloc (bfd *abfd ATTRIBUTE_UNUSED,
  898. arelent *reloc_entry ATTRIBUTE_UNUSED,
  899. asymbol *symbol ATTRIBUTE_UNUSED,
  900. void *data ATTRIBUTE_UNUSED,
  901. asection *input_section ATTRIBUTE_UNUSED,
  902. bfd *output_bfd ATTRIBUTE_UNUSED,
  903. char **error_message ATTRIBUTE_UNUSED)
  904. {
  905. return bfd_reloc_ok;
  906. }
  907. /* Perform a single relocation. By default we use the standard BFD
  908. routines, but a few relocs, we have to do them ourselves. */
  909. static bfd_reloc_status_type
  910. avr_final_link_relocate (reloc_howto_type * howto,
  911. bfd * input_bfd,
  912. asection * input_section,
  913. bfd_byte * contents,
  914. Elf_Internal_Rela * rel,
  915. bfd_vma relocation,
  916. struct elf32_avr_link_hash_table * htab)
  917. {
  918. bfd_reloc_status_type r = bfd_reloc_ok;
  919. bfd_vma x;
  920. bfd_signed_vma srel;
  921. bfd_signed_vma reloc_addr;
  922. bfd_boolean use_stubs = FALSE;
  923. /* Usually is 0, unless we are generating code for a bootloader. */
  924. bfd_signed_vma base_addr = htab->vector_base;
  925. /* Absolute addr of the reloc in the final excecutable. */
  926. reloc_addr = rel->r_offset + input_section->output_section->vma
  927. + input_section->output_offset;
  928. switch (howto->type)
  929. {
  930. case R_AVR_7_PCREL:
  931. contents += rel->r_offset;
  932. srel = (bfd_signed_vma) relocation;
  933. srel += rel->r_addend;
  934. srel -= rel->r_offset;
  935. srel -= 2; /* Branch instructions add 2 to the PC... */
  936. srel -= (input_section->output_section->vma +
  937. input_section->output_offset);
  938. if (srel & 1)
  939. return bfd_reloc_outofrange;
  940. if (srel > ((1 << 7) - 1) || (srel < - (1 << 7)))
  941. return bfd_reloc_overflow;
  942. x = bfd_get_16 (input_bfd, contents);
  943. x = (x & 0xfc07) | (((srel >> 1) << 3) & 0x3f8);
  944. bfd_put_16 (input_bfd, x, contents);
  945. break;
  946. case R_AVR_13_PCREL:
  947. contents += rel->r_offset;
  948. srel = (bfd_signed_vma) relocation;
  949. srel += rel->r_addend;
  950. srel -= rel->r_offset;
  951. srel -= 2; /* Branch instructions add 2 to the PC... */
  952. srel -= (input_section->output_section->vma +
  953. input_section->output_offset);
  954. if (srel & 1)
  955. return bfd_reloc_outofrange;
  956. srel = avr_relative_distance_considering_wrap_around (srel);
  957. /* AVR addresses commands as words. */
  958. srel >>= 1;
  959. /* Check for overflow. */
  960. if (srel < -2048 || srel > 2047)
  961. {
  962. /* Relative distance is too large. */
  963. /* Always apply WRAPAROUND for avr2, avr25, and avr4. */
  964. switch (bfd_get_mach (input_bfd))
  965. {
  966. case bfd_mach_avr2:
  967. case bfd_mach_avr25:
  968. case bfd_mach_avr4:
  969. break;
  970. default:
  971. return bfd_reloc_overflow;
  972. }
  973. }
  974. x = bfd_get_16 (input_bfd, contents);
  975. x = (x & 0xf000) | (srel & 0xfff);
  976. bfd_put_16 (input_bfd, x, contents);
  977. break;
  978. case R_AVR_LO8_LDI:
  979. contents += rel->r_offset;
  980. srel = (bfd_signed_vma) relocation + rel->r_addend;
  981. x = bfd_get_16 (input_bfd, contents);
  982. x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
  983. bfd_put_16 (input_bfd, x, contents);
  984. break;
  985. case R_AVR_LDI:
  986. contents += rel->r_offset;
  987. srel = (bfd_signed_vma) relocation + rel->r_addend;
  988. if (((srel > 0) && (srel & 0xffff) > 255)
  989. || ((srel < 0) && ((-srel) & 0xffff) > 128))
  990. /* Remove offset for data/eeprom section. */
  991. return bfd_reloc_overflow;
  992. x = bfd_get_16 (input_bfd, contents);
  993. x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
  994. bfd_put_16 (input_bfd, x, contents);
  995. break;
  996. case R_AVR_6:
  997. contents += rel->r_offset;
  998. srel = (bfd_signed_vma) relocation + rel->r_addend;
  999. if (((srel & 0xffff) > 63) || (srel < 0))
  1000. /* Remove offset for data/eeprom section. */
  1001. return bfd_reloc_overflow;
  1002. x = bfd_get_16 (input_bfd, contents);
  1003. x = (x & 0xd3f8) | ((srel & 7) | ((srel & (3 << 3)) << 7)
  1004. | ((srel & (1 << 5)) << 8));
  1005. bfd_put_16 (input_bfd, x, contents);
  1006. break;
  1007. case R_AVR_6_ADIW:
  1008. contents += rel->r_offset;
  1009. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1010. if (((srel & 0xffff) > 63) || (srel < 0))
  1011. /* Remove offset for data/eeprom section. */
  1012. return bfd_reloc_overflow;
  1013. x = bfd_get_16 (input_bfd, contents);
  1014. x = (x & 0xff30) | (srel & 0xf) | ((srel & 0x30) << 2);
  1015. bfd_put_16 (input_bfd, x, contents);
  1016. break;
  1017. case R_AVR_HI8_LDI:
  1018. contents += rel->r_offset;
  1019. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1020. srel = (srel >> 8) & 0xff;
  1021. x = bfd_get_16 (input_bfd, contents);
  1022. x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
  1023. bfd_put_16 (input_bfd, x, contents);
  1024. break;
  1025. case R_AVR_HH8_LDI:
  1026. contents += rel->r_offset;
  1027. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1028. srel = (srel >> 16) & 0xff;
  1029. x = bfd_get_16 (input_bfd, contents);
  1030. x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
  1031. bfd_put_16 (input_bfd, x, contents);
  1032. break;
  1033. case R_AVR_MS8_LDI:
  1034. contents += rel->r_offset;
  1035. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1036. srel = (srel >> 24) & 0xff;
  1037. x = bfd_get_16 (input_bfd, contents);
  1038. x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
  1039. bfd_put_16 (input_bfd, x, contents);
  1040. break;
  1041. case R_AVR_LO8_LDI_NEG:
  1042. contents += rel->r_offset;
  1043. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1044. srel = -srel;
  1045. x = bfd_get_16 (input_bfd, contents);
  1046. x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
  1047. bfd_put_16 (input_bfd, x, contents);
  1048. break;
  1049. case R_AVR_HI8_LDI_NEG:
  1050. contents += rel->r_offset;
  1051. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1052. srel = -srel;
  1053. srel = (srel >> 8) & 0xff;
  1054. x = bfd_get_16 (input_bfd, contents);
  1055. x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
  1056. bfd_put_16 (input_bfd, x, contents);
  1057. break;
  1058. case R_AVR_HH8_LDI_NEG:
  1059. contents += rel->r_offset;
  1060. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1061. srel = -srel;
  1062. srel = (srel >> 16) & 0xff;
  1063. x = bfd_get_16 (input_bfd, contents);
  1064. x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
  1065. bfd_put_16 (input_bfd, x, contents);
  1066. break;
  1067. case R_AVR_MS8_LDI_NEG:
  1068. contents += rel->r_offset;
  1069. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1070. srel = -srel;
  1071. srel = (srel >> 24) & 0xff;
  1072. x = bfd_get_16 (input_bfd, contents);
  1073. x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
  1074. bfd_put_16 (input_bfd, x, contents);
  1075. break;
  1076. case R_AVR_LO8_LDI_GS:
  1077. use_stubs = (!htab->no_stubs);
  1078. /* Fall through. */
  1079. case R_AVR_LO8_LDI_PM:
  1080. contents += rel->r_offset;
  1081. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1082. if (use_stubs
  1083. && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
  1084. {
  1085. bfd_vma old_srel = srel;
  1086. /* We need to use the address of the stub instead. */
  1087. srel = avr_get_stub_addr (srel, htab);
  1088. if (debug_stubs)
  1089. printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
  1090. "reloc at address 0x%x.\n",
  1091. (unsigned int) srel,
  1092. (unsigned int) old_srel,
  1093. (unsigned int) reloc_addr);
  1094. if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
  1095. return bfd_reloc_outofrange;
  1096. }
  1097. if (srel & 1)
  1098. return bfd_reloc_outofrange;
  1099. srel = srel >> 1;
  1100. x = bfd_get_16 (input_bfd, contents);
  1101. x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
  1102. bfd_put_16 (input_bfd, x, contents);
  1103. break;
  1104. case R_AVR_HI8_LDI_GS:
  1105. use_stubs = (!htab->no_stubs);
  1106. /* Fall through. */
  1107. case R_AVR_HI8_LDI_PM:
  1108. contents += rel->r_offset;
  1109. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1110. if (use_stubs
  1111. && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
  1112. {
  1113. bfd_vma old_srel = srel;
  1114. /* We need to use the address of the stub instead. */
  1115. srel = avr_get_stub_addr (srel, htab);
  1116. if (debug_stubs)
  1117. printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
  1118. "reloc at address 0x%x.\n",
  1119. (unsigned int) srel,
  1120. (unsigned int) old_srel,
  1121. (unsigned int) reloc_addr);
  1122. if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
  1123. return bfd_reloc_outofrange;
  1124. }
  1125. if (srel & 1)
  1126. return bfd_reloc_outofrange;
  1127. srel = srel >> 1;
  1128. srel = (srel >> 8) & 0xff;
  1129. x = bfd_get_16 (input_bfd, contents);
  1130. x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
  1131. bfd_put_16 (input_bfd, x, contents);
  1132. break;
  1133. case R_AVR_HH8_LDI_PM:
  1134. contents += rel->r_offset;
  1135. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1136. if (srel & 1)
  1137. return bfd_reloc_outofrange;
  1138. srel = srel >> 1;
  1139. srel = (srel >> 16) & 0xff;
  1140. x = bfd_get_16 (input_bfd, contents);
  1141. x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
  1142. bfd_put_16 (input_bfd, x, contents);
  1143. break;
  1144. case R_AVR_LO8_LDI_PM_NEG:
  1145. contents += rel->r_offset;
  1146. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1147. srel = -srel;
  1148. if (srel & 1)
  1149. return bfd_reloc_outofrange;
  1150. srel = srel >> 1;
  1151. x = bfd_get_16 (input_bfd, contents);
  1152. x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
  1153. bfd_put_16 (input_bfd, x, contents);
  1154. break;
  1155. case R_AVR_HI8_LDI_PM_NEG:
  1156. contents += rel->r_offset;
  1157. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1158. srel = -srel;
  1159. if (srel & 1)
  1160. return bfd_reloc_outofrange;
  1161. srel = srel >> 1;
  1162. srel = (srel >> 8) & 0xff;
  1163. x = bfd_get_16 (input_bfd, contents);
  1164. x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
  1165. bfd_put_16 (input_bfd, x, contents);
  1166. break;
  1167. case R_AVR_HH8_LDI_PM_NEG:
  1168. contents += rel->r_offset;
  1169. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1170. srel = -srel;
  1171. if (srel & 1)
  1172. return bfd_reloc_outofrange;
  1173. srel = srel >> 1;
  1174. srel = (srel >> 16) & 0xff;
  1175. x = bfd_get_16 (input_bfd, contents);
  1176. x = (x & 0xf0f0) | (srel & 0xf) | ((srel << 4) & 0xf00);
  1177. bfd_put_16 (input_bfd, x, contents);
  1178. break;
  1179. case R_AVR_CALL:
  1180. contents += rel->r_offset;
  1181. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1182. if (srel & 1)
  1183. return bfd_reloc_outofrange;
  1184. srel = srel >> 1;
  1185. x = bfd_get_16 (input_bfd, contents);
  1186. x |= ((srel & 0x10000) | ((srel << 3) & 0x1f00000)) >> 16;
  1187. bfd_put_16 (input_bfd, x, contents);
  1188. bfd_put_16 (input_bfd, (bfd_vma) srel & 0xffff, contents+2);
  1189. break;
  1190. case R_AVR_16_PM:
  1191. use_stubs = (!htab->no_stubs);
  1192. contents += rel->r_offset;
  1193. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1194. if (use_stubs
  1195. && avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
  1196. {
  1197. bfd_vma old_srel = srel;
  1198. /* We need to use the address of the stub instead. */
  1199. srel = avr_get_stub_addr (srel,htab);
  1200. if (debug_stubs)
  1201. printf ("LD: Using jump stub (at 0x%x) with destination 0x%x for "
  1202. "reloc at address 0x%x.\n",
  1203. (unsigned int) srel,
  1204. (unsigned int) old_srel,
  1205. (unsigned int) reloc_addr);
  1206. if (avr_stub_is_required_for_16_bit_reloc (srel - base_addr))
  1207. return bfd_reloc_outofrange;
  1208. }
  1209. if (srel & 1)
  1210. return bfd_reloc_outofrange;
  1211. srel = srel >> 1;
  1212. bfd_put_16 (input_bfd, (bfd_vma) srel &0x00ffff, contents);
  1213. break;
  1214. case R_AVR_DIFF8:
  1215. case R_AVR_DIFF16:
  1216. case R_AVR_DIFF32:
  1217. /* Nothing to do here, as contents already contains the diff value. */
  1218. r = bfd_reloc_ok;
  1219. break;
  1220. case R_AVR_LDS_STS_16:
  1221. contents += rel->r_offset;
  1222. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1223. if ((srel & 0xFFFF) < 0x40 || (srel & 0xFFFF) > 0xbf)
  1224. return bfd_reloc_outofrange;
  1225. srel = srel & 0x7f;
  1226. x = bfd_get_16 (input_bfd, contents);
  1227. x |= (srel & 0x0f) | ((srel & 0x30) << 5) | ((srel & 0x40) << 2);
  1228. bfd_put_16 (input_bfd, x, contents);
  1229. break;
  1230. case R_AVR_PORT6:
  1231. contents += rel->r_offset;
  1232. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1233. if ((srel & 0xffff) > 0x3f)
  1234. return bfd_reloc_outofrange;
  1235. x = bfd_get_16 (input_bfd, contents);
  1236. x = (x & 0xf9f0) | ((srel & 0x30) << 5) | (srel & 0x0f);
  1237. bfd_put_16 (input_bfd, x, contents);
  1238. break;
  1239. case R_AVR_PORT5:
  1240. contents += rel->r_offset;
  1241. srel = (bfd_signed_vma) relocation + rel->r_addend;
  1242. if ((srel & 0xffff) > 0x1f)
  1243. return bfd_reloc_outofrange;
  1244. x = bfd_get_16 (input_bfd, contents);
  1245. x = (x & 0xff07) | ((srel & 0x1f) << 3);
  1246. bfd_put_16 (input_bfd, x, contents);
  1247. break;
  1248. default:
  1249. r = _bfd_final_link_relocate (howto, input_bfd, input_section,
  1250. contents, rel->r_offset,
  1251. relocation, rel->r_addend);
  1252. }
  1253. return r;
  1254. }
  1255. /* Relocate an AVR ELF section. */
  1256. static bfd_boolean
  1257. elf32_avr_relocate_section (bfd *output_bfd ATTRIBUTE_UNUSED,
  1258. struct bfd_link_info *info,
  1259. bfd *input_bfd,
  1260. asection *input_section,
  1261. bfd_byte *contents,
  1262. Elf_Internal_Rela *relocs,
  1263. Elf_Internal_Sym *local_syms,
  1264. asection **local_sections)
  1265. {
  1266. Elf_Internal_Shdr * symtab_hdr;
  1267. struct elf_link_hash_entry ** sym_hashes;
  1268. Elf_Internal_Rela * rel;
  1269. Elf_Internal_Rela * relend;
  1270. struct elf32_avr_link_hash_table * htab = avr_link_hash_table (info);
  1271. if (htab == NULL)
  1272. return FALSE;
  1273. symtab_hdr = & elf_tdata (input_bfd)->symtab_hdr;
  1274. sym_hashes = elf_sym_hashes (input_bfd);
  1275. relend = relocs + input_section->reloc_count;
  1276. for (rel = relocs; rel < relend; rel ++)
  1277. {
  1278. reloc_howto_type * howto;
  1279. unsigned long r_symndx;
  1280. Elf_Internal_Sym * sym;
  1281. asection * sec;
  1282. struct elf_link_hash_entry * h;
  1283. bfd_vma relocation;
  1284. bfd_reloc_status_type r;
  1285. const char * name;
  1286. int r_type;
  1287. r_type = ELF32_R_TYPE (rel->r_info);
  1288. r_symndx = ELF32_R_SYM (rel->r_info);
  1289. howto = elf_avr_howto_table + r_type;
  1290. h = NULL;
  1291. sym = NULL;
  1292. sec = NULL;
  1293. if (r_symndx < symtab_hdr->sh_info)
  1294. {
  1295. sym = local_syms + r_symndx;
  1296. sec = local_sections [r_symndx];
  1297. relocation = _bfd_elf_rela_local_sym (output_bfd, sym, &sec, rel);
  1298. name = bfd_elf_string_from_elf_section
  1299. (input_bfd, symtab_hdr->sh_link, sym->st_name);
  1300. name = (name == NULL) ? bfd_section_name (input_bfd, sec) : name;
  1301. }
  1302. else
  1303. {
  1304. bfd_boolean unresolved_reloc, warned, ignored;
  1305. RELOC_FOR_GLOBAL_SYMBOL (info, input_bfd, input_section, rel,
  1306. r_symndx, symtab_hdr, sym_hashes,
  1307. h, sec, relocation,
  1308. unresolved_reloc, warned, ignored);
  1309. name = h->root.root.string;
  1310. }
  1311. if (sec != NULL && discarded_section (sec))
  1312. RELOC_AGAINST_DISCARDED_SECTION (info, input_bfd, input_section,
  1313. rel, 1, relend, howto, 0, contents);
  1314. if (bfd_link_relocatable (info))
  1315. continue;
  1316. r = avr_final_link_relocate (howto, input_bfd, input_section,
  1317. contents, rel, relocation, htab);
  1318. if (r != bfd_reloc_ok)
  1319. {
  1320. const char * msg = (const char *) NULL;
  1321. switch (r)
  1322. {
  1323. case bfd_reloc_overflow:
  1324. r = info->callbacks->reloc_overflow
  1325. (info, (h ? &h->root : NULL),
  1326. name, howto->name, (bfd_vma) 0,
  1327. input_bfd, input_section, rel->r_offset);
  1328. break;
  1329. case bfd_reloc_undefined:
  1330. r = info->callbacks->undefined_symbol
  1331. (info, name, input_bfd, input_section, rel->r_offset, TRUE);
  1332. break;
  1333. case bfd_reloc_outofrange:
  1334. msg = _("internal error: out of range error");
  1335. break;
  1336. case bfd_reloc_notsupported:
  1337. msg = _("internal error: unsupported relocation error");
  1338. break;
  1339. case bfd_reloc_dangerous:
  1340. msg = _("internal error: dangerous relocation");
  1341. break;
  1342. default:
  1343. msg = _("internal error: unknown error");
  1344. break;
  1345. }
  1346. if (msg)
  1347. r = info->callbacks->warning
  1348. (info, msg, name, input_bfd, input_section, rel->r_offset);
  1349. if (! r)
  1350. return FALSE;
  1351. }
  1352. }
  1353. return TRUE;
  1354. }
  1355. /* The final processing done just before writing out a AVR ELF object
  1356. file. This gets the AVR architecture right based on the machine
  1357. number. */
  1358. static void
  1359. bfd_elf_avr_final_write_processing (bfd *abfd,
  1360. bfd_boolean linker ATTRIBUTE_UNUSED)
  1361. {
  1362. unsigned long val;
  1363. switch (bfd_get_mach (abfd))
  1364. {
  1365. default:
  1366. case bfd_mach_avr2:
  1367. val = E_AVR_MACH_AVR2;
  1368. break;
  1369. case bfd_mach_avr1:
  1370. val = E_AVR_MACH_AVR1;
  1371. break;
  1372. case bfd_mach_avr25:
  1373. val = E_AVR_MACH_AVR25;
  1374. break;
  1375. case bfd_mach_avr3:
  1376. val = E_AVR_MACH_AVR3;
  1377. break;
  1378. case bfd_mach_avr31:
  1379. val = E_AVR_MACH_AVR31;
  1380. break;
  1381. case bfd_mach_avr35:
  1382. val = E_AVR_MACH_AVR35;
  1383. break;
  1384. case bfd_mach_avr4:
  1385. val = E_AVR_MACH_AVR4;
  1386. break;
  1387. case bfd_mach_avr5:
  1388. val = E_AVR_MACH_AVR5;
  1389. break;
  1390. case bfd_mach_avr51:
  1391. val = E_AVR_MACH_AVR51;
  1392. break;
  1393. case bfd_mach_avr6:
  1394. val = E_AVR_MACH_AVR6;
  1395. break;
  1396. case bfd_mach_avrxmega1:
  1397. val = E_AVR_MACH_XMEGA1;
  1398. break;
  1399. case bfd_mach_avrxmega2:
  1400. val = E_AVR_MACH_XMEGA2;
  1401. break;
  1402. case bfd_mach_avrxmega3:
  1403. val = E_AVR_MACH_XMEGA3;
  1404. break;
  1405. case bfd_mach_avrxmega4:
  1406. val = E_AVR_MACH_XMEGA4;
  1407. break;
  1408. case bfd_mach_avrxmega5:
  1409. val = E_AVR_MACH_XMEGA5;
  1410. break;
  1411. case bfd_mach_avrxmega6:
  1412. val = E_AVR_MACH_XMEGA6;
  1413. break;
  1414. case bfd_mach_avrxmega7:
  1415. val = E_AVR_MACH_XMEGA7;
  1416. break;
  1417. case bfd_mach_avrtiny:
  1418. val = E_AVR_MACH_AVRTINY;
  1419. break;
  1420. }
  1421. elf_elfheader (abfd)->e_machine = EM_AVR;
  1422. elf_elfheader (abfd)->e_flags &= ~ EF_AVR_MACH;
  1423. elf_elfheader (abfd)->e_flags |= val;
  1424. }
  1425. /* Set the right machine number. */
  1426. static bfd_boolean
  1427. elf32_avr_object_p (bfd *abfd)
  1428. {
  1429. unsigned int e_set = bfd_mach_avr2;
  1430. if (elf_elfheader (abfd)->e_machine == EM_AVR
  1431. || elf_elfheader (abfd)->e_machine == EM_AVR_OLD)
  1432. {
  1433. int e_mach = elf_elfheader (abfd)->e_flags & EF_AVR_MACH;
  1434. switch (e_mach)
  1435. {
  1436. default:
  1437. case E_AVR_MACH_AVR2:
  1438. e_set = bfd_mach_avr2;
  1439. break;
  1440. case E_AVR_MACH_AVR1:
  1441. e_set = bfd_mach_avr1;
  1442. break;
  1443. case E_AVR_MACH_AVR25:
  1444. e_set = bfd_mach_avr25;
  1445. break;
  1446. case E_AVR_MACH_AVR3:
  1447. e_set = bfd_mach_avr3;
  1448. break;
  1449. case E_AVR_MACH_AVR31:
  1450. e_set = bfd_mach_avr31;
  1451. break;
  1452. case E_AVR_MACH_AVR35:
  1453. e_set = bfd_mach_avr35;
  1454. break;
  1455. case E_AVR_MACH_AVR4:
  1456. e_set = bfd_mach_avr4;
  1457. break;
  1458. case E_AVR_MACH_AVR5:
  1459. e_set = bfd_mach_avr5;
  1460. break;
  1461. case E_AVR_MACH_AVR51:
  1462. e_set = bfd_mach_avr51;
  1463. break;
  1464. case E_AVR_MACH_AVR6:
  1465. e_set = bfd_mach_avr6;
  1466. break;
  1467. case E_AVR_MACH_XMEGA1:
  1468. e_set = bfd_mach_avrxmega1;
  1469. break;
  1470. case E_AVR_MACH_XMEGA2:
  1471. e_set = bfd_mach_avrxmega2;
  1472. break;
  1473. case E_AVR_MACH_XMEGA3:
  1474. e_set = bfd_mach_avrxmega3;
  1475. break;
  1476. case E_AVR_MACH_XMEGA4:
  1477. e_set = bfd_mach_avrxmega4;
  1478. break;
  1479. case E_AVR_MACH_XMEGA5:
  1480. e_set = bfd_mach_avrxmega5;
  1481. break;
  1482. case E_AVR_MACH_XMEGA6:
  1483. e_set = bfd_mach_avrxmega6;
  1484. break;
  1485. case E_AVR_MACH_XMEGA7:
  1486. e_set = bfd_mach_avrxmega7;
  1487. break;
  1488. case E_AVR_MACH_AVRTINY:
  1489. e_set = bfd_mach_avrtiny;
  1490. break;
  1491. }
  1492. }
  1493. return bfd_default_set_arch_mach (abfd, bfd_arch_avr,
  1494. e_set);
  1495. }
  1496. /* Returns whether the relocation type passed is a diff reloc. */
  1497. static bfd_boolean
  1498. elf32_avr_is_diff_reloc (Elf_Internal_Rela *irel)
  1499. {
  1500. return (ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF8
  1501. ||ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF16
  1502. || ELF32_R_TYPE (irel->r_info) == R_AVR_DIFF32);
  1503. }
  1504. /* Reduce the diff value written in the section by count if the shrinked
  1505. insn address happens to fall between the two symbols for which this
  1506. diff reloc was emitted. */
  1507. static void
  1508. elf32_avr_adjust_diff_reloc_value (bfd *abfd,
  1509. struct bfd_section *isec,
  1510. Elf_Internal_Rela *irel,
  1511. bfd_vma symval,
  1512. bfd_vma shrinked_insn_address,
  1513. int count)
  1514. {
  1515. unsigned char *reloc_contents = NULL;
  1516. unsigned char *isec_contents = elf_section_data (isec)->this_hdr.contents;
  1517. if (isec_contents == NULL)
  1518. {
  1519. if (! bfd_malloc_and_get_section (abfd, isec, &isec_contents))
  1520. return;
  1521. elf_section_data (isec)->this_hdr.contents = isec_contents;
  1522. }
  1523. reloc_contents = isec_contents + irel->r_offset;
  1524. /* Read value written in object file. */
  1525. bfd_vma x = 0;
  1526. switch (ELF32_R_TYPE (irel->r_info))
  1527. {
  1528. case R_AVR_DIFF8:
  1529. {
  1530. x = *reloc_contents;
  1531. break;
  1532. }
  1533. case R_AVR_DIFF16:
  1534. {
  1535. x = bfd_get_16 (abfd, reloc_contents);
  1536. break;
  1537. }
  1538. case R_AVR_DIFF32:
  1539. {
  1540. x = bfd_get_32 (abfd, reloc_contents);
  1541. break;
  1542. }
  1543. default:
  1544. {
  1545. BFD_FAIL();
  1546. }
  1547. }
  1548. /* For a diff reloc sym1 - sym2 the diff at assembly time (x) is written
  1549. into the object file at the reloc offset. sym2's logical value is
  1550. symval (<start_of_section>) + reloc addend. Compute the start and end
  1551. addresses and check if the shrinked insn falls between sym1 and sym2. */
  1552. bfd_vma end_address = symval + irel->r_addend;
  1553. bfd_vma start_address = end_address - x;
  1554. /* Reduce the diff value by count bytes and write it back into section
  1555. contents. */
  1556. if (shrinked_insn_address >= start_address
  1557. && shrinked_insn_address <= end_address)
  1558. {
  1559. switch (ELF32_R_TYPE (irel->r_info))
  1560. {
  1561. case R_AVR_DIFF8:
  1562. {
  1563. *reloc_contents = (x - count);
  1564. break;
  1565. }
  1566. case R_AVR_DIFF16:
  1567. {
  1568. bfd_put_16 (abfd, (x - count) & 0xFFFF, reloc_contents);
  1569. break;
  1570. }
  1571. case R_AVR_DIFF32:
  1572. {
  1573. bfd_put_32 (abfd, (x - count) & 0xFFFFFFFF, reloc_contents);
  1574. break;
  1575. }
  1576. default:
  1577. {
  1578. BFD_FAIL();
  1579. }
  1580. }
  1581. }
  1582. }
  1583. /* Delete some bytes from a section while changing the size of an instruction.
  1584. The parameter "addr" denotes the section-relative offset pointing just
  1585. behind the shrinked instruction. "addr+count" point at the first
  1586. byte just behind the original unshrinked instruction. */
  1587. static bfd_boolean
  1588. elf32_avr_relax_delete_bytes (bfd *abfd,
  1589. asection *sec,
  1590. bfd_vma addr,
  1591. int count)
  1592. {
  1593. Elf_Internal_Shdr *symtab_hdr;
  1594. unsigned int sec_shndx;
  1595. bfd_byte *contents;
  1596. Elf_Internal_Rela *irel, *irelend;
  1597. Elf_Internal_Sym *isym;
  1598. Elf_Internal_Sym *isymbuf = NULL;
  1599. bfd_vma toaddr;
  1600. struct elf_link_hash_entry **sym_hashes;
  1601. struct elf_link_hash_entry **end_hashes;
  1602. unsigned int symcount;
  1603. struct avr_relax_info *relax_info;
  1604. struct avr_property_record *prop_record = NULL;
  1605. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1606. sec_shndx = _bfd_elf_section_from_bfd_section (abfd, sec);
  1607. contents = elf_section_data (sec)->this_hdr.contents;
  1608. relax_info = get_avr_relax_info (sec);
  1609. toaddr = sec->size;
  1610. if (relax_info->records.count > 0)
  1611. {
  1612. /* There should be no property record within the range of deleted
  1613. bytes, however, there might be a property record for ADDR, this is
  1614. how we handle alignment directives.
  1615. Find the next (if any) property record after the deleted bytes. */
  1616. unsigned int i;
  1617. for (i = 0; i < relax_info->records.count; ++i)
  1618. {
  1619. bfd_vma offset = relax_info->records.items [i].offset;
  1620. BFD_ASSERT (offset <= addr || offset >= (addr + count));
  1621. if (offset >= (addr + count))
  1622. {
  1623. prop_record = &relax_info->records.items [i];
  1624. toaddr = offset;
  1625. break;
  1626. }
  1627. }
  1628. }
  1629. irel = elf_section_data (sec)->relocs;
  1630. irelend = irel + sec->reloc_count;
  1631. /* Actually delete the bytes. */
  1632. if (toaddr - addr - count > 0)
  1633. memmove (contents + addr, contents + addr + count,
  1634. (size_t) (toaddr - addr - count));
  1635. if (prop_record == NULL)
  1636. sec->size -= count;
  1637. else
  1638. {
  1639. /* Use the property record to fill in the bytes we've opened up. */
  1640. int fill = 0;
  1641. switch (prop_record->type)
  1642. {
  1643. case RECORD_ORG_AND_FILL:
  1644. fill = prop_record->data.org.fill;
  1645. /* Fall through. */
  1646. case RECORD_ORG:
  1647. break;
  1648. case RECORD_ALIGN_AND_FILL:
  1649. fill = prop_record->data.align.fill;
  1650. /* Fall through. */
  1651. case RECORD_ALIGN:
  1652. prop_record->data.align.preceding_deleted += count;
  1653. break;
  1654. };
  1655. memset (contents + toaddr - count, fill, count);
  1656. /* Adjust the TOADDR to avoid moving symbols located at the address
  1657. of the property record, which has not moved. */
  1658. toaddr -= count;
  1659. }
  1660. /* Adjust all the reloc addresses. */
  1661. for (irel = elf_section_data (sec)->relocs; irel < irelend; irel++)
  1662. {
  1663. bfd_vma old_reloc_address;
  1664. old_reloc_address = (sec->output_section->vma
  1665. + sec->output_offset + irel->r_offset);
  1666. /* Get the new reloc address. */
  1667. if ((irel->r_offset > addr
  1668. && irel->r_offset < toaddr))
  1669. {
  1670. if (debug_relax)
  1671. printf ("Relocation at address 0x%x needs to be moved.\n"
  1672. "Old section offset: 0x%x, New section offset: 0x%x \n",
  1673. (unsigned int) old_reloc_address,
  1674. (unsigned int) irel->r_offset,
  1675. (unsigned int) ((irel->r_offset) - count));
  1676. irel->r_offset -= count;
  1677. }
  1678. }
  1679. /* The reloc's own addresses are now ok. However, we need to readjust
  1680. the reloc's addend, i.e. the reloc's value if two conditions are met:
  1681. 1.) the reloc is relative to a symbol in this section that
  1682. is located in front of the shrinked instruction
  1683. 2.) symbol plus addend end up behind the shrinked instruction.
  1684. The most common case where this happens are relocs relative to
  1685. the section-start symbol.
  1686. This step needs to be done for all of the sections of the bfd. */
  1687. {
  1688. struct bfd_section *isec;
  1689. for (isec = abfd->sections; isec; isec = isec->next)
  1690. {
  1691. bfd_vma symval;
  1692. bfd_vma shrinked_insn_address;
  1693. if (isec->reloc_count == 0)
  1694. continue;
  1695. shrinked_insn_address = (sec->output_section->vma
  1696. + sec->output_offset + addr - count);
  1697. irel = elf_section_data (isec)->relocs;
  1698. /* PR 12161: Read in the relocs for this section if necessary. */
  1699. if (irel == NULL)
  1700. irel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE);
  1701. for (irelend = irel + isec->reloc_count;
  1702. irel < irelend;
  1703. irel++)
  1704. {
  1705. /* Read this BFD's local symbols if we haven't done
  1706. so already. */
  1707. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  1708. {
  1709. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  1710. if (isymbuf == NULL)
  1711. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  1712. symtab_hdr->sh_info, 0,
  1713. NULL, NULL, NULL);
  1714. if (isymbuf == NULL)
  1715. return FALSE;
  1716. }
  1717. /* Get the value of the symbol referred to by the reloc. */
  1718. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  1719. {
  1720. /* A local symbol. */
  1721. asection *sym_sec;
  1722. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  1723. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  1724. symval = isym->st_value;
  1725. /* If the reloc is absolute, it will not have
  1726. a symbol or section associated with it. */
  1727. if (sym_sec == sec)
  1728. {
  1729. symval += sym_sec->output_section->vma
  1730. + sym_sec->output_offset;
  1731. if (debug_relax)
  1732. printf ("Checking if the relocation's "
  1733. "addend needs corrections.\n"
  1734. "Address of anchor symbol: 0x%x \n"
  1735. "Address of relocation target: 0x%x \n"
  1736. "Address of relaxed insn: 0x%x \n",
  1737. (unsigned int) symval,
  1738. (unsigned int) (symval + irel->r_addend),
  1739. (unsigned int) shrinked_insn_address);
  1740. if (symval <= shrinked_insn_address
  1741. && (symval + irel->r_addend) > shrinked_insn_address)
  1742. {
  1743. if (elf32_avr_is_diff_reloc (irel))
  1744. {
  1745. elf32_avr_adjust_diff_reloc_value (abfd, isec, irel,
  1746. symval,
  1747. shrinked_insn_address,
  1748. count);
  1749. }
  1750. irel->r_addend -= count;
  1751. if (debug_relax)
  1752. printf ("Relocation's addend needed to be fixed \n");
  1753. }
  1754. }
  1755. /* else...Reference symbol is absolute. No adjustment needed. */
  1756. }
  1757. /* else...Reference symbol is extern. No need for adjusting
  1758. the addend. */
  1759. }
  1760. }
  1761. }
  1762. /* Adjust the local symbols defined in this section. */
  1763. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  1764. /* Fix PR 9841, there may be no local symbols. */
  1765. if (isym != NULL)
  1766. {
  1767. Elf_Internal_Sym *isymend;
  1768. isymend = isym + symtab_hdr->sh_info;
  1769. for (; isym < isymend; isym++)
  1770. {
  1771. if (isym->st_shndx == sec_shndx)
  1772. {
  1773. if (isym->st_value > addr
  1774. && isym->st_value <= toaddr)
  1775. isym->st_value -= count;
  1776. if (isym->st_value <= addr
  1777. && isym->st_value + isym->st_size > addr)
  1778. {
  1779. /* If this assert fires then we have a symbol that ends
  1780. part way through an instruction. Does that make
  1781. sense? */
  1782. BFD_ASSERT (isym->st_value + isym->st_size >= addr + count);
  1783. isym->st_size -= count;
  1784. }
  1785. }
  1786. }
  1787. }
  1788. /* Now adjust the global symbols defined in this section. */
  1789. symcount = (symtab_hdr->sh_size / sizeof (Elf32_External_Sym)
  1790. - symtab_hdr->sh_info);
  1791. sym_hashes = elf_sym_hashes (abfd);
  1792. end_hashes = sym_hashes + symcount;
  1793. for (; sym_hashes < end_hashes; sym_hashes++)
  1794. {
  1795. struct elf_link_hash_entry *sym_hash = *sym_hashes;
  1796. if ((sym_hash->root.type == bfd_link_hash_defined
  1797. || sym_hash->root.type == bfd_link_hash_defweak)
  1798. && sym_hash->root.u.def.section == sec)
  1799. {
  1800. if (sym_hash->root.u.def.value > addr
  1801. && sym_hash->root.u.def.value <= toaddr)
  1802. sym_hash->root.u.def.value -= count;
  1803. if (sym_hash->root.u.def.value <= addr
  1804. && (sym_hash->root.u.def.value + sym_hash->size > addr))
  1805. {
  1806. /* If this assert fires then we have a symbol that ends
  1807. part way through an instruction. Does that make
  1808. sense? */
  1809. BFD_ASSERT (sym_hash->root.u.def.value + sym_hash->size
  1810. >= addr + count);
  1811. sym_hash->size -= count;
  1812. }
  1813. }
  1814. }
  1815. return TRUE;
  1816. }
  1817. static Elf_Internal_Sym *
  1818. retrieve_local_syms (bfd *input_bfd)
  1819. {
  1820. Elf_Internal_Shdr *symtab_hdr;
  1821. Elf_Internal_Sym *isymbuf;
  1822. size_t locsymcount;
  1823. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  1824. locsymcount = symtab_hdr->sh_info;
  1825. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  1826. if (isymbuf == NULL && locsymcount != 0)
  1827. isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr, locsymcount, 0,
  1828. NULL, NULL, NULL);
  1829. /* Save the symbols for this input file so they won't be read again. */
  1830. if (isymbuf && isymbuf != (Elf_Internal_Sym *) symtab_hdr->contents)
  1831. symtab_hdr->contents = (unsigned char *) isymbuf;
  1832. return isymbuf;
  1833. }
  1834. /* Get the input section for a given symbol index.
  1835. If the symbol is:
  1836. . a section symbol, return the section;
  1837. . a common symbol, return the common section;
  1838. . an undefined symbol, return the undefined section;
  1839. . an indirect symbol, follow the links;
  1840. . an absolute value, return the absolute section. */
  1841. static asection *
  1842. get_elf_r_symndx_section (bfd *abfd, unsigned long r_symndx)
  1843. {
  1844. Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1845. asection *target_sec = NULL;
  1846. if (r_symndx < symtab_hdr->sh_info)
  1847. {
  1848. Elf_Internal_Sym *isymbuf;
  1849. unsigned int section_index;
  1850. isymbuf = retrieve_local_syms (abfd);
  1851. section_index = isymbuf[r_symndx].st_shndx;
  1852. if (section_index == SHN_UNDEF)
  1853. target_sec = bfd_und_section_ptr;
  1854. else if (section_index == SHN_ABS)
  1855. target_sec = bfd_abs_section_ptr;
  1856. else if (section_index == SHN_COMMON)
  1857. target_sec = bfd_com_section_ptr;
  1858. else
  1859. target_sec = bfd_section_from_elf_index (abfd, section_index);
  1860. }
  1861. else
  1862. {
  1863. unsigned long indx = r_symndx - symtab_hdr->sh_info;
  1864. struct elf_link_hash_entry *h = elf_sym_hashes (abfd)[indx];
  1865. while (h->root.type == bfd_link_hash_indirect
  1866. || h->root.type == bfd_link_hash_warning)
  1867. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1868. switch (h->root.type)
  1869. {
  1870. case bfd_link_hash_defined:
  1871. case bfd_link_hash_defweak:
  1872. target_sec = h->root.u.def.section;
  1873. break;
  1874. case bfd_link_hash_common:
  1875. target_sec = bfd_com_section_ptr;
  1876. break;
  1877. case bfd_link_hash_undefined:
  1878. case bfd_link_hash_undefweak:
  1879. target_sec = bfd_und_section_ptr;
  1880. break;
  1881. default: /* New indirect warning. */
  1882. target_sec = bfd_und_section_ptr;
  1883. break;
  1884. }
  1885. }
  1886. return target_sec;
  1887. }
  1888. /* Get the section-relative offset for a symbol number. */
  1889. static bfd_vma
  1890. get_elf_r_symndx_offset (bfd *abfd, unsigned long r_symndx)
  1891. {
  1892. Elf_Internal_Shdr *symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  1893. bfd_vma offset = 0;
  1894. if (r_symndx < symtab_hdr->sh_info)
  1895. {
  1896. Elf_Internal_Sym *isymbuf;
  1897. isymbuf = retrieve_local_syms (abfd);
  1898. offset = isymbuf[r_symndx].st_value;
  1899. }
  1900. else
  1901. {
  1902. unsigned long indx = r_symndx - symtab_hdr->sh_info;
  1903. struct elf_link_hash_entry *h =
  1904. elf_sym_hashes (abfd)[indx];
  1905. while (h->root.type == bfd_link_hash_indirect
  1906. || h->root.type == bfd_link_hash_warning)
  1907. h = (struct elf_link_hash_entry *) h->root.u.i.link;
  1908. if (h->root.type == bfd_link_hash_defined
  1909. || h->root.type == bfd_link_hash_defweak)
  1910. offset = h->root.u.def.value;
  1911. }
  1912. return offset;
  1913. }
  1914. /* Iterate over the property records in R_LIST, and copy each record into
  1915. the list of records within the relaxation information for the section to
  1916. which the record applies. */
  1917. static void
  1918. avr_elf32_assign_records_to_sections (struct avr_property_record_list *r_list)
  1919. {
  1920. unsigned int i;
  1921. for (i = 0; i < r_list->record_count; ++i)
  1922. {
  1923. struct avr_relax_info *relax_info;
  1924. relax_info = get_avr_relax_info (r_list->records [i].section);
  1925. BFD_ASSERT (relax_info != NULL);
  1926. if (relax_info->records.count
  1927. == relax_info->records.allocated)
  1928. {
  1929. /* Allocate more space. */
  1930. bfd_size_type size;
  1931. relax_info->records.allocated += 10;
  1932. size = (sizeof (struct avr_property_record)
  1933. * relax_info->records.allocated);
  1934. relax_info->records.items
  1935. = bfd_realloc (relax_info->records.items, size);
  1936. }
  1937. memcpy (&relax_info->records.items [relax_info->records.count],
  1938. &r_list->records [i],
  1939. sizeof (struct avr_property_record));
  1940. relax_info->records.count++;
  1941. }
  1942. }
  1943. /* Compare two STRUCT AVR_PROPERTY_RECORD in AP and BP, used as the
  1944. ordering callback from QSORT. */
  1945. static int
  1946. avr_property_record_compare (const void *ap, const void *bp)
  1947. {
  1948. const struct avr_property_record *a
  1949. = (struct avr_property_record *) ap;
  1950. const struct avr_property_record *b
  1951. = (struct avr_property_record *) bp;
  1952. if (a->offset != b->offset)
  1953. return (a->offset - b->offset);
  1954. if (a->section != b->section)
  1955. return (bfd_get_section_vma (a->section->owner, a->section)
  1956. - bfd_get_section_vma (b->section->owner, b->section));
  1957. return (a->type - b->type);
  1958. }
  1959. /* Load all of the avr property sections from all of the bfd objects
  1960. referenced from LINK_INFO. All of the records within each property
  1961. section are assigned to the STRUCT AVR_RELAX_INFO within the section
  1962. specific data of the appropriate section. */
  1963. static void
  1964. avr_load_all_property_sections (struct bfd_link_info *link_info)
  1965. {
  1966. bfd *abfd;
  1967. asection *sec;
  1968. /* Initialize the per-section relaxation info. */
  1969. for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
  1970. for (sec = abfd->sections; sec != NULL; sec = sec->next)
  1971. {
  1972. init_avr_relax_info (sec);
  1973. }
  1974. /* Load the descriptor tables from .avr.prop sections. */
  1975. for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
  1976. {
  1977. struct avr_property_record_list *r_list;
  1978. r_list = avr_elf32_load_property_records (abfd);
  1979. if (r_list != NULL)
  1980. avr_elf32_assign_records_to_sections (r_list);
  1981. free (r_list);
  1982. }
  1983. /* Now, for every section, ensure that the descriptor list in the
  1984. relaxation data is sorted by ascending offset within the section. */
  1985. for (abfd = link_info->input_bfds; abfd != NULL; abfd = abfd->link.next)
  1986. for (sec = abfd->sections; sec != NULL; sec = sec->next)
  1987. {
  1988. struct avr_relax_info *relax_info = get_avr_relax_info (sec);
  1989. if (relax_info && relax_info->records.count > 0)
  1990. {
  1991. unsigned int i;
  1992. qsort (relax_info->records.items,
  1993. relax_info->records.count,
  1994. sizeof (struct avr_property_record),
  1995. avr_property_record_compare);
  1996. /* For debug purposes, list all the descriptors. */
  1997. for (i = 0; i < relax_info->records.count; ++i)
  1998. {
  1999. switch (relax_info->records.items [i].type)
  2000. {
  2001. case RECORD_ORG:
  2002. break;
  2003. case RECORD_ORG_AND_FILL:
  2004. break;
  2005. case RECORD_ALIGN:
  2006. break;
  2007. case RECORD_ALIGN_AND_FILL:
  2008. break;
  2009. };
  2010. }
  2011. }
  2012. }
  2013. }
  2014. /* This function handles relaxing for the avr.
  2015. Many important relaxing opportunities within functions are already
  2016. realized by the compiler itself.
  2017. Here we try to replace call (4 bytes) -> rcall (2 bytes)
  2018. and jump -> rjmp (safes also 2 bytes).
  2019. As well we now optimize seqences of
  2020. - call/rcall function
  2021. - ret
  2022. to yield
  2023. - jmp/rjmp function
  2024. - ret
  2025. . In case that within a sequence
  2026. - jmp/rjmp label
  2027. - ret
  2028. the ret could no longer be reached it is optimized away. In order
  2029. to check if the ret is no longer needed, it is checked that the ret's address
  2030. is not the target of a branch or jump within the same section, it is checked
  2031. that there is no skip instruction before the jmp/rjmp and that there
  2032. is no local or global label place at the address of the ret.
  2033. We refrain from relaxing within sections ".vectors" and
  2034. ".jumptables" in order to maintain the position of the instructions.
  2035. There, however, we substitute jmp/call by a sequence rjmp,nop/rcall,nop
  2036. if possible. (In future one could possibly use the space of the nop
  2037. for the first instruction of the irq service function.
  2038. The .jumptables sections is meant to be used for a future tablejump variant
  2039. for the devices with 3-byte program counter where the table itself
  2040. contains 4-byte jump instructions whose relative offset must not
  2041. be changed. */
  2042. static bfd_boolean
  2043. elf32_avr_relax_section (bfd *abfd,
  2044. asection *sec,
  2045. struct bfd_link_info *link_info,
  2046. bfd_boolean *again)
  2047. {
  2048. Elf_Internal_Shdr *symtab_hdr;
  2049. Elf_Internal_Rela *internal_relocs;
  2050. Elf_Internal_Rela *irel, *irelend;
  2051. bfd_byte *contents = NULL;
  2052. Elf_Internal_Sym *isymbuf = NULL;
  2053. struct elf32_avr_link_hash_table *htab;
  2054. static bfd_boolean relaxation_initialised = FALSE;
  2055. if (!relaxation_initialised)
  2056. {
  2057. relaxation_initialised = TRUE;
  2058. /* Load entries from the .avr.prop sections. */
  2059. avr_load_all_property_sections (link_info);
  2060. }
  2061. /* If 'shrinkable' is FALSE, do not shrink by deleting bytes while
  2062. relaxing. Such shrinking can cause issues for the sections such
  2063. as .vectors and .jumptables. Instead the unused bytes should be
  2064. filled with nop instructions. */
  2065. bfd_boolean shrinkable = TRUE;
  2066. if (!strcmp (sec->name,".vectors")
  2067. || !strcmp (sec->name,".jumptables"))
  2068. shrinkable = FALSE;
  2069. if (bfd_link_relocatable (link_info))
  2070. (*link_info->callbacks->einfo)
  2071. (_("%P%F: --relax and -r may not be used together\n"));
  2072. htab = avr_link_hash_table (link_info);
  2073. if (htab == NULL)
  2074. return FALSE;
  2075. /* Assume nothing changes. */
  2076. *again = FALSE;
  2077. if ((!htab->no_stubs) && (sec == htab->stub_sec))
  2078. {
  2079. /* We are just relaxing the stub section.
  2080. Let's calculate the size needed again. */
  2081. bfd_size_type last_estimated_stub_section_size = htab->stub_sec->size;
  2082. if (debug_relax)
  2083. printf ("Relaxing the stub section. Size prior to this pass: %i\n",
  2084. (int) last_estimated_stub_section_size);
  2085. elf32_avr_size_stubs (htab->stub_sec->output_section->owner,
  2086. link_info, FALSE);
  2087. /* Check if the number of trampolines changed. */
  2088. if (last_estimated_stub_section_size != htab->stub_sec->size)
  2089. *again = TRUE;
  2090. if (debug_relax)
  2091. printf ("Size of stub section after this pass: %i\n",
  2092. (int) htab->stub_sec->size);
  2093. return TRUE;
  2094. }
  2095. /* We don't have to do anything for a relocatable link, if
  2096. this section does not have relocs, or if this is not a
  2097. code section. */
  2098. if (bfd_link_relocatable (link_info)
  2099. || (sec->flags & SEC_RELOC) == 0
  2100. || sec->reloc_count == 0
  2101. || (sec->flags & SEC_CODE) == 0)
  2102. return TRUE;
  2103. /* Check if the object file to relax uses internal symbols so that we
  2104. could fix up the relocations. */
  2105. if (!(elf_elfheader (abfd)->e_flags & EF_AVR_LINKRELAX_PREPARED))
  2106. return TRUE;
  2107. symtab_hdr = &elf_tdata (abfd)->symtab_hdr;
  2108. /* Get a copy of the native relocations. */
  2109. internal_relocs = (_bfd_elf_link_read_relocs
  2110. (abfd, sec, NULL, NULL, link_info->keep_memory));
  2111. if (internal_relocs == NULL)
  2112. goto error_return;
  2113. /* Walk through the relocs looking for relaxing opportunities. */
  2114. irelend = internal_relocs + sec->reloc_count;
  2115. for (irel = internal_relocs; irel < irelend; irel++)
  2116. {
  2117. bfd_vma symval;
  2118. if ( ELF32_R_TYPE (irel->r_info) != R_AVR_13_PCREL
  2119. && ELF32_R_TYPE (irel->r_info) != R_AVR_7_PCREL
  2120. && ELF32_R_TYPE (irel->r_info) != R_AVR_CALL)
  2121. continue;
  2122. /* Get the section contents if we haven't done so already. */
  2123. if (contents == NULL)
  2124. {
  2125. /* Get cached copy if it exists. */
  2126. if (elf_section_data (sec)->this_hdr.contents != NULL)
  2127. contents = elf_section_data (sec)->this_hdr.contents;
  2128. else
  2129. {
  2130. /* Go get them off disk. */
  2131. if (! bfd_malloc_and_get_section (abfd, sec, &contents))
  2132. goto error_return;
  2133. }
  2134. }
  2135. /* Read this BFD's local symbols if we haven't done so already. */
  2136. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  2137. {
  2138. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  2139. if (isymbuf == NULL)
  2140. isymbuf = bfd_elf_get_elf_syms (abfd, symtab_hdr,
  2141. symtab_hdr->sh_info, 0,
  2142. NULL, NULL, NULL);
  2143. if (isymbuf == NULL)
  2144. goto error_return;
  2145. }
  2146. /* Get the value of the symbol referred to by the reloc. */
  2147. if (ELF32_R_SYM (irel->r_info) < symtab_hdr->sh_info)
  2148. {
  2149. /* A local symbol. */
  2150. Elf_Internal_Sym *isym;
  2151. asection *sym_sec;
  2152. isym = isymbuf + ELF32_R_SYM (irel->r_info);
  2153. sym_sec = bfd_section_from_elf_index (abfd, isym->st_shndx);
  2154. symval = isym->st_value;
  2155. /* If the reloc is absolute, it will not have
  2156. a symbol or section associated with it. */
  2157. if (sym_sec)
  2158. symval += sym_sec->output_section->vma
  2159. + sym_sec->output_offset;
  2160. }
  2161. else
  2162. {
  2163. unsigned long indx;
  2164. struct elf_link_hash_entry *h;
  2165. /* An external symbol. */
  2166. indx = ELF32_R_SYM (irel->r_info) - symtab_hdr->sh_info;
  2167. h = elf_sym_hashes (abfd)[indx];
  2168. BFD_ASSERT (h != NULL);
  2169. if (h->root.type != bfd_link_hash_defined
  2170. && h->root.type != bfd_link_hash_defweak)
  2171. /* This appears to be a reference to an undefined
  2172. symbol. Just ignore it--it will be caught by the
  2173. regular reloc processing. */
  2174. continue;
  2175. symval = (h->root.u.def.value
  2176. + h->root.u.def.section->output_section->vma
  2177. + h->root.u.def.section->output_offset);
  2178. }
  2179. /* For simplicity of coding, we are going to modify the section
  2180. contents, the section relocs, and the BFD symbol table. We
  2181. must tell the rest of the code not to free up this
  2182. information. It would be possible to instead create a table
  2183. of changes which have to be made, as is done in coff-mips.c;
  2184. that would be more work, but would require less memory when
  2185. the linker is run. */
  2186. switch (ELF32_R_TYPE (irel->r_info))
  2187. {
  2188. /* Try to turn a 22-bit absolute call/jump into an 13-bit
  2189. pc-relative rcall/rjmp. */
  2190. case R_AVR_CALL:
  2191. {
  2192. bfd_vma value = symval + irel->r_addend;
  2193. bfd_vma dot, gap;
  2194. int distance_short_enough = 0;
  2195. /* Get the address of this instruction. */
  2196. dot = (sec->output_section->vma
  2197. + sec->output_offset + irel->r_offset);
  2198. /* Compute the distance from this insn to the branch target. */
  2199. gap = value - dot;
  2200. /* Check if the gap falls in the range that can be accommodated
  2201. in 13bits signed (It is 12bits when encoded, as we deal with
  2202. word addressing). */
  2203. if (!shrinkable && ((int) gap >= -4096 && (int) gap <= 4095))
  2204. distance_short_enough = 1;
  2205. /* If shrinkable, then we can check for a range of distance which
  2206. is two bytes farther on both the directions because the call
  2207. or jump target will be closer by two bytes after the
  2208. relaxation. */
  2209. else if (shrinkable && ((int) gap >= -4094 && (int) gap <= 4097))
  2210. distance_short_enough = 1;
  2211. /* Here we handle the wrap-around case. E.g. for a 16k device
  2212. we could use a rjmp to jump from address 0x100 to 0x3d00!
  2213. In order to make this work properly, we need to fill the
  2214. vaiable avr_pc_wrap_around with the appropriate value.
  2215. I.e. 0x4000 for a 16k device. */
  2216. {
  2217. /* Shrinking the code size makes the gaps larger in the
  2218. case of wrap-arounds. So we use a heuristical safety
  2219. margin to avoid that during relax the distance gets
  2220. again too large for the short jumps. Let's assume
  2221. a typical code-size reduction due to relax for a
  2222. 16k device of 600 bytes. So let's use twice the
  2223. typical value as safety margin. */
  2224. int rgap;
  2225. int safety_margin;
  2226. int assumed_shrink = 600;
  2227. if (avr_pc_wrap_around > 0x4000)
  2228. assumed_shrink = 900;
  2229. safety_margin = 2 * assumed_shrink;
  2230. rgap = avr_relative_distance_considering_wrap_around (gap);
  2231. if (rgap >= (-4092 + safety_margin)
  2232. && rgap <= (4094 - safety_margin))
  2233. distance_short_enough = 1;
  2234. }
  2235. if (distance_short_enough)
  2236. {
  2237. unsigned char code_msb;
  2238. unsigned char code_lsb;
  2239. if (debug_relax)
  2240. printf ("shrinking jump/call instruction at address 0x%x"
  2241. " in section %s\n\n",
  2242. (int) dot, sec->name);
  2243. /* Note that we've changed the relocs, section contents,
  2244. etc. */
  2245. elf_section_data (sec)->relocs = internal_relocs;
  2246. elf_section_data (sec)->this_hdr.contents = contents;
  2247. symtab_hdr->contents = (unsigned char *) isymbuf;
  2248. /* Get the instruction code for relaxing. */
  2249. code_lsb = bfd_get_8 (abfd, contents + irel->r_offset);
  2250. code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
  2251. /* Mask out the relocation bits. */
  2252. code_msb &= 0x94;
  2253. code_lsb &= 0x0E;
  2254. if (code_msb == 0x94 && code_lsb == 0x0E)
  2255. {
  2256. /* we are changing call -> rcall . */
  2257. bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
  2258. bfd_put_8 (abfd, 0xD0, contents + irel->r_offset + 1);
  2259. }
  2260. else if (code_msb == 0x94 && code_lsb == 0x0C)
  2261. {
  2262. /* we are changeing jump -> rjmp. */
  2263. bfd_put_8 (abfd, 0x00, contents + irel->r_offset);
  2264. bfd_put_8 (abfd, 0xC0, contents + irel->r_offset + 1);
  2265. }
  2266. else
  2267. abort ();
  2268. /* Fix the relocation's type. */
  2269. irel->r_info = ELF32_R_INFO (ELF32_R_SYM (irel->r_info),
  2270. R_AVR_13_PCREL);
  2271. /* We should not modify the ordering if 'shrinkable' is
  2272. FALSE. */
  2273. if (!shrinkable)
  2274. {
  2275. /* Let's insert a nop. */
  2276. bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 2);
  2277. bfd_put_8 (abfd, 0x00, contents + irel->r_offset + 3);
  2278. }
  2279. else
  2280. {
  2281. /* Delete two bytes of data. */
  2282. if (!elf32_avr_relax_delete_bytes (abfd, sec,
  2283. irel->r_offset + 2, 2))
  2284. goto error_return;
  2285. /* That will change things, so, we should relax again.
  2286. Note that this is not required, and it may be slow. */
  2287. *again = TRUE;
  2288. }
  2289. }
  2290. }
  2291. default:
  2292. {
  2293. unsigned char code_msb;
  2294. unsigned char code_lsb;
  2295. bfd_vma dot;
  2296. code_msb = bfd_get_8 (abfd, contents + irel->r_offset + 1);
  2297. code_lsb = bfd_get_8 (abfd, contents + irel->r_offset + 0);
  2298. /* Get the address of this instruction. */
  2299. dot = (sec->output_section->vma
  2300. + sec->output_offset + irel->r_offset);
  2301. /* Here we look for rcall/ret or call/ret sequences that could be
  2302. safely replaced by rjmp/ret or jmp/ret. */
  2303. if (((code_msb & 0xf0) == 0xd0)
  2304. && avr_replace_call_ret_sequences)
  2305. {
  2306. /* This insn is a rcall. */
  2307. unsigned char next_insn_msb = 0;
  2308. unsigned char next_insn_lsb = 0;
  2309. if (irel->r_offset + 3 < sec->size)
  2310. {
  2311. next_insn_msb =
  2312. bfd_get_8 (abfd, contents + irel->r_offset + 3);
  2313. next_insn_lsb =
  2314. bfd_get_8 (abfd, contents + irel->r_offset + 2);
  2315. }
  2316. if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
  2317. {
  2318. /* The next insn is a ret. We now convert the rcall insn
  2319. into a rjmp instruction. */
  2320. code_msb &= 0xef;
  2321. bfd_put_8 (abfd, code_msb, contents + irel->r_offset + 1);
  2322. if (debug_relax)
  2323. printf ("converted rcall/ret sequence at address 0x%x"
  2324. " into rjmp/ret sequence. Section is %s\n\n",
  2325. (int) dot, sec->name);
  2326. *again = TRUE;
  2327. break;
  2328. }
  2329. }
  2330. else if ((0x94 == (code_msb & 0xfe))
  2331. && (0x0e == (code_lsb & 0x0e))
  2332. && avr_replace_call_ret_sequences)
  2333. {
  2334. /* This insn is a call. */
  2335. unsigned char next_insn_msb = 0;
  2336. unsigned char next_insn_lsb = 0;
  2337. if (irel->r_offset + 5 < sec->size)
  2338. {
  2339. next_insn_msb =
  2340. bfd_get_8 (abfd, contents + irel->r_offset + 5);
  2341. next_insn_lsb =
  2342. bfd_get_8 (abfd, contents + irel->r_offset + 4);
  2343. }
  2344. if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
  2345. {
  2346. /* The next insn is a ret. We now convert the call insn
  2347. into a jmp instruction. */
  2348. code_lsb &= 0xfd;
  2349. bfd_put_8 (abfd, code_lsb, contents + irel->r_offset);
  2350. if (debug_relax)
  2351. printf ("converted call/ret sequence at address 0x%x"
  2352. " into jmp/ret sequence. Section is %s\n\n",
  2353. (int) dot, sec->name);
  2354. *again = TRUE;
  2355. break;
  2356. }
  2357. }
  2358. else if ((0xc0 == (code_msb & 0xf0))
  2359. || ((0x94 == (code_msb & 0xfe))
  2360. && (0x0c == (code_lsb & 0x0e))))
  2361. {
  2362. /* This insn is a rjmp or a jmp. */
  2363. unsigned char next_insn_msb = 0;
  2364. unsigned char next_insn_lsb = 0;
  2365. int insn_size;
  2366. if (0xc0 == (code_msb & 0xf0))
  2367. insn_size = 2; /* rjmp insn */
  2368. else
  2369. insn_size = 4; /* jmp insn */
  2370. if (irel->r_offset + insn_size + 1 < sec->size)
  2371. {
  2372. next_insn_msb =
  2373. bfd_get_8 (abfd, contents + irel->r_offset
  2374. + insn_size + 1);
  2375. next_insn_lsb =
  2376. bfd_get_8 (abfd, contents + irel->r_offset
  2377. + insn_size);
  2378. }
  2379. if ((0x95 == next_insn_msb) && (0x08 == next_insn_lsb))
  2380. {
  2381. /* The next insn is a ret. We possibly could delete
  2382. this ret. First we need to check for preceding
  2383. sbis/sbic/sbrs or cpse "skip" instructions. */
  2384. int there_is_preceding_non_skip_insn = 1;
  2385. bfd_vma address_of_ret;
  2386. address_of_ret = dot + insn_size;
  2387. if (debug_relax && (insn_size == 2))
  2388. printf ("found rjmp / ret sequence at address 0x%x\n",
  2389. (int) dot);
  2390. if (debug_relax && (insn_size == 4))
  2391. printf ("found jmp / ret sequence at address 0x%x\n",
  2392. (int) dot);
  2393. /* We have to make sure that there is a preceding insn. */
  2394. if (irel->r_offset >= 2)
  2395. {
  2396. unsigned char preceding_msb;
  2397. unsigned char preceding_lsb;
  2398. preceding_msb =
  2399. bfd_get_8 (abfd, contents + irel->r_offset - 1);
  2400. preceding_lsb =
  2401. bfd_get_8 (abfd, contents + irel->r_offset - 2);
  2402. /* sbic. */
  2403. if (0x99 == preceding_msb)
  2404. there_is_preceding_non_skip_insn = 0;
  2405. /* sbis. */
  2406. if (0x9b == preceding_msb)
  2407. there_is_preceding_non_skip_insn = 0;
  2408. /* sbrc */
  2409. if ((0xfc == (preceding_msb & 0xfe)
  2410. && (0x00 == (preceding_lsb & 0x08))))
  2411. there_is_preceding_non_skip_insn = 0;
  2412. /* sbrs */
  2413. if ((0xfe == (preceding_msb & 0xfe)
  2414. && (0x00 == (preceding_lsb & 0x08))))
  2415. there_is_preceding_non_skip_insn = 0;
  2416. /* cpse */
  2417. if (0x10 == (preceding_msb & 0xfc))
  2418. there_is_preceding_non_skip_insn = 0;
  2419. if (there_is_preceding_non_skip_insn == 0)
  2420. if (debug_relax)
  2421. printf ("preceding skip insn prevents deletion of"
  2422. " ret insn at Addy 0x%x in section %s\n",
  2423. (int) dot + 2, sec->name);
  2424. }
  2425. else
  2426. {
  2427. /* There is no previous instruction. */
  2428. there_is_preceding_non_skip_insn = 0;
  2429. }
  2430. if (there_is_preceding_non_skip_insn)
  2431. {
  2432. /* We now only have to make sure that there is no
  2433. local label defined at the address of the ret
  2434. instruction and that there is no local relocation
  2435. in this section pointing to the ret. */
  2436. int deleting_ret_is_safe = 1;
  2437. unsigned int section_offset_of_ret_insn =
  2438. irel->r_offset + insn_size;
  2439. Elf_Internal_Sym *isym, *isymend;
  2440. unsigned int sec_shndx;
  2441. struct bfd_section *isec;
  2442. sec_shndx =
  2443. _bfd_elf_section_from_bfd_section (abfd, sec);
  2444. /* Check for local symbols. */
  2445. isym = (Elf_Internal_Sym *) symtab_hdr->contents;
  2446. isymend = isym + symtab_hdr->sh_info;
  2447. /* PR 6019: There may not be any local symbols. */
  2448. for (; isym != NULL && isym < isymend; isym++)
  2449. {
  2450. if (isym->st_value == section_offset_of_ret_insn
  2451. && isym->st_shndx == sec_shndx)
  2452. {
  2453. deleting_ret_is_safe = 0;
  2454. if (debug_relax)
  2455. printf ("local label prevents deletion of ret "
  2456. "insn at address 0x%x\n",
  2457. (int) dot + insn_size);
  2458. }
  2459. }
  2460. /* Now check for global symbols. */
  2461. {
  2462. int symcount;
  2463. struct elf_link_hash_entry **sym_hashes;
  2464. struct elf_link_hash_entry **end_hashes;
  2465. symcount = (symtab_hdr->sh_size
  2466. / sizeof (Elf32_External_Sym)
  2467. - symtab_hdr->sh_info);
  2468. sym_hashes = elf_sym_hashes (abfd);
  2469. end_hashes = sym_hashes + symcount;
  2470. for (; sym_hashes < end_hashes; sym_hashes++)
  2471. {
  2472. struct elf_link_hash_entry *sym_hash =
  2473. *sym_hashes;
  2474. if ((sym_hash->root.type == bfd_link_hash_defined
  2475. || sym_hash->root.type ==
  2476. bfd_link_hash_defweak)
  2477. && sym_hash->root.u.def.section == sec
  2478. && sym_hash->root.u.def.value == section_offset_of_ret_insn)
  2479. {
  2480. deleting_ret_is_safe = 0;
  2481. if (debug_relax)
  2482. printf ("global label prevents deletion of "
  2483. "ret insn at address 0x%x\n",
  2484. (int) dot + insn_size);
  2485. }
  2486. }
  2487. }
  2488. /* Now we check for relocations pointing to ret. */
  2489. for (isec = abfd->sections; isec && deleting_ret_is_safe; isec = isec->next)
  2490. {
  2491. Elf_Internal_Rela *rel;
  2492. Elf_Internal_Rela *relend;
  2493. rel = elf_section_data (isec)->relocs;
  2494. if (rel == NULL)
  2495. rel = _bfd_elf_link_read_relocs (abfd, isec, NULL, NULL, TRUE);
  2496. relend = rel + isec->reloc_count;
  2497. for (; rel && rel < relend; rel++)
  2498. {
  2499. bfd_vma reloc_target = 0;
  2500. /* Read this BFD's local symbols if we haven't
  2501. done so already. */
  2502. if (isymbuf == NULL && symtab_hdr->sh_info != 0)
  2503. {
  2504. isymbuf = (Elf_Internal_Sym *)
  2505. symtab_hdr->contents;
  2506. if (isymbuf == NULL)
  2507. isymbuf = bfd_elf_get_elf_syms
  2508. (abfd,
  2509. symtab_hdr,
  2510. symtab_hdr->sh_info, 0,
  2511. NULL, NULL, NULL);
  2512. if (isymbuf == NULL)
  2513. break;
  2514. }
  2515. /* Get the value of the symbol referred to
  2516. by the reloc. */
  2517. if (ELF32_R_SYM (rel->r_info)
  2518. < symtab_hdr->sh_info)
  2519. {
  2520. /* A local symbol. */
  2521. asection *sym_sec;
  2522. isym = isymbuf
  2523. + ELF32_R_SYM (rel->r_info);
  2524. sym_sec = bfd_section_from_elf_index
  2525. (abfd, isym->st_shndx);
  2526. symval = isym->st_value;
  2527. /* If the reloc is absolute, it will not
  2528. have a symbol or section associated
  2529. with it. */
  2530. if (sym_sec)
  2531. {
  2532. symval +=
  2533. sym_sec->output_section->vma
  2534. + sym_sec->output_offset;
  2535. reloc_target = symval + rel->r_addend;
  2536. }
  2537. else
  2538. {
  2539. reloc_target = symval + rel->r_addend;
  2540. /* Reference symbol is absolute. */
  2541. }
  2542. }
  2543. /* else ... reference symbol is extern. */
  2544. if (address_of_ret == reloc_target)
  2545. {
  2546. deleting_ret_is_safe = 0;
  2547. if (debug_relax)
  2548. printf ("ret from "
  2549. "rjmp/jmp ret sequence at address"
  2550. " 0x%x could not be deleted. ret"
  2551. " is target of a relocation.\n",
  2552. (int) address_of_ret);
  2553. break;
  2554. }
  2555. }
  2556. }
  2557. if (deleting_ret_is_safe)
  2558. {
  2559. if (debug_relax)
  2560. printf ("unreachable ret instruction "
  2561. "at address 0x%x deleted.\n",
  2562. (int) dot + insn_size);
  2563. /* Delete two bytes of data. */
  2564. if (!elf32_avr_relax_delete_bytes (abfd, sec,
  2565. irel->r_offset + insn_size, 2))
  2566. goto error_return;
  2567. /* That will change things, so, we should relax
  2568. again. Note that this is not required, and it
  2569. may be slow. */
  2570. *again = TRUE;
  2571. break;
  2572. }
  2573. }
  2574. }
  2575. }
  2576. break;
  2577. }
  2578. }
  2579. }
  2580. if (!*again)
  2581. {
  2582. /* Look through all the property records in this section to see if
  2583. there's any alignment records that can be moved. */
  2584. struct avr_relax_info *relax_info;
  2585. relax_info = get_avr_relax_info (sec);
  2586. if (relax_info->records.count > 0)
  2587. {
  2588. unsigned int i;
  2589. for (i = 0; i < relax_info->records.count; ++i)
  2590. {
  2591. switch (relax_info->records.items [i].type)
  2592. {
  2593. case RECORD_ORG:
  2594. case RECORD_ORG_AND_FILL:
  2595. break;
  2596. case RECORD_ALIGN:
  2597. case RECORD_ALIGN_AND_FILL:
  2598. {
  2599. struct avr_property_record *record;
  2600. unsigned long bytes_to_align;
  2601. int count = 0;
  2602. /* Look for alignment directives that have had enough
  2603. bytes deleted before them, such that the directive
  2604. can be moved backwards and still maintain the
  2605. required alignment. */
  2606. record = &relax_info->records.items [i];
  2607. bytes_to_align
  2608. = (unsigned long) (1 << record->data.align.bytes);
  2609. while (record->data.align.preceding_deleted >=
  2610. bytes_to_align)
  2611. {
  2612. record->data.align.preceding_deleted
  2613. -= bytes_to_align;
  2614. count += bytes_to_align;
  2615. }
  2616. if (count > 0)
  2617. {
  2618. bfd_vma addr = record->offset;
  2619. /* We can delete COUNT bytes and this alignment
  2620. directive will still be correctly aligned.
  2621. First move the alignment directive, then delete
  2622. the bytes. */
  2623. record->offset -= count;
  2624. elf32_avr_relax_delete_bytes (abfd, sec,
  2625. addr - count,
  2626. count);
  2627. *again = TRUE;
  2628. }
  2629. }
  2630. break;
  2631. }
  2632. }
  2633. }
  2634. }
  2635. if (contents != NULL
  2636. && elf_section_data (sec)->this_hdr.contents != contents)
  2637. {
  2638. if (! link_info->keep_memory)
  2639. free (contents);
  2640. else
  2641. {
  2642. /* Cache the section contents for elf_link_input_bfd. */
  2643. elf_section_data (sec)->this_hdr.contents = contents;
  2644. }
  2645. }
  2646. if (internal_relocs != NULL
  2647. && elf_section_data (sec)->relocs != internal_relocs)
  2648. free (internal_relocs);
  2649. return TRUE;
  2650. error_return:
  2651. if (isymbuf != NULL
  2652. && symtab_hdr->contents != (unsigned char *) isymbuf)
  2653. free (isymbuf);
  2654. if (contents != NULL
  2655. && elf_section_data (sec)->this_hdr.contents != contents)
  2656. free (contents);
  2657. if (internal_relocs != NULL
  2658. && elf_section_data (sec)->relocs != internal_relocs)
  2659. free (internal_relocs);
  2660. return FALSE;
  2661. }
  2662. /* This is a version of bfd_generic_get_relocated_section_contents
  2663. which uses elf32_avr_relocate_section.
  2664. For avr it's essentially a cut and paste taken from the H8300 port.
  2665. The author of the relaxation support patch for avr had absolutely no
  2666. clue what is happening here but found out that this part of the code
  2667. seems to be important. */
  2668. static bfd_byte *
  2669. elf32_avr_get_relocated_section_contents (bfd *output_bfd,
  2670. struct bfd_link_info *link_info,
  2671. struct bfd_link_order *link_order,
  2672. bfd_byte *data,
  2673. bfd_boolean relocatable,
  2674. asymbol **symbols)
  2675. {
  2676. Elf_Internal_Shdr *symtab_hdr;
  2677. asection *input_section = link_order->u.indirect.section;
  2678. bfd *input_bfd = input_section->owner;
  2679. asection **sections = NULL;
  2680. Elf_Internal_Rela *internal_relocs = NULL;
  2681. Elf_Internal_Sym *isymbuf = NULL;
  2682. /* We only need to handle the case of relaxing, or of having a
  2683. particular set of section contents, specially. */
  2684. if (relocatable
  2685. || elf_section_data (input_section)->this_hdr.contents == NULL)
  2686. return bfd_generic_get_relocated_section_contents (output_bfd, link_info,
  2687. link_order, data,
  2688. relocatable,
  2689. symbols);
  2690. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  2691. memcpy (data, elf_section_data (input_section)->this_hdr.contents,
  2692. (size_t) input_section->size);
  2693. if ((input_section->flags & SEC_RELOC) != 0
  2694. && input_section->reloc_count > 0)
  2695. {
  2696. asection **secpp;
  2697. Elf_Internal_Sym *isym, *isymend;
  2698. bfd_size_type amt;
  2699. internal_relocs = (_bfd_elf_link_read_relocs
  2700. (input_bfd, input_section, NULL, NULL, FALSE));
  2701. if (internal_relocs == NULL)
  2702. goto error_return;
  2703. if (symtab_hdr->sh_info != 0)
  2704. {
  2705. isymbuf = (Elf_Internal_Sym *) symtab_hdr->contents;
  2706. if (isymbuf == NULL)
  2707. isymbuf = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
  2708. symtab_hdr->sh_info, 0,
  2709. NULL, NULL, NULL);
  2710. if (isymbuf == NULL)
  2711. goto error_return;
  2712. }
  2713. amt = symtab_hdr->sh_info;
  2714. amt *= sizeof (asection *);
  2715. sections = bfd_malloc (amt);
  2716. if (sections == NULL && amt != 0)
  2717. goto error_return;
  2718. isymend = isymbuf + symtab_hdr->sh_info;
  2719. for (isym = isymbuf, secpp = sections; isym < isymend; ++isym, ++secpp)
  2720. {
  2721. asection *isec;
  2722. if (isym->st_shndx == SHN_UNDEF)
  2723. isec = bfd_und_section_ptr;
  2724. else if (isym->st_shndx == SHN_ABS)
  2725. isec = bfd_abs_section_ptr;
  2726. else if (isym->st_shndx == SHN_COMMON)
  2727. isec = bfd_com_section_ptr;
  2728. else
  2729. isec = bfd_section_from_elf_index (input_bfd, isym->st_shndx);
  2730. *secpp = isec;
  2731. }
  2732. if (! elf32_avr_relocate_section (output_bfd, link_info, input_bfd,
  2733. input_section, data, internal_relocs,
  2734. isymbuf, sections))
  2735. goto error_return;
  2736. if (sections != NULL)
  2737. free (sections);
  2738. if (isymbuf != NULL
  2739. && symtab_hdr->contents != (unsigned char *) isymbuf)
  2740. free (isymbuf);
  2741. if (elf_section_data (input_section)->relocs != internal_relocs)
  2742. free (internal_relocs);
  2743. }
  2744. return data;
  2745. error_return:
  2746. if (sections != NULL)
  2747. free (sections);
  2748. if (isymbuf != NULL
  2749. && symtab_hdr->contents != (unsigned char *) isymbuf)
  2750. free (isymbuf);
  2751. if (internal_relocs != NULL
  2752. && elf_section_data (input_section)->relocs != internal_relocs)
  2753. free (internal_relocs);
  2754. return NULL;
  2755. }
  2756. /* Determines the hash entry name for a particular reloc. It consists of
  2757. the identifier of the symbol section and the added reloc addend and
  2758. symbol offset relative to the section the symbol is attached to. */
  2759. static char *
  2760. avr_stub_name (const asection *symbol_section,
  2761. const bfd_vma symbol_offset,
  2762. const Elf_Internal_Rela *rela)
  2763. {
  2764. char *stub_name;
  2765. bfd_size_type len;
  2766. len = 8 + 1 + 8 + 1 + 1;
  2767. stub_name = bfd_malloc (len);
  2768. sprintf (stub_name, "%08x+%08x",
  2769. symbol_section->id & 0xffffffff,
  2770. (unsigned int) ((rela->r_addend & 0xffffffff) + symbol_offset));
  2771. return stub_name;
  2772. }
  2773. /* Add a new stub entry to the stub hash. Not all fields of the new
  2774. stub entry are initialised. */
  2775. static struct elf32_avr_stub_hash_entry *
  2776. avr_add_stub (const char *stub_name,
  2777. struct elf32_avr_link_hash_table *htab)
  2778. {
  2779. struct elf32_avr_stub_hash_entry *hsh;
  2780. /* Enter this entry into the linker stub hash table. */
  2781. hsh = avr_stub_hash_lookup (&htab->bstab, stub_name, TRUE, FALSE);
  2782. if (hsh == NULL)
  2783. {
  2784. (*_bfd_error_handler) (_("%B: cannot create stub entry %s"),
  2785. NULL, stub_name);
  2786. return NULL;
  2787. }
  2788. hsh->stub_offset = 0;
  2789. return hsh;
  2790. }
  2791. /* We assume that there is already space allocated for the stub section
  2792. contents and that before building the stubs the section size is
  2793. initialized to 0. We assume that within the stub hash table entry,
  2794. the absolute position of the jmp target has been written in the
  2795. target_value field. We write here the offset of the generated jmp insn
  2796. relative to the trampoline section start to the stub_offset entry in
  2797. the stub hash table entry. */
  2798. static bfd_boolean
  2799. avr_build_one_stub (struct bfd_hash_entry *bh, void *in_arg)
  2800. {
  2801. struct elf32_avr_stub_hash_entry *hsh;
  2802. struct bfd_link_info *info;
  2803. struct elf32_avr_link_hash_table *htab;
  2804. bfd *stub_bfd;
  2805. bfd_byte *loc;
  2806. bfd_vma target;
  2807. bfd_vma starget;
  2808. /* Basic opcode */
  2809. bfd_vma jmp_insn = 0x0000940c;
  2810. /* Massage our args to the form they really have. */
  2811. hsh = avr_stub_hash_entry (bh);
  2812. if (!hsh->is_actually_needed)
  2813. return TRUE;
  2814. info = (struct bfd_link_info *) in_arg;
  2815. htab = avr_link_hash_table (info);
  2816. if (htab == NULL)
  2817. return FALSE;
  2818. target = hsh->target_value;
  2819. /* Make a note of the offset within the stubs for this entry. */
  2820. hsh->stub_offset = htab->stub_sec->size;
  2821. loc = htab->stub_sec->contents + hsh->stub_offset;
  2822. stub_bfd = htab->stub_sec->owner;
  2823. if (debug_stubs)
  2824. printf ("Building one Stub. Address: 0x%x, Offset: 0x%x\n",
  2825. (unsigned int) target,
  2826. (unsigned int) hsh->stub_offset);
  2827. /* We now have to add the information on the jump target to the bare
  2828. opcode bits already set in jmp_insn. */
  2829. /* Check for the alignment of the address. */
  2830. if (target & 1)
  2831. return FALSE;
  2832. starget = target >> 1;
  2833. jmp_insn |= ((starget & 0x10000) | ((starget << 3) & 0x1f00000)) >> 16;
  2834. bfd_put_16 (stub_bfd, jmp_insn, loc);
  2835. bfd_put_16 (stub_bfd, (bfd_vma) starget & 0xffff, loc + 2);
  2836. htab->stub_sec->size += 4;
  2837. /* Now add the entries in the address mapping table if there is still
  2838. space left. */
  2839. {
  2840. unsigned int nr;
  2841. nr = htab->amt_entry_cnt + 1;
  2842. if (nr <= htab->amt_max_entry_cnt)
  2843. {
  2844. htab->amt_entry_cnt = nr;
  2845. htab->amt_stub_offsets[nr - 1] = hsh->stub_offset;
  2846. htab->amt_destination_addr[nr - 1] = target;
  2847. }
  2848. }
  2849. return TRUE;
  2850. }
  2851. static bfd_boolean
  2852. avr_mark_stub_not_to_be_necessary (struct bfd_hash_entry *bh,
  2853. void *in_arg ATTRIBUTE_UNUSED)
  2854. {
  2855. struct elf32_avr_stub_hash_entry *hsh;
  2856. hsh = avr_stub_hash_entry (bh);
  2857. hsh->is_actually_needed = FALSE;
  2858. return TRUE;
  2859. }
  2860. static bfd_boolean
  2861. avr_size_one_stub (struct bfd_hash_entry *bh, void *in_arg)
  2862. {
  2863. struct elf32_avr_stub_hash_entry *hsh;
  2864. struct elf32_avr_link_hash_table *htab;
  2865. int size;
  2866. /* Massage our args to the form they really have. */
  2867. hsh = avr_stub_hash_entry (bh);
  2868. htab = in_arg;
  2869. if (hsh->is_actually_needed)
  2870. size = 4;
  2871. else
  2872. size = 0;
  2873. htab->stub_sec->size += size;
  2874. return TRUE;
  2875. }
  2876. void
  2877. elf32_avr_setup_params (struct bfd_link_info *info,
  2878. bfd *avr_stub_bfd,
  2879. asection *avr_stub_section,
  2880. bfd_boolean no_stubs,
  2881. bfd_boolean deb_stubs,
  2882. bfd_boolean deb_relax,
  2883. bfd_vma pc_wrap_around,
  2884. bfd_boolean call_ret_replacement)
  2885. {
  2886. struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
  2887. if (htab == NULL)
  2888. return;
  2889. htab->stub_sec = avr_stub_section;
  2890. htab->stub_bfd = avr_stub_bfd;
  2891. htab->no_stubs = no_stubs;
  2892. debug_relax = deb_relax;
  2893. debug_stubs = deb_stubs;
  2894. avr_pc_wrap_around = pc_wrap_around;
  2895. avr_replace_call_ret_sequences = call_ret_replacement;
  2896. }
  2897. /* Set up various things so that we can make a list of input sections
  2898. for each output section included in the link. Returns -1 on error,
  2899. 0 when no stubs will be needed, and 1 on success. It also sets
  2900. information on the stubs bfd and the stub section in the info
  2901. struct. */
  2902. int
  2903. elf32_avr_setup_section_lists (bfd *output_bfd,
  2904. struct bfd_link_info *info)
  2905. {
  2906. bfd *input_bfd;
  2907. unsigned int bfd_count;
  2908. unsigned int top_id, top_index;
  2909. asection *section;
  2910. asection **input_list, **list;
  2911. bfd_size_type amt;
  2912. struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
  2913. if (htab == NULL || htab->no_stubs)
  2914. return 0;
  2915. /* Count the number of input BFDs and find the top input section id. */
  2916. for (input_bfd = info->input_bfds, bfd_count = 0, top_id = 0;
  2917. input_bfd != NULL;
  2918. input_bfd = input_bfd->link.next)
  2919. {
  2920. bfd_count += 1;
  2921. for (section = input_bfd->sections;
  2922. section != NULL;
  2923. section = section->next)
  2924. if (top_id < section->id)
  2925. top_id = section->id;
  2926. }
  2927. htab->bfd_count = bfd_count;
  2928. /* We can't use output_bfd->section_count here to find the top output
  2929. section index as some sections may have been removed, and
  2930. strip_excluded_output_sections doesn't renumber the indices. */
  2931. for (section = output_bfd->sections, top_index = 0;
  2932. section != NULL;
  2933. section = section->next)
  2934. if (top_index < section->index)
  2935. top_index = section->index;
  2936. htab->top_index = top_index;
  2937. amt = sizeof (asection *) * (top_index + 1);
  2938. input_list = bfd_malloc (amt);
  2939. htab->input_list = input_list;
  2940. if (input_list == NULL)
  2941. return -1;
  2942. /* For sections we aren't interested in, mark their entries with a
  2943. value we can check later. */
  2944. list = input_list + top_index;
  2945. do
  2946. *list = bfd_abs_section_ptr;
  2947. while (list-- != input_list);
  2948. for (section = output_bfd->sections;
  2949. section != NULL;
  2950. section = section->next)
  2951. if ((section->flags & SEC_CODE) != 0)
  2952. input_list[section->index] = NULL;
  2953. return 1;
  2954. }
  2955. /* Read in all local syms for all input bfds, and create hash entries
  2956. for export stubs if we are building a multi-subspace shared lib.
  2957. Returns -1 on error, 0 otherwise. */
  2958. static int
  2959. get_local_syms (bfd *input_bfd, struct bfd_link_info *info)
  2960. {
  2961. unsigned int bfd_indx;
  2962. Elf_Internal_Sym *local_syms, **all_local_syms;
  2963. struct elf32_avr_link_hash_table *htab = avr_link_hash_table (info);
  2964. bfd_size_type amt;
  2965. if (htab == NULL)
  2966. return -1;
  2967. /* We want to read in symbol extension records only once. To do this
  2968. we need to read in the local symbols in parallel and save them for
  2969. later use; so hold pointers to the local symbols in an array. */
  2970. amt = sizeof (Elf_Internal_Sym *) * htab->bfd_count;
  2971. all_local_syms = bfd_zmalloc (amt);
  2972. htab->all_local_syms = all_local_syms;
  2973. if (all_local_syms == NULL)
  2974. return -1;
  2975. /* Walk over all the input BFDs, swapping in local symbols.
  2976. If we are creating a shared library, create hash entries for the
  2977. export stubs. */
  2978. for (bfd_indx = 0;
  2979. input_bfd != NULL;
  2980. input_bfd = input_bfd->link.next, bfd_indx++)
  2981. {
  2982. Elf_Internal_Shdr *symtab_hdr;
  2983. /* We'll need the symbol table in a second. */
  2984. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  2985. if (symtab_hdr->sh_info == 0)
  2986. continue;
  2987. /* We need an array of the local symbols attached to the input bfd. */
  2988. local_syms = (Elf_Internal_Sym *) symtab_hdr->contents;
  2989. if (local_syms == NULL)
  2990. {
  2991. local_syms = bfd_elf_get_elf_syms (input_bfd, symtab_hdr,
  2992. symtab_hdr->sh_info, 0,
  2993. NULL, NULL, NULL);
  2994. /* Cache them for elf_link_input_bfd. */
  2995. symtab_hdr->contents = (unsigned char *) local_syms;
  2996. }
  2997. if (local_syms == NULL)
  2998. return -1;
  2999. all_local_syms[bfd_indx] = local_syms;
  3000. }
  3001. return 0;
  3002. }
  3003. #define ADD_DUMMY_STUBS_FOR_DEBUGGING 0
  3004. bfd_boolean
  3005. elf32_avr_size_stubs (bfd *output_bfd,
  3006. struct bfd_link_info *info,
  3007. bfd_boolean is_prealloc_run)
  3008. {
  3009. struct elf32_avr_link_hash_table *htab;
  3010. int stub_changed = 0;
  3011. htab = avr_link_hash_table (info);
  3012. if (htab == NULL)
  3013. return FALSE;
  3014. /* At this point we initialize htab->vector_base
  3015. To the start of the text output section. */
  3016. htab->vector_base = htab->stub_sec->output_section->vma;
  3017. if (get_local_syms (info->input_bfds, info))
  3018. {
  3019. if (htab->all_local_syms)
  3020. goto error_ret_free_local;
  3021. return FALSE;
  3022. }
  3023. if (ADD_DUMMY_STUBS_FOR_DEBUGGING)
  3024. {
  3025. struct elf32_avr_stub_hash_entry *test;
  3026. test = avr_add_stub ("Hugo",htab);
  3027. test->target_value = 0x123456;
  3028. test->stub_offset = 13;
  3029. test = avr_add_stub ("Hugo2",htab);
  3030. test->target_value = 0x84210;
  3031. test->stub_offset = 14;
  3032. }
  3033. while (1)
  3034. {
  3035. bfd *input_bfd;
  3036. unsigned int bfd_indx;
  3037. /* We will have to re-generate the stub hash table each time anything
  3038. in memory has changed. */
  3039. bfd_hash_traverse (&htab->bstab, avr_mark_stub_not_to_be_necessary, htab);
  3040. for (input_bfd = info->input_bfds, bfd_indx = 0;
  3041. input_bfd != NULL;
  3042. input_bfd = input_bfd->link.next, bfd_indx++)
  3043. {
  3044. Elf_Internal_Shdr *symtab_hdr;
  3045. asection *section;
  3046. Elf_Internal_Sym *local_syms;
  3047. /* We'll need the symbol table in a second. */
  3048. symtab_hdr = &elf_tdata (input_bfd)->symtab_hdr;
  3049. if (symtab_hdr->sh_info == 0)
  3050. continue;
  3051. local_syms = htab->all_local_syms[bfd_indx];
  3052. /* Walk over each section attached to the input bfd. */
  3053. for (section = input_bfd->sections;
  3054. section != NULL;
  3055. section = section->next)
  3056. {
  3057. Elf_Internal_Rela *internal_relocs, *irelaend, *irela;
  3058. /* If there aren't any relocs, then there's nothing more
  3059. to do. */
  3060. if ((section->flags & SEC_RELOC) == 0
  3061. || section->reloc_count == 0)
  3062. continue;
  3063. /* If this section is a link-once section that will be
  3064. discarded, then don't create any stubs. */
  3065. if (section->output_section == NULL
  3066. || section->output_section->owner != output_bfd)
  3067. continue;
  3068. /* Get the relocs. */
  3069. internal_relocs
  3070. = _bfd_elf_link_read_relocs (input_bfd, section, NULL, NULL,
  3071. info->keep_memory);
  3072. if (internal_relocs == NULL)
  3073. goto error_ret_free_local;
  3074. /* Now examine each relocation. */
  3075. irela = internal_relocs;
  3076. irelaend = irela + section->reloc_count;
  3077. for (; irela < irelaend; irela++)
  3078. {
  3079. unsigned int r_type, r_indx;
  3080. struct elf32_avr_stub_hash_entry *hsh;
  3081. asection *sym_sec;
  3082. bfd_vma sym_value;
  3083. bfd_vma destination;
  3084. struct elf_link_hash_entry *hh;
  3085. char *stub_name;
  3086. r_type = ELF32_R_TYPE (irela->r_info);
  3087. r_indx = ELF32_R_SYM (irela->r_info);
  3088. /* Only look for 16 bit GS relocs. No other reloc will need a
  3089. stub. */
  3090. if (!((r_type == R_AVR_16_PM)
  3091. || (r_type == R_AVR_LO8_LDI_GS)
  3092. || (r_type == R_AVR_HI8_LDI_GS)))
  3093. continue;
  3094. /* Now determine the call target, its name, value,
  3095. section. */
  3096. sym_sec = NULL;
  3097. sym_value = 0;
  3098. destination = 0;
  3099. hh = NULL;
  3100. if (r_indx < symtab_hdr->sh_info)
  3101. {
  3102. /* It's a local symbol. */
  3103. Elf_Internal_Sym *sym;
  3104. Elf_Internal_Shdr *hdr;
  3105. unsigned int shndx;
  3106. sym = local_syms + r_indx;
  3107. if (ELF_ST_TYPE (sym->st_info) != STT_SECTION)
  3108. sym_value = sym->st_value;
  3109. shndx = sym->st_shndx;
  3110. if (shndx < elf_numsections (input_bfd))
  3111. {
  3112. hdr = elf_elfsections (input_bfd)[shndx];
  3113. sym_sec = hdr->bfd_section;
  3114. destination = (sym_value + irela->r_addend
  3115. + sym_sec->output_offset
  3116. + sym_sec->output_section->vma);
  3117. }
  3118. }
  3119. else
  3120. {
  3121. /* It's an external symbol. */
  3122. int e_indx;
  3123. e_indx = r_indx - symtab_hdr->sh_info;
  3124. hh = elf_sym_hashes (input_bfd)[e_indx];
  3125. while (hh->root.type == bfd_link_hash_indirect
  3126. || hh->root.type == bfd_link_hash_warning)
  3127. hh = (struct elf_link_hash_entry *)
  3128. (hh->root.u.i.link);
  3129. if (hh->root.type == bfd_link_hash_defined
  3130. || hh->root.type == bfd_link_hash_defweak)
  3131. {
  3132. sym_sec = hh->root.u.def.section;
  3133. sym_value = hh->root.u.def.value;
  3134. if (sym_sec->output_section != NULL)
  3135. destination = (sym_value + irela->r_addend
  3136. + sym_sec->output_offset
  3137. + sym_sec->output_section->vma);
  3138. }
  3139. else if (hh->root.type == bfd_link_hash_undefweak)
  3140. {
  3141. if (! bfd_link_pic (info))
  3142. continue;
  3143. }
  3144. else if (hh->root.type == bfd_link_hash_undefined)
  3145. {
  3146. if (! (info->unresolved_syms_in_objects == RM_IGNORE
  3147. && (ELF_ST_VISIBILITY (hh->other)
  3148. == STV_DEFAULT)))
  3149. continue;
  3150. }
  3151. else
  3152. {
  3153. bfd_set_error (bfd_error_bad_value);
  3154. error_ret_free_internal:
  3155. if (elf_section_data (section)->relocs == NULL)
  3156. free (internal_relocs);
  3157. goto error_ret_free_local;
  3158. }
  3159. }
  3160. if (! avr_stub_is_required_for_16_bit_reloc
  3161. (destination - htab->vector_base))
  3162. {
  3163. if (!is_prealloc_run)
  3164. /* We are having a reloc that does't need a stub. */
  3165. continue;
  3166. /* We don't right now know if a stub will be needed.
  3167. Let's rather be on the safe side. */
  3168. }
  3169. /* Get the name of this stub. */
  3170. stub_name = avr_stub_name (sym_sec, sym_value, irela);
  3171. if (!stub_name)
  3172. goto error_ret_free_internal;
  3173. hsh = avr_stub_hash_lookup (&htab->bstab,
  3174. stub_name,
  3175. FALSE, FALSE);
  3176. if (hsh != NULL)
  3177. {
  3178. /* The proper stub has already been created. Mark it
  3179. to be used and write the possibly changed destination
  3180. value. */
  3181. hsh->is_actually_needed = TRUE;
  3182. hsh->target_value = destination;
  3183. free (stub_name);
  3184. continue;
  3185. }
  3186. hsh = avr_add_stub (stub_name, htab);
  3187. if (hsh == NULL)
  3188. {
  3189. free (stub_name);
  3190. goto error_ret_free_internal;
  3191. }
  3192. hsh->is_actually_needed = TRUE;
  3193. hsh->target_value = destination;
  3194. if (debug_stubs)
  3195. printf ("Adding stub with destination 0x%x to the"
  3196. " hash table.\n", (unsigned int) destination);
  3197. if (debug_stubs)
  3198. printf ("(Pre-Alloc run: %i)\n", is_prealloc_run);
  3199. stub_changed = TRUE;
  3200. }
  3201. /* We're done with the internal relocs, free them. */
  3202. if (elf_section_data (section)->relocs == NULL)
  3203. free (internal_relocs);
  3204. }
  3205. }
  3206. /* Re-Calculate the number of needed stubs. */
  3207. htab->stub_sec->size = 0;
  3208. bfd_hash_traverse (&htab->bstab, avr_size_one_stub, htab);
  3209. if (!stub_changed)
  3210. break;
  3211. stub_changed = FALSE;
  3212. }
  3213. free (htab->all_local_syms);
  3214. return TRUE;
  3215. error_ret_free_local:
  3216. free (htab->all_local_syms);
  3217. return FALSE;
  3218. }
  3219. /* Build all the stubs associated with the current output file. The
  3220. stubs are kept in a hash table attached to the main linker hash
  3221. table. We also set up the .plt entries for statically linked PIC
  3222. functions here. This function is called via hppaelf_finish in the
  3223. linker. */
  3224. bfd_boolean
  3225. elf32_avr_build_stubs (struct bfd_link_info *info)
  3226. {
  3227. asection *stub_sec;
  3228. struct bfd_hash_table *table;
  3229. struct elf32_avr_link_hash_table *htab;
  3230. bfd_size_type total_size = 0;
  3231. htab = avr_link_hash_table (info);
  3232. if (htab == NULL)
  3233. return FALSE;
  3234. /* In case that there were several stub sections: */
  3235. for (stub_sec = htab->stub_bfd->sections;
  3236. stub_sec != NULL;
  3237. stub_sec = stub_sec->next)
  3238. {
  3239. bfd_size_type size;
  3240. /* Allocate memory to hold the linker stubs. */
  3241. size = stub_sec->size;
  3242. total_size += size;
  3243. stub_sec->contents = bfd_zalloc (htab->stub_bfd, size);
  3244. if (stub_sec->contents == NULL && size != 0)
  3245. return FALSE;
  3246. stub_sec->size = 0;
  3247. }
  3248. /* Allocate memory for the adress mapping table. */
  3249. htab->amt_entry_cnt = 0;
  3250. htab->amt_max_entry_cnt = total_size / 4;
  3251. htab->amt_stub_offsets = bfd_malloc (sizeof (bfd_vma)
  3252. * htab->amt_max_entry_cnt);
  3253. htab->amt_destination_addr = bfd_malloc (sizeof (bfd_vma)
  3254. * htab->amt_max_entry_cnt );
  3255. if (debug_stubs)
  3256. printf ("Allocating %i entries in the AMT\n", htab->amt_max_entry_cnt);
  3257. /* Build the stubs as directed by the stub hash table. */
  3258. table = &htab->bstab;
  3259. bfd_hash_traverse (table, avr_build_one_stub, info);
  3260. if (debug_stubs)
  3261. printf ("Final Stub section Size: %i\n", (int) htab->stub_sec->size);
  3262. return TRUE;
  3263. }
  3264. /* Callback used by QSORT to order relocations AP and BP. */
  3265. static int
  3266. internal_reloc_compare (const void *ap, const void *bp)
  3267. {
  3268. const Elf_Internal_Rela *a = (const Elf_Internal_Rela *) ap;
  3269. const Elf_Internal_Rela *b = (const Elf_Internal_Rela *) bp;
  3270. if (a->r_offset != b->r_offset)
  3271. return (a->r_offset - b->r_offset);
  3272. /* We don't need to sort on these criteria for correctness,
  3273. but enforcing a more strict ordering prevents unstable qsort
  3274. from behaving differently with different implementations.
  3275. Without the code below we get correct but different results
  3276. on Solaris 2.7 and 2.8. We would like to always produce the
  3277. same results no matter the host. */
  3278. if (a->r_info != b->r_info)
  3279. return (a->r_info - b->r_info);
  3280. return (a->r_addend - b->r_addend);
  3281. }
  3282. /* Return true if ADDRESS is within the vma range of SECTION from ABFD. */
  3283. static bfd_boolean
  3284. avr_is_section_for_address (bfd *abfd, asection *section, bfd_vma address)
  3285. {
  3286. bfd_vma vma;
  3287. bfd_size_type size;
  3288. vma = bfd_get_section_vma (abfd, section);
  3289. if (address < vma)
  3290. return FALSE;
  3291. size = section->size;
  3292. if (address >= vma + size)
  3293. return FALSE;
  3294. return TRUE;
  3295. }
  3296. /* Data structure used by AVR_FIND_SECTION_FOR_ADDRESS. */
  3297. struct avr_find_section_data
  3298. {
  3299. /* The address we're looking for. */
  3300. bfd_vma address;
  3301. /* The section we've found. */
  3302. asection *section;
  3303. };
  3304. /* Helper function to locate the section holding a certain virtual memory
  3305. address. This is called via bfd_map_over_sections. The DATA is an
  3306. instance of STRUCT AVR_FIND_SECTION_DATA, the address field of which
  3307. has been set to the address to search for, and the section field has
  3308. been set to NULL. If SECTION from ABFD contains ADDRESS then the
  3309. section field in DATA will be set to SECTION. As an optimisation, if
  3310. the section field is already non-null then this function does not
  3311. perform any checks, and just returns. */
  3312. static void
  3313. avr_find_section_for_address (bfd *abfd,
  3314. asection *section, void *data)
  3315. {
  3316. struct avr_find_section_data *fs_data
  3317. = (struct avr_find_section_data *) data;
  3318. /* Return if already found. */
  3319. if (fs_data->section != NULL)
  3320. return;
  3321. /* If this section isn't part of the addressable code content, skip it. */
  3322. if ((bfd_get_section_flags (abfd, section) & SEC_ALLOC) == 0
  3323. && (bfd_get_section_flags (abfd, section) & SEC_CODE) == 0)
  3324. return;
  3325. if (avr_is_section_for_address (abfd, section, fs_data->address))
  3326. fs_data->section = section;
  3327. }
  3328. /* Load all of the property records from SEC, a section from ABFD. Return
  3329. a STRUCT AVR_PROPERTY_RECORD_LIST containing all the records. The
  3330. memory for the returned structure, and all of the records pointed too by
  3331. the structure are allocated with a single call to malloc, so, only the
  3332. pointer returned needs to be free'd. */
  3333. static struct avr_property_record_list *
  3334. avr_elf32_load_records_from_section (bfd *abfd, asection *sec)
  3335. {
  3336. char *contents = NULL, *ptr;
  3337. bfd_size_type size, mem_size;
  3338. bfd_byte version, flags;
  3339. uint16_t record_count, i;
  3340. struct avr_property_record_list *r_list = NULL;
  3341. Elf_Internal_Rela *internal_relocs = NULL, *rel, *rel_end;
  3342. struct avr_find_section_data fs_data;
  3343. fs_data.section = NULL;
  3344. size = bfd_get_section_size (sec);
  3345. contents = bfd_malloc (size);
  3346. bfd_get_section_contents (abfd, sec, contents, 0, size);
  3347. ptr = contents;
  3348. /* Load the relocations for the '.avr.prop' section if there are any, and
  3349. sort them. */
  3350. internal_relocs = (_bfd_elf_link_read_relocs
  3351. (abfd, sec, NULL, NULL, FALSE));
  3352. if (internal_relocs)
  3353. qsort (internal_relocs, sec->reloc_count,
  3354. sizeof (Elf_Internal_Rela), internal_reloc_compare);
  3355. /* There is a header at the start of the property record section SEC, the
  3356. format of this header is:
  3357. uint8_t : version number
  3358. uint8_t : flags
  3359. uint16_t : record counter
  3360. */
  3361. /* Check we have at least got a headers worth of bytes. */
  3362. if (size < AVR_PROPERTY_SECTION_HEADER_SIZE)
  3363. goto load_failed;
  3364. version = *((bfd_byte *) ptr);
  3365. ptr++;
  3366. flags = *((bfd_byte *) ptr);
  3367. ptr++;
  3368. record_count = *((uint16_t *) ptr);
  3369. ptr+=2;
  3370. BFD_ASSERT (ptr - contents == AVR_PROPERTY_SECTION_HEADER_SIZE);
  3371. /* Now allocate space for the list structure, and all of the list
  3372. elements in a single block. */
  3373. mem_size = sizeof (struct avr_property_record_list)
  3374. + sizeof (struct avr_property_record) * record_count;
  3375. r_list = bfd_malloc (mem_size);
  3376. if (r_list == NULL)
  3377. goto load_failed;
  3378. r_list->version = version;
  3379. r_list->flags = flags;
  3380. r_list->section = sec;
  3381. r_list->record_count = record_count;
  3382. r_list->records = (struct avr_property_record *) (&r_list [1]);
  3383. size -= AVR_PROPERTY_SECTION_HEADER_SIZE;
  3384. /* Check that we understand the version number. There is only one
  3385. version number right now, anything else is an error. */
  3386. if (r_list->version != AVR_PROPERTY_RECORDS_VERSION)
  3387. goto load_failed;
  3388. rel = internal_relocs;
  3389. rel_end = rel + sec->reloc_count;
  3390. for (i = 0; i < record_count; ++i)
  3391. {
  3392. bfd_vma address;
  3393. /* Each entry is a 32-bit address, followed by a single byte type.
  3394. After that is the type specific data. We must take care to
  3395. ensure that we don't read beyond the end of the section data. */
  3396. if (size < 5)
  3397. goto load_failed;
  3398. r_list->records [i].section = NULL;
  3399. r_list->records [i].offset = 0;
  3400. if (rel)
  3401. {
  3402. /* The offset of the address within the .avr.prop section. */
  3403. size_t offset = ptr - contents;
  3404. while (rel < rel_end && rel->r_offset < offset)
  3405. ++rel;
  3406. if (rel == rel_end)
  3407. rel = NULL;
  3408. else if (rel->r_offset == offset)
  3409. {
  3410. /* Find section and section offset. */
  3411. unsigned long r_symndx;
  3412. asection * rel_sec;
  3413. bfd_vma sec_offset;
  3414. r_symndx = ELF32_R_SYM (rel->r_info);
  3415. rel_sec = get_elf_r_symndx_section (abfd, r_symndx);
  3416. sec_offset = get_elf_r_symndx_offset (abfd, r_symndx)
  3417. + rel->r_addend;
  3418. r_list->records [i].section = rel_sec;
  3419. r_list->records [i].offset = sec_offset;
  3420. }
  3421. }
  3422. address = *((uint32_t *) ptr);
  3423. ptr += 4;
  3424. size -= 4;
  3425. if (r_list->records [i].section == NULL)
  3426. {
  3427. /* Try to find section and offset from address. */
  3428. if (fs_data.section != NULL
  3429. && !avr_is_section_for_address (abfd, fs_data.section,
  3430. address))
  3431. fs_data.section = NULL;
  3432. if (fs_data.section == NULL)
  3433. {
  3434. fs_data.address = address;
  3435. bfd_map_over_sections (abfd, avr_find_section_for_address,
  3436. &fs_data);
  3437. }
  3438. if (fs_data.section == NULL)
  3439. {
  3440. fprintf (stderr, "Failed to find matching section.\n");
  3441. goto load_failed;
  3442. }
  3443. r_list->records [i].section = fs_data.section;
  3444. r_list->records [i].offset
  3445. = address - bfd_get_section_vma (abfd, fs_data.section);
  3446. }
  3447. r_list->records [i].type = *((bfd_byte *) ptr);
  3448. ptr += 1;
  3449. size -= 1;
  3450. switch (r_list->records [i].type)
  3451. {
  3452. case RECORD_ORG:
  3453. /* Nothing else to load. */
  3454. break;
  3455. case RECORD_ORG_AND_FILL:
  3456. /* Just a 4-byte fill to load. */
  3457. if (size < 4)
  3458. goto load_failed;
  3459. r_list->records [i].data.org.fill = *((uint32_t *) ptr);
  3460. ptr += 4;
  3461. size -= 4;
  3462. break;
  3463. case RECORD_ALIGN:
  3464. /* Just a 4-byte alignment to load. */
  3465. if (size < 4)
  3466. goto load_failed;
  3467. r_list->records [i].data.align.bytes = *((uint32_t *) ptr);
  3468. ptr += 4;
  3469. size -= 4;
  3470. /* Just initialise PRECEDING_DELETED field, this field is
  3471. used during linker relaxation. */
  3472. r_list->records [i].data.align.preceding_deleted = 0;
  3473. break;
  3474. case RECORD_ALIGN_AND_FILL:
  3475. /* A 4-byte alignment, and a 4-byte fill to load. */
  3476. if (size < 8)
  3477. goto load_failed;
  3478. r_list->records [i].data.align.bytes = *((uint32_t *) ptr);
  3479. ptr += 4;
  3480. r_list->records [i].data.align.fill = *((uint32_t *) ptr);
  3481. ptr += 4;
  3482. size -= 8;
  3483. /* Just initialise PRECEDING_DELETED field, this field is
  3484. used during linker relaxation. */
  3485. r_list->records [i].data.align.preceding_deleted = 0;
  3486. break;
  3487. default:
  3488. goto load_failed;
  3489. }
  3490. }
  3491. free (contents);
  3492. free (internal_relocs);
  3493. return r_list;
  3494. load_failed:
  3495. free (internal_relocs);
  3496. free (contents);
  3497. free (r_list);
  3498. return NULL;
  3499. }
  3500. /* Load all of the property records from ABFD. See
  3501. AVR_ELF32_LOAD_RECORDS_FROM_SECTION for details of the return value. */
  3502. struct avr_property_record_list *
  3503. avr_elf32_load_property_records (bfd *abfd)
  3504. {
  3505. asection *sec;
  3506. /* Find the '.avr.prop' section and load the contents into memory. */
  3507. sec = bfd_get_section_by_name (abfd, AVR_PROPERTY_RECORD_SECTION_NAME);
  3508. if (sec == NULL)
  3509. return NULL;
  3510. return avr_elf32_load_records_from_section (abfd, sec);
  3511. }
  3512. const char *
  3513. avr_elf32_property_record_name (struct avr_property_record *rec)
  3514. {
  3515. const char *str;
  3516. switch (rec->type)
  3517. {
  3518. case RECORD_ORG:
  3519. str = "ORG";
  3520. break;
  3521. case RECORD_ORG_AND_FILL:
  3522. str = "ORG+FILL";
  3523. break;
  3524. case RECORD_ALIGN:
  3525. str = "ALIGN";
  3526. break;
  3527. case RECORD_ALIGN_AND_FILL:
  3528. str = "ALIGN+FILL";
  3529. break;
  3530. default:
  3531. str = "unknown";
  3532. }
  3533. return str;
  3534. }
  3535. #define ELF_ARCH bfd_arch_avr
  3536. #define ELF_TARGET_ID AVR_ELF_DATA
  3537. #define ELF_MACHINE_CODE EM_AVR
  3538. #define ELF_MACHINE_ALT1 EM_AVR_OLD
  3539. #define ELF_MAXPAGESIZE 1
  3540. #define TARGET_LITTLE_SYM avr_elf32_vec
  3541. #define TARGET_LITTLE_NAME "elf32-avr"
  3542. #define bfd_elf32_bfd_link_hash_table_create elf32_avr_link_hash_table_create
  3543. #define elf_info_to_howto avr_info_to_howto_rela
  3544. #define elf_info_to_howto_rel NULL
  3545. #define elf_backend_relocate_section elf32_avr_relocate_section
  3546. #define elf_backend_can_gc_sections 1
  3547. #define elf_backend_rela_normal 1
  3548. #define elf_backend_final_write_processing \
  3549. bfd_elf_avr_final_write_processing
  3550. #define elf_backend_object_p elf32_avr_object_p
  3551. #define bfd_elf32_bfd_relax_section elf32_avr_relax_section
  3552. #define bfd_elf32_bfd_get_relocated_section_contents \
  3553. elf32_avr_get_relocated_section_contents
  3554. #define bfd_elf32_new_section_hook elf_avr_new_section_hook
  3555. #include "elf32-target.h"