3
0

modutils-24.c 95 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438343934403441344234433444344534463447344834493450345134523453345434553456345734583459346034613462346334643465346634673468346934703471347234733474347534763477347834793480348134823483348434853486348734883489349034913492349334943495349634973498349935003501350235033504350535063507350835093510351135123513351435153516351735183519352035213522352335243525352635273528352935303531353235333534353535363537353835393540354135423543354435453546354735483549355035513552355335543555355635573558355935603561356235633564356535663567356835693570357135723573357435753576357735783579358035813582358335843585358635873588358935903591359235933594359535963597359835993600360136023603360436053606360736083609361036113612361336143615361636173618361936203621362236233624362536263627362836293630363136323633363436353636363736383639364036413642364336443645364636473648364936503651365236533654365536563657365836593660366136623663366436653666366736683669367036713672367336743675367636773678367936803681368236833684368536863687368836893690369136923693369436953696369736983699370037013702370337043705370637073708370937103711371237133714371537163717371837193720372137223723372437253726372737283729373037313732373337343735373637373738373937403741374237433744374537463747374837493750375137523753375437553756375737583759376037613762376337643765376637673768376937703771377237733774377537763777377837793780378137823783378437853786378737883789379037913792379337943795379637973798379938003801380238033804380538063807380838093810381138123813381438153816381738183819382038213822382338243825382638273828382938303831383238333834383538363837383838393840384138423843384438453846384738483849385038513852385338543855385638573858385938603861386238633864386538663867386838693870387138723873387438753876387738783879388038813882388338843885388638873888388938903891389238933894389538963897389838993900390139023903390439053906390739083909391039113912391339143915391639173918391939203921392239233924392539263927392839293930393139323933393439353936393739383939394039413942394339443945394639473948
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Mini insmod implementation for busybox
  4. *
  5. * This version of insmod supports ARM, CRIS, H8/300, x86, ia64, x86_64,
  6. * m68k, MIPS, PowerPC, S390, SH3/4/5, Sparc, v850e, and x86_64.
  7. *
  8. * Copyright (C) 1999-2004 by Erik Andersen <andersen@codepoet.org>
  9. * and Ron Alder <alder@lineo.com>
  10. *
  11. * Rodney Radford <rradford@mindspring.com> 17-Aug-2004.
  12. * Added x86_64 support.
  13. *
  14. * Miles Bader <miles@gnu.org> added NEC V850E support.
  15. *
  16. * Modified by Bryan Rittmeyer <bryan@ixiacom.com> to support SH4
  17. * and (theoretically) SH3. I have only tested SH4 in little endian mode.
  18. *
  19. * Modified by Alcove, Julien Gaulmin <julien.gaulmin@alcove.fr> and
  20. * Nicolas Ferre <nicolas.ferre@alcove.fr> to support ARM7TDMI. Only
  21. * very minor changes required to also work with StrongArm and presumably
  22. * all ARM based systems.
  23. *
  24. * Yoshinori Sato <ysato@users.sourceforge.jp> 19-May-2004.
  25. * added Renesas H8/300 support.
  26. *
  27. * Paul Mundt <lethal@linux-sh.org> 08-Aug-2003.
  28. * Integrated support for sh64 (SH-5), from preliminary modutils
  29. * patches from Benedict Gaster <benedict.gaster@superh.com>.
  30. * Currently limited to support for 32bit ABI.
  31. *
  32. * Magnus Damm <damm@opensource.se> 22-May-2002.
  33. * The plt and got code are now using the same structs.
  34. * Added generic linked list code to fully support PowerPC.
  35. * Replaced the mess in arch_apply_relocation() with architecture blocks.
  36. * The arch_create_got() function got cleaned up with architecture blocks.
  37. * These blocks should be easy maintain and sync with obj_xxx.c in modutils.
  38. *
  39. * Magnus Damm <damm@opensource.se> added PowerPC support 20-Feb-2001.
  40. * PowerPC specific code stolen from modutils-2.3.16,
  41. * written by Paul Mackerras, Copyright 1996, 1997 Linux International.
  42. * I've only tested the code on mpc8xx platforms in big-endian mode.
  43. * Did some cleanup and added USE_xxx_ENTRIES...
  44. *
  45. * Quinn Jensen <jensenq@lineo.com> added MIPS support 23-Feb-2001.
  46. * based on modutils-2.4.2
  47. * MIPS specific support for Elf loading and relocation.
  48. * Copyright 1996, 1997 Linux International.
  49. * Contributed by Ralf Baechle <ralf@gnu.ai.mit.edu>
  50. *
  51. * Based almost entirely on the Linux modutils-2.3.11 implementation.
  52. * Copyright 1996, 1997 Linux International.
  53. * New implementation contributed by Richard Henderson <rth@tamu.edu>
  54. * Based on original work by Bjorn Ekwall <bj0rn@blox.se>
  55. * Restructured (and partly rewritten) by:
  56. * Björn Ekwall <bj0rn@blox.se> February 1999
  57. *
  58. * Licensed under GPLv2 or later, see file LICENSE in this source tree.
  59. */
  60. #include "libbb.h"
  61. #include "modutils.h"
  62. #include <sys/utsname.h>
  63. #if ENABLE_FEATURE_INSMOD_LOADINKMEM
  64. #define LOADBITS 0
  65. #else
  66. #define LOADBITS 1
  67. #endif
  68. /* Alpha */
  69. #if defined(__alpha__)
  70. #define MATCH_MACHINE(x) (x == EM_ALPHA)
  71. #define SHT_RELM SHT_RELA
  72. #define Elf64_RelM Elf64_Rela
  73. #define ELFCLASSM ELFCLASS64
  74. #endif
  75. /* ARM support */
  76. #if defined(__arm__)
  77. #define MATCH_MACHINE(x) (x == EM_ARM)
  78. #define SHT_RELM SHT_REL
  79. #define Elf32_RelM Elf32_Rel
  80. #define ELFCLASSM ELFCLASS32
  81. #define USE_PLT_ENTRIES
  82. #define PLT_ENTRY_SIZE 8
  83. #define USE_GOT_ENTRIES
  84. #define GOT_ENTRY_SIZE 8
  85. #define USE_SINGLE
  86. #endif
  87. /* NDS32 support */
  88. #if defined(__nds32__) || defined(__NDS32__)
  89. #define CONFIG_USE_GOT_ENTRIES
  90. #define CONFIG_GOT_ENTRY_SIZE 4
  91. #define CONFIG_USE_SINGLE
  92. #if defined(__NDS32_EB__)
  93. #define MATCH_MACHINE(x) (x == EM_NDS32)
  94. #define SHT_RELM SHT_RELA
  95. #define Elf32_RelM Elf32_Rela
  96. #define ELFCLASSM ELFCLASS32
  97. #endif
  98. #if defined(__NDS32_EL__)
  99. #define MATCH_MACHINE(x) (x == EM_NDS32)
  100. #define SHT_RELM SHT_RELA
  101. #define Elf32_RelM Elf32_Rela
  102. #define ELFCLASSM ELFCLASS32
  103. #endif
  104. #endif
  105. /* blackfin */
  106. #if defined(BFIN)
  107. #define MATCH_MACHINE(x) (x == EM_BLACKFIN)
  108. #define SHT_RELM SHT_RELA
  109. #define Elf32_RelM Elf32_Rela
  110. #define ELFCLASSM ELFCLASS32
  111. #endif
  112. /* CRIS */
  113. #if defined(__cris__)
  114. #define MATCH_MACHINE(x) (x == EM_CRIS)
  115. #define SHT_RELM SHT_RELA
  116. #define Elf32_RelM Elf32_Rela
  117. #define ELFCLASSM ELFCLASS32
  118. #ifndef EM_CRIS
  119. #define EM_CRIS 76
  120. #define R_CRIS_NONE 0
  121. #define R_CRIS_32 3
  122. #endif
  123. #endif
  124. /* H8/300 */
  125. #if defined(__H8300H__) || defined(__H8300S__)
  126. #define MATCH_MACHINE(x) (x == EM_H8_300)
  127. #define SHT_RELM SHT_RELA
  128. #define Elf32_RelM Elf32_Rela
  129. #define ELFCLASSM ELFCLASS32
  130. #define USE_SINGLE
  131. #define SYMBOL_PREFIX "_"
  132. #endif
  133. /* PA-RISC / HP-PA */
  134. #if defined(__hppa__)
  135. #define MATCH_MACHINE(x) (x == EM_PARISC)
  136. #define SHT_RELM SHT_RELA
  137. #if defined(__LP64__)
  138. #define Elf64_RelM Elf64_Rela
  139. #define ELFCLASSM ELFCLASS64
  140. #else
  141. #define Elf32_RelM Elf32_Rela
  142. #define ELFCLASSM ELFCLASS32
  143. #endif
  144. #endif
  145. /* x86 */
  146. #if defined(__i386__)
  147. #ifndef EM_486
  148. #define MATCH_MACHINE(x) (x == EM_386)
  149. #else
  150. #define MATCH_MACHINE(x) (x == EM_386 || x == EM_486)
  151. #endif
  152. #define SHT_RELM SHT_REL
  153. #define Elf32_RelM Elf32_Rel
  154. #define ELFCLASSM ELFCLASS32
  155. #define USE_GOT_ENTRIES
  156. #define GOT_ENTRY_SIZE 4
  157. #define USE_SINGLE
  158. #endif
  159. /* IA64, aka Itanium */
  160. #if defined(__ia64__)
  161. #define MATCH_MACHINE(x) (x == EM_IA_64)
  162. #define SHT_RELM SHT_RELA
  163. #define Elf64_RelM Elf64_Rela
  164. #define ELFCLASSM ELFCLASS64
  165. #endif
  166. /* m68k */
  167. #if defined(__mc68000__)
  168. #define MATCH_MACHINE(x) (x == EM_68K)
  169. #define SHT_RELM SHT_RELA
  170. #define Elf32_RelM Elf32_Rela
  171. #define ELFCLASSM ELFCLASS32
  172. #define USE_GOT_ENTRIES
  173. #define GOT_ENTRY_SIZE 4
  174. #define USE_SINGLE
  175. #endif
  176. /* Microblaze */
  177. #if defined(__microblaze__)
  178. #define USE_SINGLE
  179. #include <linux/elf-em.h>
  180. #define MATCH_MACHINE(x) (x == EM_XILINX_MICROBLAZE)
  181. #define SHT_RELM SHT_RELA
  182. #define Elf32_RelM Elf32_Rela
  183. #define ELFCLASSM ELFCLASS32
  184. #endif
  185. /* MIPS */
  186. #if defined(__mips__)
  187. #define MATCH_MACHINE(x) (x == EM_MIPS || x == EM_MIPS_RS3_LE)
  188. #define SHT_RELM SHT_REL
  189. #define Elf32_RelM Elf32_Rel
  190. #define ELFCLASSM ELFCLASS32
  191. /* Account for ELF spec changes. */
  192. #ifndef EM_MIPS_RS3_LE
  193. #ifdef EM_MIPS_RS4_BE
  194. #define EM_MIPS_RS3_LE EM_MIPS_RS4_BE
  195. #else
  196. #define EM_MIPS_RS3_LE 10
  197. #endif
  198. #endif /* !EM_MIPS_RS3_LE */
  199. #define ARCHDATAM "__dbe_table"
  200. #endif
  201. /* Nios II */
  202. #if defined(__nios2__)
  203. #define MATCH_MACHINE(x) (x == EM_ALTERA_NIOS2)
  204. #define SHT_RELM SHT_RELA
  205. #define Elf32_RelM Elf32_Rela
  206. #define ELFCLASSM ELFCLASS32
  207. #endif
  208. /* PowerPC */
  209. #if defined(__powerpc64__)
  210. #define MATCH_MACHINE(x) (x == EM_PPC64)
  211. #define SHT_RELM SHT_RELA
  212. #define Elf64_RelM Elf64_Rela
  213. #define ELFCLASSM ELFCLASS64
  214. #elif defined(__powerpc__)
  215. #define MATCH_MACHINE(x) (x == EM_PPC)
  216. #define SHT_RELM SHT_RELA
  217. #define Elf32_RelM Elf32_Rela
  218. #define ELFCLASSM ELFCLASS32
  219. #define USE_PLT_ENTRIES
  220. #define PLT_ENTRY_SIZE 16
  221. #define USE_PLT_LIST
  222. #define LIST_ARCHTYPE ElfW(Addr)
  223. #define USE_LIST
  224. #define ARCHDATAM "__ftr_fixup"
  225. #endif
  226. /* S390 */
  227. #if defined(__s390__)
  228. #define MATCH_MACHINE(x) (x == EM_S390)
  229. #define SHT_RELM SHT_RELA
  230. #define Elf32_RelM Elf32_Rela
  231. #define ELFCLASSM ELFCLASS32
  232. #define USE_PLT_ENTRIES
  233. #define PLT_ENTRY_SIZE 8
  234. #define USE_GOT_ENTRIES
  235. #define GOT_ENTRY_SIZE 8
  236. #define USE_SINGLE
  237. #endif
  238. /* SuperH */
  239. #if defined(__sh__)
  240. #define MATCH_MACHINE(x) (x == EM_SH)
  241. #define SHT_RELM SHT_RELA
  242. #define Elf32_RelM Elf32_Rela
  243. #define ELFCLASSM ELFCLASS32
  244. #define USE_GOT_ENTRIES
  245. #define GOT_ENTRY_SIZE 4
  246. #define USE_SINGLE
  247. /* the SH changes have only been tested in =little endian= mode */
  248. /* I'm not sure about big endian, so let's warn: */
  249. #if defined(__sh__) && BB_BIG_ENDIAN
  250. # error insmod.c may require changes for use on big endian SH
  251. #endif
  252. /* it may or may not work on the SH1/SH2... Error on those also */
  253. #if ((!(defined(__SH3__) || defined(__SH4__) || defined(__SH5__)))) && (defined(__sh__))
  254. #error insmod.c may require changes for SH1 or SH2 use
  255. #endif
  256. #endif
  257. /* Sparc */
  258. #if defined(__sparc__)
  259. #define MATCH_MACHINE(x) (x == EM_SPARC)
  260. #define SHT_RELM SHT_RELA
  261. #define Elf32_RelM Elf32_Rela
  262. #define ELFCLASSM ELFCLASS32
  263. #endif
  264. /* v850e */
  265. #if defined(__v850e__)
  266. #define MATCH_MACHINE(x) ((x) == EM_V850 || (x) == EM_CYGNUS_V850)
  267. #define SHT_RELM SHT_RELA
  268. #define Elf32_RelM Elf32_Rela
  269. #define ELFCLASSM ELFCLASS32
  270. #define USE_PLT_ENTRIES
  271. #define PLT_ENTRY_SIZE 8
  272. #define USE_SINGLE
  273. #ifndef EM_CYGNUS_V850 /* grumble */
  274. #define EM_CYGNUS_V850 0x9080
  275. #endif
  276. #define SYMBOL_PREFIX "_"
  277. #endif
  278. /* X86_64 */
  279. #if defined(__x86_64__)
  280. #define MATCH_MACHINE(x) (x == EM_X86_64)
  281. #define SHT_RELM SHT_RELA
  282. #define USE_GOT_ENTRIES
  283. #define GOT_ENTRY_SIZE 8
  284. #define USE_SINGLE
  285. #define Elf64_RelM Elf64_Rela
  286. #define ELFCLASSM ELFCLASS64
  287. #endif
  288. #ifndef SHT_RELM
  289. #error Sorry, but insmod.c does not yet support this architecture...
  290. #endif
  291. //----------------------------------------------------------------------------
  292. //--------modutils module.h, lines 45-242
  293. //----------------------------------------------------------------------------
  294. /* Definitions for the Linux module syscall interface.
  295. Copyright 1996, 1997 Linux International.
  296. Contributed by Richard Henderson <rth@tamu.edu>
  297. This file is part of the Linux modutils.
  298. This program is free software; you can redistribute it and/or modify it
  299. under the terms of the GNU General Public License as published by the
  300. Free Software Foundation; either version 2 of the License, or (at your
  301. option) any later version.
  302. This program is distributed in the hope that it will be useful, but
  303. WITHOUT ANY WARRANTY; without even the implied warranty of
  304. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  305. General Public License for more details.
  306. You should have received a copy of the GNU General Public License
  307. along with this program; if not, write to the Free Software Foundation,
  308. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  309. #ifndef MODUTILS_MODULE_H
  310. /*======================================================================*/
  311. /* For sizeof() which are related to the module platform and not to the
  312. environment isnmod is running in, use sizeof_xx instead of sizeof(xx). */
  313. #define tgt_sizeof_char sizeof(char)
  314. #define tgt_sizeof_short sizeof(short)
  315. #define tgt_sizeof_int sizeof(int)
  316. #define tgt_sizeof_long sizeof(long)
  317. #define tgt_sizeof_char_p sizeof(char *)
  318. #define tgt_sizeof_void_p sizeof(void *)
  319. #define tgt_long long
  320. #if defined(__sparc__) && !defined(__sparc_v9__) && defined(ARCH_sparc64)
  321. #undef tgt_sizeof_long
  322. #undef tgt_sizeof_char_p
  323. #undef tgt_sizeof_void_p
  324. #undef tgt_long
  325. enum {
  326. tgt_sizeof_long = 8,
  327. tgt_sizeof_char_p = 8,
  328. tgt_sizeof_void_p = 8
  329. };
  330. #define tgt_long long long
  331. #endif
  332. /*======================================================================*/
  333. /* The structures used in Linux 2.1. */
  334. /* Note: new_module_symbol does not use tgt_long intentionally */
  335. struct new_module_symbol {
  336. unsigned long value;
  337. unsigned long name;
  338. };
  339. struct new_module_persist;
  340. struct new_module_ref {
  341. unsigned tgt_long dep; /* kernel addresses */
  342. unsigned tgt_long ref;
  343. unsigned tgt_long next_ref;
  344. };
  345. struct new_module {
  346. unsigned tgt_long size_of_struct; /* == sizeof(module) */
  347. unsigned tgt_long next;
  348. unsigned tgt_long name;
  349. unsigned tgt_long size;
  350. tgt_long usecount;
  351. unsigned tgt_long flags; /* AUTOCLEAN et al */
  352. unsigned nsyms;
  353. unsigned ndeps;
  354. unsigned tgt_long syms;
  355. unsigned tgt_long deps;
  356. unsigned tgt_long refs;
  357. unsigned tgt_long init;
  358. unsigned tgt_long cleanup;
  359. unsigned tgt_long ex_table_start;
  360. unsigned tgt_long ex_table_end;
  361. #ifdef __alpha__
  362. unsigned tgt_long gp;
  363. #endif
  364. /* Everything after here is extension. */
  365. unsigned tgt_long persist_start;
  366. unsigned tgt_long persist_end;
  367. unsigned tgt_long can_unload;
  368. unsigned tgt_long runsize;
  369. const char *kallsyms_start; /* All symbols for kernel debugging */
  370. const char *kallsyms_end;
  371. const char *archdata_start; /* arch specific data for module */
  372. const char *archdata_end;
  373. const char *kernel_data; /* Reserved for kernel internal use */
  374. };
  375. #ifdef ARCHDATAM
  376. #define ARCHDATA_SEC_NAME ARCHDATAM
  377. #else
  378. #define ARCHDATA_SEC_NAME "__archdata"
  379. #endif
  380. #define KALLSYMS_SEC_NAME "__kallsyms"
  381. struct new_module_info {
  382. unsigned long addr;
  383. unsigned long size;
  384. unsigned long flags;
  385. long usecount;
  386. };
  387. /* Bits of module.flags. */
  388. enum {
  389. NEW_MOD_RUNNING = 1,
  390. NEW_MOD_DELETED = 2,
  391. NEW_MOD_AUTOCLEAN = 4,
  392. NEW_MOD_VISITED = 8,
  393. NEW_MOD_USED_ONCE = 16
  394. };
  395. int init_module(const char *name, const struct new_module *);
  396. int query_module(const char *name, int which, void *buf,
  397. size_t bufsize, size_t *ret);
  398. /* Values for query_module's which. */
  399. enum {
  400. QM_MODULES = 1,
  401. QM_DEPS = 2,
  402. QM_REFS = 3,
  403. QM_SYMBOLS = 4,
  404. QM_INFO = 5
  405. };
  406. /*======================================================================*/
  407. /* The system calls unchanged between 2.0 and 2.1. */
  408. unsigned long create_module(const char *, size_t);
  409. int delete_module(const char *module, unsigned int flags);
  410. #endif /* module.h */
  411. //----------------------------------------------------------------------------
  412. //--------end of modutils module.h
  413. //----------------------------------------------------------------------------
  414. //----------------------------------------------------------------------------
  415. //--------modutils obj.h, lines 253-462
  416. //----------------------------------------------------------------------------
  417. /* Elf object file loading and relocation routines.
  418. Copyright 1996, 1997 Linux International.
  419. Contributed by Richard Henderson <rth@tamu.edu>
  420. This file is part of the Linux modutils.
  421. This program is free software; you can redistribute it and/or modify it
  422. under the terms of the GNU General Public License as published by the
  423. Free Software Foundation; either version 2 of the License, or (at your
  424. option) any later version.
  425. This program is distributed in the hope that it will be useful, but
  426. WITHOUT ANY WARRANTY; without even the implied warranty of
  427. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  428. General Public License for more details.
  429. You should have received a copy of the GNU General Public License
  430. along with this program; if not, write to the Free Software Foundation,
  431. Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
  432. #ifndef MODUTILS_OBJ_H
  433. /* The relocatable object is manipulated using elfin types. */
  434. #include <elf.h>
  435. #include <endian.h>
  436. #ifndef ElfW
  437. # if ELFCLASSM == ELFCLASS32
  438. # define ElfW(x) Elf32_ ## x
  439. # define ELFW(x) ELF32_ ## x
  440. # else
  441. # define ElfW(x) Elf64_ ## x
  442. # define ELFW(x) ELF64_ ## x
  443. # endif
  444. #endif
  445. /* For some reason this is missing from some ancient C libraries.... */
  446. #ifndef ELF32_ST_INFO
  447. # define ELF32_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
  448. #endif
  449. #ifndef ELF64_ST_INFO
  450. # define ELF64_ST_INFO(bind, type) (((bind) << 4) + ((type) & 0xf))
  451. #endif
  452. #define ELF_ST_BIND(info) ELFW(ST_BIND)(info)
  453. #define ELF_ST_TYPE(info) ELFW(ST_TYPE)(info)
  454. #define ELF_ST_INFO(bind, type) ELFW(ST_INFO)(bind, type)
  455. #define ELF_R_TYPE(val) ELFW(R_TYPE)(val)
  456. #define ELF_R_SYM(val) ELFW(R_SYM)(val)
  457. struct obj_string_patch;
  458. struct obj_symbol_patch;
  459. struct obj_section {
  460. ElfW(Shdr) header;
  461. const char *name;
  462. char *contents;
  463. struct obj_section *load_next;
  464. int idx;
  465. };
  466. struct obj_symbol {
  467. struct obj_symbol *next; /* hash table link */
  468. const char *name;
  469. unsigned long value;
  470. unsigned long size;
  471. int secidx; /* the defining section index/module */
  472. int info;
  473. int ksymidx; /* for export to the kernel symtab */
  474. int referenced; /* actually used in the link */
  475. };
  476. /* Hardcode the hash table size. We shouldn't be needing so many
  477. symbols that we begin to degrade performance, and we get a big win
  478. by giving the compiler a constant divisor. */
  479. #define HASH_BUCKETS 521
  480. struct obj_file {
  481. ElfW(Ehdr) header;
  482. ElfW(Addr) baseaddr;
  483. struct obj_section **sections;
  484. struct obj_section *load_order;
  485. struct obj_section **load_order_search_start;
  486. struct obj_string_patch *string_patches;
  487. struct obj_symbol_patch *symbol_patches;
  488. int (*symbol_cmp)(const char *, const char *); /* cant be FAST_FUNC */
  489. unsigned long (*symbol_hash)(const char *) FAST_FUNC;
  490. unsigned long local_symtab_size;
  491. struct obj_symbol **local_symtab;
  492. struct obj_symbol *symtab[HASH_BUCKETS];
  493. };
  494. enum obj_reloc {
  495. obj_reloc_ok,
  496. obj_reloc_overflow,
  497. obj_reloc_dangerous,
  498. obj_reloc_unhandled
  499. };
  500. struct obj_string_patch {
  501. struct obj_string_patch *next;
  502. int reloc_secidx;
  503. ElfW(Addr) reloc_offset;
  504. ElfW(Addr) string_offset;
  505. };
  506. struct obj_symbol_patch {
  507. struct obj_symbol_patch *next;
  508. int reloc_secidx;
  509. ElfW(Addr) reloc_offset;
  510. struct obj_symbol *sym;
  511. };
  512. /* Generic object manipulation routines. */
  513. static unsigned long FAST_FUNC obj_elf_hash(const char *);
  514. static unsigned long obj_elf_hash_n(const char *, unsigned long len);
  515. static struct obj_symbol *obj_find_symbol(struct obj_file *f,
  516. const char *name);
  517. static ElfW(Addr) obj_symbol_final_value(struct obj_file *f,
  518. struct obj_symbol *sym);
  519. #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
  520. static void obj_set_symbol_compare(struct obj_file *f,
  521. int (*cmp)(const char *, const char *),
  522. unsigned long (*hash)(const char *) FAST_FUNC);
  523. #endif
  524. static struct obj_section *obj_find_section(struct obj_file *f,
  525. const char *name);
  526. static void obj_insert_section_load_order(struct obj_file *f,
  527. struct obj_section *sec);
  528. static struct obj_section *obj_create_alloced_section(struct obj_file *f,
  529. const char *name,
  530. unsigned long align,
  531. unsigned long size);
  532. static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
  533. const char *name,
  534. unsigned long align,
  535. unsigned long size);
  536. static void *obj_extend_section(struct obj_section *sec, unsigned long more);
  537. static void obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
  538. const char *string);
  539. static void obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
  540. struct obj_symbol *sym);
  541. static void obj_check_undefineds(struct obj_file *f);
  542. static void obj_allocate_commons(struct obj_file *f);
  543. static unsigned long obj_load_size(struct obj_file *f);
  544. static int obj_relocate(struct obj_file *f, ElfW(Addr) base);
  545. #if !LOADBITS
  546. #define obj_load(image, image_size, loadprogbits) \
  547. obj_load(image, image_size)
  548. #endif
  549. static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbits);
  550. static int obj_create_image(struct obj_file *f, char *image);
  551. /* Architecture specific manipulation routines. */
  552. static struct obj_file *arch_new_file(void);
  553. static struct obj_section *arch_new_section(void);
  554. static struct obj_symbol *arch_new_symbol(void);
  555. static enum obj_reloc arch_apply_relocation(struct obj_file *f,
  556. struct obj_section *targsec,
  557. /*struct obj_section *symsec,*/
  558. struct obj_symbol *sym,
  559. ElfW(RelM) *rel, ElfW(Addr) value);
  560. static void arch_create_got(struct obj_file *f);
  561. #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
  562. static int obj_gpl_license(struct obj_file *f, const char **license);
  563. #endif
  564. #endif /* obj.h */
  565. //----------------------------------------------------------------------------
  566. //--------end of modutils obj.h
  567. //----------------------------------------------------------------------------
  568. /* SPFX is always a string, so it can be concatenated to string constants. */
  569. #ifdef SYMBOL_PREFIX
  570. #define SPFX SYMBOL_PREFIX
  571. #else
  572. #define SPFX ""
  573. #endif
  574. enum { STRVERSIONLEN = 64 };
  575. /*======================================================================*/
  576. #define flag_force_load (option_mask32 & INSMOD_OPT_FORCE)
  577. #define flag_autoclean (option_mask32 & INSMOD_OPT_KERNELD)
  578. #define flag_verbose (option_mask32 & INSMOD_OPT_VERBOSE)
  579. #define flag_quiet (option_mask32 & INSMOD_OPT_SILENT)
  580. #define flag_noexport (option_mask32 & INSMOD_OPT_NO_EXPORT)
  581. #define flag_print_load_map (option_mask32 & INSMOD_OPT_PRINT_MAP)
  582. /*======================================================================*/
  583. #if defined(USE_LIST)
  584. struct arch_list_entry {
  585. struct arch_list_entry *next;
  586. LIST_ARCHTYPE addend;
  587. int offset;
  588. int inited : 1;
  589. };
  590. #endif
  591. #if defined(USE_SINGLE)
  592. struct arch_single_entry {
  593. int offset;
  594. int inited : 1;
  595. int allocated : 1;
  596. };
  597. #endif
  598. #if defined(__mips__)
  599. struct mips_hi16 {
  600. struct mips_hi16 *next;
  601. ElfW(Addr) *addr;
  602. ElfW(Addr) value;
  603. };
  604. #endif
  605. struct arch_file {
  606. struct obj_file root;
  607. #if defined(USE_PLT_ENTRIES)
  608. struct obj_section *plt;
  609. #endif
  610. #if defined(USE_GOT_ENTRIES)
  611. struct obj_section *got;
  612. #endif
  613. #if defined(__mips__)
  614. struct mips_hi16 *mips_hi16_list;
  615. #endif
  616. };
  617. struct arch_symbol {
  618. struct obj_symbol root;
  619. #if defined(USE_PLT_ENTRIES)
  620. #if defined(USE_PLT_LIST)
  621. struct arch_list_entry *pltent;
  622. #else
  623. struct arch_single_entry pltent;
  624. #endif
  625. #endif
  626. #if defined(USE_GOT_ENTRIES)
  627. struct arch_single_entry gotent;
  628. #endif
  629. };
  630. struct external_module {
  631. const char *name;
  632. ElfW(Addr) addr;
  633. int used;
  634. size_t nsyms;
  635. struct new_module_symbol *syms;
  636. };
  637. static struct new_module_symbol *ksyms;
  638. static size_t nksyms;
  639. static struct external_module *ext_modules;
  640. static int n_ext_modules;
  641. static int n_ext_modules_used;
  642. /*======================================================================*/
  643. static struct obj_file *arch_new_file(void)
  644. {
  645. struct arch_file *f;
  646. f = xzalloc(sizeof(*f));
  647. return &f->root; /* it's a first member */
  648. }
  649. static struct obj_section *arch_new_section(void)
  650. {
  651. return xzalloc(sizeof(struct obj_section));
  652. }
  653. static struct obj_symbol *arch_new_symbol(void)
  654. {
  655. struct arch_symbol *sym;
  656. sym = xzalloc(sizeof(*sym));
  657. return &sym->root;
  658. }
  659. static enum obj_reloc
  660. arch_apply_relocation(struct obj_file *f,
  661. struct obj_section *targsec,
  662. /*struct obj_section *symsec,*/
  663. struct obj_symbol *sym,
  664. ElfW(RelM) *rel, ElfW(Addr) v)
  665. {
  666. #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
  667. || defined(__sh__) || defined(__s390__) || defined(__x86_64__) \
  668. || defined(__powerpc__) || defined(__mips__)
  669. struct arch_file *ifile = (struct arch_file *) f;
  670. #endif
  671. enum obj_reloc ret = obj_reloc_ok;
  672. ElfW(Addr) *loc = (ElfW(Addr) *) (targsec->contents + rel->r_offset);
  673. #if defined(__arm__) || defined(__H8300H__) || defined(__H8300S__) \
  674. || defined(__i386__) || defined(__mc68000__) || defined(__microblaze__) \
  675. || defined(__mips__) || defined(__nios2__) || defined(__powerpc__) \
  676. || defined(__s390__) || defined(__sh__) || defined(__x86_64__)
  677. ElfW(Addr) dot = targsec->header.sh_addr + rel->r_offset;
  678. #endif
  679. #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
  680. struct arch_symbol *isym = (struct arch_symbol *) sym;
  681. #endif
  682. #if defined(__arm__) || defined(__i386__) || defined(__mc68000__) \
  683. || defined(__sh__) || defined(__s390__)
  684. #if defined(USE_GOT_ENTRIES)
  685. ElfW(Addr) got = ifile->got ? ifile->got->header.sh_addr : 0;
  686. #endif
  687. #endif
  688. #if defined(USE_PLT_ENTRIES)
  689. ElfW(Addr) plt = ifile->plt ? ifile->plt->header.sh_addr : 0;
  690. unsigned long *ip;
  691. # if defined(USE_PLT_LIST)
  692. struct arch_list_entry *pe;
  693. # else
  694. struct arch_single_entry *pe;
  695. # endif
  696. #endif
  697. switch (ELF_R_TYPE(rel->r_info)) {
  698. #if defined(__arm__)
  699. case R_ARM_NONE:
  700. break;
  701. case R_ARM_ABS32:
  702. *loc += v;
  703. break;
  704. case R_ARM_GOT32:
  705. goto bb_use_got;
  706. case R_ARM_GOTPC:
  707. /* relative reloc, always to _GLOBAL_OFFSET_TABLE_
  708. * (which is .got) similar to branch,
  709. * but is full 32 bits relative */
  710. *loc += got - dot;
  711. break;
  712. case R_ARM_PC24:
  713. case R_ARM_PLT32:
  714. goto bb_use_plt;
  715. case R_ARM_GOTOFF: /* address relative to the got */
  716. *loc += v - got;
  717. break;
  718. #elif defined(__cris__)
  719. case R_CRIS_NONE:
  720. break;
  721. case R_CRIS_32:
  722. /* CRIS keeps the relocation value in the r_addend field and
  723. * should not use whats in *loc at all
  724. */
  725. *loc = v;
  726. break;
  727. #elif defined(__H8300H__) || defined(__H8300S__)
  728. case R_H8_DIR24R8:
  729. loc = (ElfW(Addr) *)((ElfW(Addr))loc - 1);
  730. *loc = (*loc & 0xff000000) | ((*loc & 0xffffff) + v);
  731. break;
  732. case R_H8_DIR24A8:
  733. *loc += v;
  734. break;
  735. case R_H8_DIR32:
  736. case R_H8_DIR32A16:
  737. *loc += v;
  738. break;
  739. case R_H8_PCREL16:
  740. v -= dot + 2;
  741. if ((ElfW(Sword))v > 0x7fff
  742. || (ElfW(Sword))v < -(ElfW(Sword))0x8000
  743. ) {
  744. ret = obj_reloc_overflow;
  745. } else {
  746. *(unsigned short *)loc = v;
  747. }
  748. break;
  749. case R_H8_PCREL8:
  750. v -= dot + 1;
  751. if ((ElfW(Sword))v > 0x7f
  752. || (ElfW(Sword))v < -(ElfW(Sword))0x80
  753. ) {
  754. ret = obj_reloc_overflow;
  755. } else {
  756. *(unsigned char *)loc = v;
  757. }
  758. break;
  759. #elif defined(__i386__)
  760. case R_386_NONE:
  761. break;
  762. case R_386_32:
  763. *loc += v;
  764. break;
  765. case R_386_PLT32:
  766. case R_386_PC32:
  767. case R_386_GOTOFF:
  768. *loc += v - dot;
  769. break;
  770. case R_386_GLOB_DAT:
  771. case R_386_JMP_SLOT:
  772. *loc = v;
  773. break;
  774. case R_386_RELATIVE:
  775. *loc += f->baseaddr;
  776. break;
  777. case R_386_GOTPC:
  778. *loc += got - dot;
  779. break;
  780. case R_386_GOT32:
  781. goto bb_use_got;
  782. break;
  783. #elif defined(__microblaze__)
  784. case R_MICROBLAZE_NONE:
  785. case R_MICROBLAZE_64_NONE:
  786. case R_MICROBLAZE_32_SYM_OP_SYM:
  787. case R_MICROBLAZE_32_PCREL:
  788. break;
  789. case R_MICROBLAZE_64_PCREL: {
  790. /* dot is the address of the current instruction.
  791. * v is the target symbol address.
  792. * So we need to extract the offset in the code,
  793. * adding v, then subtrating the current address
  794. * of this instruction.
  795. * Ex: "IMM 0xFFFE bralid 0x0000" = "bralid 0xFFFE0000"
  796. */
  797. /* Get split offset stored in code */
  798. unsigned int temp = (loc[0] & 0xFFFF) << 16 |
  799. (loc[1] & 0xFFFF);
  800. /* Adjust relative offset. -4 adjustment required
  801. * because dot points to the IMM insn, but branch
  802. * is computed relative to the branch instruction itself.
  803. */
  804. temp += v - dot - 4;
  805. /* Store back into code */
  806. loc[0] = (loc[0] & 0xFFFF0000) | temp >> 16;
  807. loc[1] = (loc[1] & 0xFFFF0000) | (temp & 0xFFFF);
  808. break;
  809. }
  810. case R_MICROBLAZE_32:
  811. *loc += v;
  812. break;
  813. case R_MICROBLAZE_64: {
  814. /* Get split pointer stored in code */
  815. unsigned int temp1 = (loc[0] & 0xFFFF) << 16 |
  816. (loc[1] & 0xFFFF);
  817. /* Add reloc offset */
  818. temp1+=v;
  819. /* Store back into code */
  820. loc[0] = (loc[0] & 0xFFFF0000) | temp1 >> 16;
  821. loc[1] = (loc[1] & 0xFFFF0000) | (temp1 & 0xFFFF);
  822. break;
  823. }
  824. case R_MICROBLAZE_32_PCREL_LO:
  825. case R_MICROBLAZE_32_LO:
  826. case R_MICROBLAZE_SRO32:
  827. case R_MICROBLAZE_SRW32:
  828. ret = obj_reloc_unhandled;
  829. break;
  830. #elif defined(__mc68000__)
  831. case R_68K_NONE:
  832. break;
  833. case R_68K_32:
  834. *loc += v;
  835. break;
  836. case R_68K_8:
  837. if (v > 0xff) {
  838. ret = obj_reloc_overflow;
  839. }
  840. *(char *)loc = v;
  841. break;
  842. case R_68K_16:
  843. if (v > 0xffff) {
  844. ret = obj_reloc_overflow;
  845. }
  846. *(short *)loc = v;
  847. break;
  848. case R_68K_PC8:
  849. v -= dot;
  850. if ((ElfW(Sword))v > 0x7f
  851. || (ElfW(Sword))v < -(ElfW(Sword))0x80
  852. ) {
  853. ret = obj_reloc_overflow;
  854. }
  855. *(char *)loc = v;
  856. break;
  857. case R_68K_PC16:
  858. v -= dot;
  859. if ((ElfW(Sword))v > 0x7fff
  860. || (ElfW(Sword))v < -(ElfW(Sword))0x8000
  861. ) {
  862. ret = obj_reloc_overflow;
  863. }
  864. *(short *)loc = v;
  865. break;
  866. case R_68K_PC32:
  867. *(int *)loc = v - dot;
  868. break;
  869. case R_68K_GLOB_DAT:
  870. case R_68K_JMP_SLOT:
  871. *loc = v;
  872. break;
  873. case R_68K_RELATIVE:
  874. *(int *)loc += f->baseaddr;
  875. break;
  876. case R_68K_GOT32:
  877. goto bb_use_got;
  878. # ifdef R_68K_GOTOFF
  879. case R_68K_GOTOFF:
  880. *loc += v - got;
  881. break;
  882. # endif
  883. #elif defined(__mips__)
  884. case R_MIPS_NONE:
  885. break;
  886. case R_MIPS_32:
  887. *loc += v;
  888. break;
  889. case R_MIPS_26:
  890. if (v % 4)
  891. ret = obj_reloc_dangerous;
  892. if ((v & 0xf0000000) != ((dot + 4) & 0xf0000000))
  893. ret = obj_reloc_overflow;
  894. *loc =
  895. (*loc & ~0x03ffffff) | ((*loc + (v >> 2)) &
  896. 0x03ffffff);
  897. break;
  898. case R_MIPS_HI16:
  899. {
  900. struct mips_hi16 *n;
  901. /* We cannot relocate this one now because we don't know the value
  902. of the carry we need to add. Save the information, and let LO16
  903. do the actual relocation. */
  904. n = xmalloc(sizeof *n);
  905. n->addr = loc;
  906. n->value = v;
  907. n->next = ifile->mips_hi16_list;
  908. ifile->mips_hi16_list = n;
  909. break;
  910. }
  911. case R_MIPS_LO16:
  912. {
  913. unsigned long insnlo = *loc;
  914. ElfW(Addr) val, vallo;
  915. /* Sign extend the addend we extract from the lo insn. */
  916. vallo = ((insnlo & 0xffff) ^ 0x8000) - 0x8000;
  917. if (ifile->mips_hi16_list != NULL) {
  918. struct mips_hi16 *l;
  919. l = ifile->mips_hi16_list;
  920. while (l != NULL) {
  921. struct mips_hi16 *next;
  922. unsigned long insn;
  923. /* Do the HI16 relocation. Note that we actually don't
  924. need to know anything about the LO16 itself, except where
  925. to find the low 16 bits of the addend needed by the LO16. */
  926. insn = *l->addr;
  927. val =
  928. ((insn & 0xffff) << 16) +
  929. vallo;
  930. val += v;
  931. /* Account for the sign extension that will happen in the
  932. low bits. */
  933. val =
  934. ((val >> 16) +
  935. ((val & 0x8000) !=
  936. 0)) & 0xffff;
  937. insn = (insn & ~0xffff) | val;
  938. *l->addr = insn;
  939. next = l->next;
  940. free(l);
  941. l = next;
  942. }
  943. ifile->mips_hi16_list = NULL;
  944. }
  945. /* Ok, we're done with the HI16 relocs. Now deal with the LO16. */
  946. val = v + vallo;
  947. insnlo = (insnlo & ~0xffff) | (val & 0xffff);
  948. *loc = insnlo;
  949. break;
  950. }
  951. #elif defined(__nios2__)
  952. case R_NIOS2_NONE:
  953. break;
  954. case R_NIOS2_BFD_RELOC_32:
  955. *loc += v;
  956. break;
  957. case R_NIOS2_BFD_RELOC_16:
  958. if (v > 0xffff) {
  959. ret = obj_reloc_overflow;
  960. }
  961. *(short *)loc = v;
  962. break;
  963. case R_NIOS2_BFD_RELOC_8:
  964. if (v > 0xff) {
  965. ret = obj_reloc_overflow;
  966. }
  967. *(char *)loc = v;
  968. break;
  969. case R_NIOS2_S16:
  970. {
  971. Elf32_Addr word;
  972. if ((Elf32_Sword)v > 0x7fff
  973. || (Elf32_Sword)v < -(Elf32_Sword)0x8000
  974. ) {
  975. ret = obj_reloc_overflow;
  976. }
  977. word = *loc;
  978. *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
  979. (word & 0x3f);
  980. }
  981. break;
  982. case R_NIOS2_U16:
  983. {
  984. Elf32_Addr word;
  985. if (v > 0xffff) {
  986. ret = obj_reloc_overflow;
  987. }
  988. word = *loc;
  989. *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
  990. (word & 0x3f);
  991. }
  992. break;
  993. case R_NIOS2_PCREL16:
  994. {
  995. Elf32_Addr word;
  996. v -= dot + 4;
  997. if ((Elf32_Sword)v > 0x7fff
  998. || (Elf32_Sword)v < -(Elf32_Sword)0x8000
  999. ) {
  1000. ret = obj_reloc_overflow;
  1001. }
  1002. word = *loc;
  1003. *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
  1004. }
  1005. break;
  1006. case R_NIOS2_GPREL:
  1007. {
  1008. Elf32_Addr word, gp;
  1009. /* get _gp */
  1010. gp = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "_gp"));
  1011. v -= gp;
  1012. if ((Elf32_Sword)v > 0x7fff
  1013. || (Elf32_Sword)v < -(Elf32_Sword)0x8000
  1014. ) {
  1015. ret = obj_reloc_overflow;
  1016. }
  1017. word = *loc;
  1018. *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) | (word & 0x3f);
  1019. }
  1020. break;
  1021. case R_NIOS2_CALL26:
  1022. if (v & 3)
  1023. ret = obj_reloc_dangerous;
  1024. if ((v >> 28) != (dot >> 28))
  1025. ret = obj_reloc_overflow;
  1026. *loc = (*loc & 0x3f) | ((v >> 2) << 6);
  1027. break;
  1028. case R_NIOS2_IMM5:
  1029. {
  1030. Elf32_Addr word;
  1031. if (v > 0x1f) {
  1032. ret = obj_reloc_overflow;
  1033. }
  1034. word = *loc & ~0x7c0;
  1035. *loc = word | ((v & 0x1f) << 6);
  1036. }
  1037. break;
  1038. case R_NIOS2_IMM6:
  1039. {
  1040. Elf32_Addr word;
  1041. if (v > 0x3f) {
  1042. ret = obj_reloc_overflow;
  1043. }
  1044. word = *loc & ~0xfc0;
  1045. *loc = word | ((v & 0x3f) << 6);
  1046. }
  1047. break;
  1048. case R_NIOS2_IMM8:
  1049. {
  1050. Elf32_Addr word;
  1051. if (v > 0xff) {
  1052. ret = obj_reloc_overflow;
  1053. }
  1054. word = *loc & ~0x3fc0;
  1055. *loc = word | ((v & 0xff) << 6);
  1056. }
  1057. break;
  1058. case R_NIOS2_HI16:
  1059. {
  1060. Elf32_Addr word;
  1061. word = *loc;
  1062. *loc = ((((word >> 22) << 16) | ((v >>16) & 0xffff)) << 6) |
  1063. (word & 0x3f);
  1064. }
  1065. break;
  1066. case R_NIOS2_LO16:
  1067. {
  1068. Elf32_Addr word;
  1069. word = *loc;
  1070. *loc = ((((word >> 22) << 16) | (v & 0xffff)) << 6) |
  1071. (word & 0x3f);
  1072. }
  1073. break;
  1074. case R_NIOS2_HIADJ16:
  1075. {
  1076. Elf32_Addr word1, word2;
  1077. word1 = *loc;
  1078. word2 = ((v >> 16) + ((v >> 15) & 1)) & 0xffff;
  1079. *loc = ((((word1 >> 22) << 16) | word2) << 6) |
  1080. (word1 & 0x3f);
  1081. }
  1082. break;
  1083. #elif defined(__powerpc64__)
  1084. /* PPC64 needs a 2.6 kernel, 2.4 module relocation irrelevant */
  1085. #elif defined(__powerpc__)
  1086. case R_PPC_ADDR16_HA:
  1087. *(unsigned short *)loc = (v + 0x8000) >> 16;
  1088. break;
  1089. case R_PPC_ADDR16_HI:
  1090. *(unsigned short *)loc = v >> 16;
  1091. break;
  1092. case R_PPC_ADDR16_LO:
  1093. *(unsigned short *)loc = v;
  1094. break;
  1095. case R_PPC_REL24:
  1096. goto bb_use_plt;
  1097. case R_PPC_REL32:
  1098. *loc = v - dot;
  1099. break;
  1100. case R_PPC_ADDR32:
  1101. *loc = v;
  1102. break;
  1103. #elif defined(__s390__)
  1104. case R_390_32:
  1105. *(unsigned int *) loc += v;
  1106. break;
  1107. case R_390_16:
  1108. *(unsigned short *) loc += v;
  1109. break;
  1110. case R_390_8:
  1111. *(unsigned char *) loc += v;
  1112. break;
  1113. case R_390_PC32:
  1114. *(unsigned int *) loc += v - dot;
  1115. break;
  1116. case R_390_PC16DBL:
  1117. *(unsigned short *) loc += (v - dot) >> 1;
  1118. break;
  1119. case R_390_PC16:
  1120. *(unsigned short *) loc += v - dot;
  1121. break;
  1122. case R_390_PLT32:
  1123. case R_390_PLT16DBL:
  1124. /* find the plt entry and initialize it. */
  1125. pe = (struct arch_single_entry *) &isym->pltent;
  1126. if (pe->inited == 0) {
  1127. ip = (unsigned long *)(ifile->plt->contents + pe->offset);
  1128. ip[0] = 0x0d105810; /* basr 1,0; lg 1,10(1); br 1 */
  1129. ip[1] = 0x100607f1;
  1130. if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
  1131. ip[2] = v - 2;
  1132. else
  1133. ip[2] = v;
  1134. pe->inited = 1;
  1135. }
  1136. /* Insert relative distance to target. */
  1137. v = plt + pe->offset - dot;
  1138. if (ELF_R_TYPE(rel->r_info) == R_390_PLT32)
  1139. *(unsigned int *) loc = (unsigned int) v;
  1140. else if (ELF_R_TYPE(rel->r_info) == R_390_PLT16DBL)
  1141. *(unsigned short *) loc = (unsigned short) ((v + 2) >> 1);
  1142. break;
  1143. case R_390_GLOB_DAT:
  1144. case R_390_JMP_SLOT:
  1145. *loc = v;
  1146. break;
  1147. case R_390_RELATIVE:
  1148. *loc += f->baseaddr;
  1149. break;
  1150. case R_390_GOTPC:
  1151. *(unsigned long *) loc += got - dot;
  1152. break;
  1153. case R_390_GOT12:
  1154. case R_390_GOT16:
  1155. case R_390_GOT32:
  1156. if (!isym->gotent.inited)
  1157. {
  1158. isym->gotent.inited = 1;
  1159. *(ElfW(Addr) *)(ifile->got->contents + isym->gotent.offset) = v;
  1160. }
  1161. if (ELF_R_TYPE(rel->r_info) == R_390_GOT12)
  1162. *(unsigned short *) loc |= (*(unsigned short *) loc + isym->gotent.offset) & 0xfff;
  1163. else if (ELF_R_TYPE(rel->r_info) == R_390_GOT16)
  1164. *(unsigned short *) loc += isym->gotent.offset;
  1165. else if (ELF_R_TYPE(rel->r_info) == R_390_GOT32)
  1166. *(unsigned int *) loc += isym->gotent.offset;
  1167. break;
  1168. # ifndef R_390_GOTOFF32
  1169. # define R_390_GOTOFF32 R_390_GOTOFF
  1170. # endif
  1171. case R_390_GOTOFF32:
  1172. *loc += v - got;
  1173. break;
  1174. #elif defined(__sh__)
  1175. case R_SH_NONE:
  1176. break;
  1177. case R_SH_DIR32:
  1178. *loc += v;
  1179. break;
  1180. case R_SH_REL32:
  1181. *loc += v - dot;
  1182. break;
  1183. case R_SH_PLT32:
  1184. *loc = v - dot;
  1185. break;
  1186. case R_SH_GLOB_DAT:
  1187. case R_SH_JMP_SLOT:
  1188. *loc = v;
  1189. break;
  1190. case R_SH_RELATIVE:
  1191. *loc = f->baseaddr + rel->r_addend;
  1192. break;
  1193. case R_SH_GOTPC:
  1194. *loc = got - dot + rel->r_addend;
  1195. break;
  1196. case R_SH_GOT32:
  1197. goto bb_use_got;
  1198. case R_SH_GOTOFF:
  1199. *loc = v - got;
  1200. break;
  1201. # if defined(__SH5__)
  1202. case R_SH_IMM_MEDLOW16:
  1203. case R_SH_IMM_LOW16:
  1204. {
  1205. ElfW(Addr) word;
  1206. if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16)
  1207. v >>= 16;
  1208. /*
  1209. * movi and shori have the format:
  1210. *
  1211. * | op | imm | reg | reserved |
  1212. * 31..26 25..10 9.. 4 3 .. 0
  1213. *
  1214. * so we simply mask and or in imm.
  1215. */
  1216. word = *loc & ~0x3fffc00;
  1217. word |= (v & 0xffff) << 10;
  1218. *loc = word;
  1219. break;
  1220. }
  1221. case R_SH_IMM_MEDLOW16_PCREL:
  1222. case R_SH_IMM_LOW16_PCREL:
  1223. {
  1224. ElfW(Addr) word;
  1225. word = *loc & ~0x3fffc00;
  1226. v -= dot;
  1227. if (ELF_R_TYPE(rel->r_info) == R_SH_IMM_MEDLOW16_PCREL)
  1228. v >>= 16;
  1229. word |= (v & 0xffff) << 10;
  1230. *loc = word;
  1231. break;
  1232. }
  1233. # endif /* __SH5__ */
  1234. #elif defined(__v850e__)
  1235. case R_V850_NONE:
  1236. break;
  1237. case R_V850_32:
  1238. /* We write two shorts instead of a long because even
  1239. 32-bit insns only need half-word alignment, but
  1240. 32-bit data needs to be long-word aligned. */
  1241. v += ((unsigned short *)loc)[0];
  1242. v += ((unsigned short *)loc)[1] << 16;
  1243. ((unsigned short *)loc)[0] = v & 0xffff;
  1244. ((unsigned short *)loc)[1] = (v >> 16) & 0xffff;
  1245. break;
  1246. case R_V850_22_PCREL:
  1247. goto bb_use_plt;
  1248. #elif defined(__x86_64__)
  1249. case R_X86_64_NONE:
  1250. break;
  1251. case R_X86_64_64:
  1252. *loc += v;
  1253. break;
  1254. case R_X86_64_32:
  1255. *(unsigned int *) loc += v;
  1256. if (v > 0xffffffff)
  1257. {
  1258. ret = obj_reloc_overflow; /* Kernel module compiled without -mcmodel=kernel. */
  1259. /* error("Possibly is module compiled without -mcmodel=kernel!"); */
  1260. }
  1261. break;
  1262. case R_X86_64_32S:
  1263. *(signed int *) loc += v;
  1264. break;
  1265. case R_X86_64_16:
  1266. *(unsigned short *) loc += v;
  1267. break;
  1268. case R_X86_64_8:
  1269. *(unsigned char *) loc += v;
  1270. break;
  1271. case R_X86_64_PC32:
  1272. *(unsigned int *) loc += v - dot;
  1273. break;
  1274. case R_X86_64_PC16:
  1275. *(unsigned short *) loc += v - dot;
  1276. break;
  1277. case R_X86_64_PC8:
  1278. *(unsigned char *) loc += v - dot;
  1279. break;
  1280. case R_X86_64_GLOB_DAT:
  1281. case R_X86_64_JUMP_SLOT:
  1282. *loc = v;
  1283. break;
  1284. case R_X86_64_RELATIVE:
  1285. *loc += f->baseaddr;
  1286. break;
  1287. case R_X86_64_GOT32:
  1288. case R_X86_64_GOTPCREL:
  1289. goto bb_use_got;
  1290. # if 0
  1291. if (!isym->gotent.reloc_done)
  1292. {
  1293. isym->gotent.reloc_done = 1;
  1294. *(Elf64_Addr *)(ifile->got->contents + isym->gotent.offset) = v;
  1295. }
  1296. /* XXX are these really correct? */
  1297. if (ELF64_R_TYPE(rel->r_info) == R_X86_64_GOTPCREL)
  1298. *(unsigned int *) loc += v + isym->gotent.offset;
  1299. else
  1300. *loc += isym->gotent.offset;
  1301. break;
  1302. # endif
  1303. #else
  1304. # warning "no idea how to handle relocations on your arch"
  1305. #endif
  1306. default:
  1307. printf("Warning: unhandled reloc %d\n", (int)ELF_R_TYPE(rel->r_info));
  1308. ret = obj_reloc_unhandled;
  1309. break;
  1310. #if defined(USE_PLT_ENTRIES)
  1311. bb_use_plt:
  1312. /* find the plt entry and initialize it if necessary */
  1313. #if defined(USE_PLT_LIST)
  1314. for (pe = isym->pltent; pe != NULL && pe->addend != rel->r_addend;)
  1315. pe = pe->next;
  1316. #else
  1317. pe = &isym->pltent;
  1318. #endif
  1319. if (! pe->inited) {
  1320. ip = (unsigned long *) (ifile->plt->contents + pe->offset);
  1321. /* generate some machine code */
  1322. #if defined(__arm__)
  1323. ip[0] = 0xe51ff004; /* ldr pc,[pc,#-4] */
  1324. ip[1] = v; /* sym@ */
  1325. #endif
  1326. #if defined(__powerpc__)
  1327. ip[0] = 0x3d600000 + ((v + 0x8000) >> 16); /* lis r11,sym@ha */
  1328. ip[1] = 0x396b0000 + (v & 0xffff); /* addi r11,r11,sym@l */
  1329. ip[2] = 0x7d6903a6; /* mtctr r11 */
  1330. ip[3] = 0x4e800420; /* bctr */
  1331. #endif
  1332. #if defined(__v850e__)
  1333. /* We have to trash a register, so we assume that any control
  1334. transfer more than 21-bits away must be a function call
  1335. (so we can use a call-clobbered register). */
  1336. ip[0] = 0x0621 + ((v & 0xffff) << 16); /* mov sym, r1 ... */
  1337. ip[1] = ((v >> 16) & 0xffff) + 0x610000; /* ...; jmp r1 */
  1338. #endif
  1339. pe->inited = 1;
  1340. }
  1341. /* relative distance to target */
  1342. v -= dot;
  1343. /* if the target is too far away.... */
  1344. #if defined(__arm__) || defined(__powerpc__)
  1345. if ((int)v < -0x02000000 || (int)v >= 0x02000000)
  1346. #elif defined(__v850e__)
  1347. if ((ElfW(Sword))v > 0x1fffff || (ElfW(Sword))v < (ElfW(Sword))-0x200000)
  1348. #endif
  1349. /* go via the plt */
  1350. v = plt + pe->offset - dot;
  1351. #if defined(__v850e__)
  1352. if (v & 1)
  1353. #else
  1354. if (v & 3)
  1355. #endif
  1356. ret = obj_reloc_dangerous;
  1357. /* merge the offset into the instruction. */
  1358. #if defined(__arm__)
  1359. /* Convert to words. */
  1360. v >>= 2;
  1361. *loc = (*loc & ~0x00ffffff) | ((v + *loc) & 0x00ffffff);
  1362. #endif
  1363. #if defined(__powerpc__)
  1364. *loc = (*loc & ~0x03fffffc) | (v & 0x03fffffc);
  1365. #endif
  1366. #if defined(__v850e__)
  1367. /* We write two shorts instead of a long because even 32-bit insns
  1368. only need half-word alignment, but the 32-bit data write needs
  1369. to be long-word aligned. */
  1370. ((unsigned short *)loc)[0] =
  1371. (*(unsigned short *)loc & 0xffc0) /* opcode + reg */
  1372. | ((v >> 16) & 0x3f); /* offs high part */
  1373. ((unsigned short *)loc)[1] =
  1374. (v & 0xffff); /* offs low part */
  1375. #endif
  1376. break;
  1377. #endif /* USE_PLT_ENTRIES */
  1378. #if defined(USE_GOT_ENTRIES)
  1379. bb_use_got:
  1380. /* needs an entry in the .got: set it, once */
  1381. if (!isym->gotent.inited) {
  1382. isym->gotent.inited = 1;
  1383. *(ElfW(Addr) *) (ifile->got->contents + isym->gotent.offset) = v;
  1384. }
  1385. /* make the reloc with_respect_to_.got */
  1386. #if defined(__sh__)
  1387. *loc += isym->gotent.offset + rel->r_addend;
  1388. #elif defined(__i386__) || defined(__arm__) || defined(__mc68000__)
  1389. *loc += isym->gotent.offset;
  1390. #endif
  1391. break;
  1392. #endif /* USE_GOT_ENTRIES */
  1393. }
  1394. return ret;
  1395. }
  1396. #if defined(USE_LIST)
  1397. static int arch_list_add(ElfW(RelM) *rel, struct arch_list_entry **list,
  1398. int offset, int size)
  1399. {
  1400. struct arch_list_entry *pe;
  1401. for (pe = *list; pe != NULL; pe = pe->next) {
  1402. if (pe->addend == rel->r_addend) {
  1403. break;
  1404. }
  1405. }
  1406. if (pe == NULL) {
  1407. pe = xzalloc(sizeof(struct arch_list_entry));
  1408. pe->next = *list;
  1409. pe->addend = rel->r_addend;
  1410. pe->offset = offset;
  1411. /*pe->inited = 0;*/
  1412. *list = pe;
  1413. return size;
  1414. }
  1415. return 0;
  1416. }
  1417. #endif
  1418. #if defined(USE_SINGLE)
  1419. static int arch_single_init(/*ElfW(RelM) *rel,*/ struct arch_single_entry *single,
  1420. int offset, int size)
  1421. {
  1422. if (single->allocated == 0) {
  1423. single->allocated = 1;
  1424. single->offset = offset;
  1425. single->inited = 0;
  1426. return size;
  1427. }
  1428. return 0;
  1429. }
  1430. #endif
  1431. #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
  1432. static struct obj_section *arch_xsect_init(struct obj_file *f, const char *name,
  1433. int offset, int size)
  1434. {
  1435. struct obj_section *myrelsec = obj_find_section(f, name);
  1436. if (offset == 0) {
  1437. offset += size;
  1438. }
  1439. if (myrelsec) {
  1440. obj_extend_section(myrelsec, offset);
  1441. } else {
  1442. myrelsec = obj_create_alloced_section(f, name,
  1443. size, offset);
  1444. }
  1445. return myrelsec;
  1446. }
  1447. #endif
  1448. static void arch_create_got(struct obj_file *f)
  1449. {
  1450. #if defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES)
  1451. struct arch_file *ifile = (struct arch_file *) f;
  1452. int i;
  1453. #if defined(USE_GOT_ENTRIES)
  1454. int got_offset = 0, got_needed = 0, got_allocate;
  1455. #endif
  1456. #if defined(USE_PLT_ENTRIES)
  1457. int plt_offset = 0, plt_needed = 0, plt_allocate;
  1458. #endif
  1459. struct obj_section *relsec, *symsec, *strsec;
  1460. ElfW(RelM) *rel, *relend;
  1461. ElfW(Sym) *symtab, *extsym;
  1462. const char *strtab, *name;
  1463. struct arch_symbol *intsym;
  1464. for (i = 0; i < f->header.e_shnum; ++i) {
  1465. relsec = f->sections[i];
  1466. if (relsec->header.sh_type != SHT_RELM)
  1467. continue;
  1468. symsec = f->sections[relsec->header.sh_link];
  1469. strsec = f->sections[symsec->header.sh_link];
  1470. rel = (ElfW(RelM) *) relsec->contents;
  1471. relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
  1472. symtab = (ElfW(Sym) *) symsec->contents;
  1473. strtab = (const char *) strsec->contents;
  1474. for (; rel < relend; ++rel) {
  1475. extsym = &symtab[ELF_R_SYM(rel->r_info)];
  1476. #if defined(USE_GOT_ENTRIES)
  1477. got_allocate = 0;
  1478. #endif
  1479. #if defined(USE_PLT_ENTRIES)
  1480. plt_allocate = 0;
  1481. #endif
  1482. switch (ELF_R_TYPE(rel->r_info)) {
  1483. #if defined(__arm__)
  1484. case R_ARM_PC24:
  1485. case R_ARM_PLT32:
  1486. plt_allocate = 1;
  1487. break;
  1488. case R_ARM_GOTOFF:
  1489. case R_ARM_GOTPC:
  1490. got_needed = 1;
  1491. continue;
  1492. case R_ARM_GOT32:
  1493. got_allocate = 1;
  1494. break;
  1495. #elif defined(__i386__)
  1496. case R_386_GOTPC:
  1497. case R_386_GOTOFF:
  1498. got_needed = 1;
  1499. continue;
  1500. case R_386_GOT32:
  1501. got_allocate = 1;
  1502. break;
  1503. #elif defined(__powerpc__)
  1504. case R_PPC_REL24:
  1505. plt_allocate = 1;
  1506. break;
  1507. #elif defined(__mc68000__)
  1508. case R_68K_GOT32:
  1509. got_allocate = 1;
  1510. break;
  1511. #ifdef R_68K_GOTOFF
  1512. case R_68K_GOTOFF:
  1513. got_needed = 1;
  1514. continue;
  1515. #endif
  1516. #elif defined(__sh__)
  1517. case R_SH_GOT32:
  1518. got_allocate = 1;
  1519. break;
  1520. case R_SH_GOTPC:
  1521. case R_SH_GOTOFF:
  1522. got_needed = 1;
  1523. continue;
  1524. #elif defined(__v850e__)
  1525. case R_V850_22_PCREL:
  1526. plt_needed = 1;
  1527. break;
  1528. #endif
  1529. default:
  1530. continue;
  1531. }
  1532. if (extsym->st_name != 0) {
  1533. name = strtab + extsym->st_name;
  1534. } else {
  1535. name = f->sections[extsym->st_shndx]->name;
  1536. }
  1537. intsym = (struct arch_symbol *) obj_find_symbol(f, name);
  1538. #if defined(USE_GOT_ENTRIES)
  1539. if (got_allocate) {
  1540. got_offset += arch_single_init(
  1541. /*rel,*/ &intsym->gotent,
  1542. got_offset, GOT_ENTRY_SIZE);
  1543. got_needed = 1;
  1544. }
  1545. #endif
  1546. #if defined(USE_PLT_ENTRIES)
  1547. if (plt_allocate) {
  1548. #if defined(USE_PLT_LIST)
  1549. plt_offset += arch_list_add(
  1550. rel, &intsym->pltent,
  1551. plt_offset, PLT_ENTRY_SIZE);
  1552. #else
  1553. plt_offset += arch_single_init(
  1554. /*rel,*/ &intsym->pltent,
  1555. plt_offset, PLT_ENTRY_SIZE);
  1556. #endif
  1557. plt_needed = 1;
  1558. }
  1559. #endif
  1560. }
  1561. }
  1562. #if defined(USE_GOT_ENTRIES)
  1563. if (got_needed) {
  1564. ifile->got = arch_xsect_init(f, ".got", got_offset,
  1565. GOT_ENTRY_SIZE);
  1566. }
  1567. #endif
  1568. #if defined(USE_PLT_ENTRIES)
  1569. if (plt_needed) {
  1570. ifile->plt = arch_xsect_init(f, ".plt", plt_offset,
  1571. PLT_ENTRY_SIZE);
  1572. }
  1573. #endif
  1574. #endif /* defined(USE_GOT_ENTRIES) || defined(USE_PLT_ENTRIES) */
  1575. }
  1576. /*======================================================================*/
  1577. /* Standard ELF hash function. */
  1578. static unsigned long obj_elf_hash_n(const char *name, unsigned long n)
  1579. {
  1580. unsigned long h = 0;
  1581. unsigned long g;
  1582. unsigned char ch;
  1583. while (n > 0) {
  1584. ch = *name++;
  1585. h = (h << 4) + ch;
  1586. g = (h & 0xf0000000);
  1587. if (g != 0) {
  1588. h ^= g >> 24;
  1589. h &= ~g;
  1590. }
  1591. n--;
  1592. }
  1593. return h;
  1594. }
  1595. static unsigned long FAST_FUNC obj_elf_hash(const char *name)
  1596. {
  1597. return obj_elf_hash_n(name, strlen(name));
  1598. }
  1599. #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
  1600. /* String comparison for non-co-versioned kernel and module. */
  1601. static int ncv_strcmp(const char *a, const char *b)
  1602. {
  1603. size_t alen = strlen(a), blen = strlen(b);
  1604. if (blen == alen + 10 && b[alen] == '_' && b[alen + 1] == 'R')
  1605. return strncmp(a, b, alen);
  1606. else if (alen == blen + 10 && a[blen] == '_' && a[blen + 1] == 'R')
  1607. return strncmp(a, b, blen);
  1608. else
  1609. return strcmp(a, b);
  1610. }
  1611. /* String hashing for non-co-versioned kernel and module. Here
  1612. we are simply forced to drop the crc from the hash. */
  1613. static unsigned long FAST_FUNC ncv_symbol_hash(const char *str)
  1614. {
  1615. size_t len = strlen(str);
  1616. if (len > 10 && str[len - 10] == '_' && str[len - 9] == 'R')
  1617. len -= 10;
  1618. return obj_elf_hash_n(str, len);
  1619. }
  1620. static void
  1621. obj_set_symbol_compare(struct obj_file *f,
  1622. int (*cmp) (const char *, const char *),
  1623. unsigned long (*hash) (const char *) FAST_FUNC)
  1624. {
  1625. if (cmp)
  1626. f->symbol_cmp = cmp;
  1627. if (hash) {
  1628. struct obj_symbol *tmptab[HASH_BUCKETS], *sym, *next;
  1629. int i;
  1630. f->symbol_hash = hash;
  1631. memcpy(tmptab, f->symtab, sizeof(tmptab));
  1632. memset(f->symtab, 0, sizeof(f->symtab));
  1633. for (i = 0; i < HASH_BUCKETS; ++i) {
  1634. for (sym = tmptab[i]; sym; sym = next) {
  1635. unsigned long h = hash(sym->name) % HASH_BUCKETS;
  1636. next = sym->next;
  1637. sym->next = f->symtab[h];
  1638. f->symtab[h] = sym;
  1639. }
  1640. }
  1641. }
  1642. }
  1643. #endif /* FEATURE_INSMOD_VERSION_CHECKING */
  1644. static struct obj_symbol *
  1645. obj_add_symbol(struct obj_file *f, const char *name,
  1646. unsigned long symidx, int info,
  1647. int secidx, ElfW(Addr) value,
  1648. unsigned long size)
  1649. {
  1650. struct obj_symbol *sym;
  1651. unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
  1652. int n_type = ELF_ST_TYPE(info);
  1653. int n_binding = ELF_ST_BIND(info);
  1654. for (sym = f->symtab[hash]; sym; sym = sym->next) {
  1655. if (f->symbol_cmp(sym->name, name) == 0) {
  1656. int o_secidx = sym->secidx;
  1657. int o_info = sym->info;
  1658. int o_type = ELF_ST_TYPE(o_info);
  1659. int o_binding = ELF_ST_BIND(o_info);
  1660. /* A redefinition! Is it legal? */
  1661. if (secidx == SHN_UNDEF)
  1662. return sym;
  1663. else if (o_secidx == SHN_UNDEF)
  1664. goto found;
  1665. else if (n_binding == STB_GLOBAL && o_binding == STB_LOCAL) {
  1666. /* Cope with local and global symbols of the same name
  1667. in the same object file, as might have been created
  1668. by ld -r. The only reason locals are now seen at this
  1669. level at all is so that we can do semi-sensible things
  1670. with parameters. */
  1671. struct obj_symbol *nsym, **p;
  1672. nsym = arch_new_symbol();
  1673. nsym->next = sym->next;
  1674. nsym->ksymidx = -1;
  1675. /* Excise the old (local) symbol from the hash chain. */
  1676. for (p = &f->symtab[hash]; *p != sym; p = &(*p)->next)
  1677. continue;
  1678. *p = sym = nsym;
  1679. goto found;
  1680. } else if (n_binding == STB_LOCAL) {
  1681. /* Another symbol of the same name has already been defined.
  1682. Just add this to the local table. */
  1683. sym = arch_new_symbol();
  1684. sym->next = NULL;
  1685. sym->ksymidx = -1;
  1686. f->local_symtab[symidx] = sym;
  1687. goto found;
  1688. } else if (n_binding == STB_WEAK)
  1689. return sym;
  1690. else if (o_binding == STB_WEAK)
  1691. goto found;
  1692. /* Don't unify COMMON symbols with object types the programmer
  1693. doesn't expect. */
  1694. else if (secidx == SHN_COMMON
  1695. && (o_type == STT_NOTYPE || o_type == STT_OBJECT))
  1696. return sym;
  1697. else if (o_secidx == SHN_COMMON
  1698. && (n_type == STT_NOTYPE || n_type == STT_OBJECT))
  1699. goto found;
  1700. else {
  1701. /* Don't report an error if the symbol is coming from
  1702. the kernel or some external module. */
  1703. if (secidx <= SHN_HIRESERVE)
  1704. bb_error_msg("%s multiply defined", name);
  1705. return sym;
  1706. }
  1707. }
  1708. }
  1709. /* Completely new symbol. */
  1710. sym = arch_new_symbol();
  1711. sym->next = f->symtab[hash];
  1712. f->symtab[hash] = sym;
  1713. sym->ksymidx = -1;
  1714. if (ELF_ST_BIND(info) == STB_LOCAL && symidx != (unsigned long)(-1)) {
  1715. if (symidx >= f->local_symtab_size)
  1716. bb_error_msg("local symbol %s with index %ld exceeds local_symtab_size %ld",
  1717. name, (long) symidx, (long) f->local_symtab_size);
  1718. else
  1719. f->local_symtab[symidx] = sym;
  1720. }
  1721. found:
  1722. sym->name = name;
  1723. sym->value = value;
  1724. sym->size = size;
  1725. sym->secidx = secidx;
  1726. sym->info = info;
  1727. return sym;
  1728. }
  1729. static struct obj_symbol *
  1730. obj_find_symbol(struct obj_file *f, const char *name)
  1731. {
  1732. struct obj_symbol *sym;
  1733. unsigned long hash = f->symbol_hash(name) % HASH_BUCKETS;
  1734. for (sym = f->symtab[hash]; sym; sym = sym->next)
  1735. if (f->symbol_cmp(sym->name, name) == 0)
  1736. return sym;
  1737. return NULL;
  1738. }
  1739. static ElfW(Addr) obj_symbol_final_value(struct obj_file * f, struct obj_symbol * sym)
  1740. {
  1741. if (sym) {
  1742. if (sym->secidx >= SHN_LORESERVE)
  1743. return sym->value;
  1744. return sym->value + f->sections[sym->secidx]->header.sh_addr;
  1745. }
  1746. /* As a special case, a NULL sym has value zero. */
  1747. return 0;
  1748. }
  1749. static struct obj_section *obj_find_section(struct obj_file *f, const char *name)
  1750. {
  1751. int i, n = f->header.e_shnum;
  1752. for (i = 0; i < n; ++i)
  1753. if (strcmp(f->sections[i]->name, name) == 0)
  1754. return f->sections[i];
  1755. return NULL;
  1756. }
  1757. static int obj_load_order_prio(struct obj_section *a)
  1758. {
  1759. unsigned long af, ac;
  1760. af = a->header.sh_flags;
  1761. ac = 0;
  1762. if (a->name[0] != '.' || strlen(a->name) != 10
  1763. || strcmp(a->name + 5, ".init") != 0
  1764. ) {
  1765. ac |= 32;
  1766. }
  1767. if (af & SHF_ALLOC)
  1768. ac |= 16;
  1769. if (!(af & SHF_WRITE))
  1770. ac |= 8;
  1771. if (af & SHF_EXECINSTR)
  1772. ac |= 4;
  1773. if (a->header.sh_type != SHT_NOBITS)
  1774. ac |= 2;
  1775. return ac;
  1776. }
  1777. static void
  1778. obj_insert_section_load_order(struct obj_file *f, struct obj_section *sec)
  1779. {
  1780. struct obj_section **p;
  1781. int prio = obj_load_order_prio(sec);
  1782. for (p = f->load_order_search_start; *p; p = &(*p)->load_next)
  1783. if (obj_load_order_prio(*p) < prio)
  1784. break;
  1785. sec->load_next = *p;
  1786. *p = sec;
  1787. }
  1788. static struct obj_section *helper_create_alloced_section(struct obj_file *f,
  1789. const char *name,
  1790. unsigned long align,
  1791. unsigned long size)
  1792. {
  1793. int newidx = f->header.e_shnum++;
  1794. struct obj_section *sec;
  1795. f->sections = xrealloc_vector(f->sections, 2, newidx);
  1796. f->sections[newidx] = sec = arch_new_section();
  1797. sec->header.sh_type = SHT_PROGBITS;
  1798. sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
  1799. sec->header.sh_size = size;
  1800. sec->header.sh_addralign = align;
  1801. sec->name = name;
  1802. sec->idx = newidx;
  1803. if (size)
  1804. sec->contents = xzalloc(size);
  1805. return sec;
  1806. }
  1807. static struct obj_section *obj_create_alloced_section(struct obj_file *f,
  1808. const char *name,
  1809. unsigned long align,
  1810. unsigned long size)
  1811. {
  1812. struct obj_section *sec;
  1813. sec = helper_create_alloced_section(f, name, align, size);
  1814. obj_insert_section_load_order(f, sec);
  1815. return sec;
  1816. }
  1817. static struct obj_section *obj_create_alloced_section_first(struct obj_file *f,
  1818. const char *name,
  1819. unsigned long align,
  1820. unsigned long size)
  1821. {
  1822. struct obj_section *sec;
  1823. sec = helper_create_alloced_section(f, name, align, size);
  1824. sec->load_next = f->load_order;
  1825. f->load_order = sec;
  1826. if (f->load_order_search_start == &f->load_order)
  1827. f->load_order_search_start = &sec->load_next;
  1828. return sec;
  1829. }
  1830. static void *obj_extend_section(struct obj_section *sec, unsigned long more)
  1831. {
  1832. unsigned long oldsize = sec->header.sh_size;
  1833. if (more) {
  1834. sec->header.sh_size += more;
  1835. sec->contents = xrealloc(sec->contents, sec->header.sh_size);
  1836. }
  1837. return sec->contents + oldsize;
  1838. }
  1839. /* Conditionally add the symbols from the given symbol set to the
  1840. new module. */
  1841. static int add_symbols_from(struct obj_file *f,
  1842. int idx,
  1843. struct new_module_symbol *syms,
  1844. size_t nsyms)
  1845. {
  1846. struct new_module_symbol *s;
  1847. size_t i;
  1848. int used = 0;
  1849. #ifdef SYMBOL_PREFIX
  1850. char *name_buf = NULL;
  1851. size_t name_alloced_size = 0;
  1852. #endif
  1853. #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
  1854. int gpl;
  1855. gpl = obj_gpl_license(f, NULL) == 0;
  1856. #endif
  1857. for (i = 0, s = syms; i < nsyms; ++i, ++s) {
  1858. /* Only add symbols that are already marked external.
  1859. If we override locals we may cause problems for
  1860. argument initialization. We will also create a false
  1861. dependency on the module. */
  1862. struct obj_symbol *sym;
  1863. char *name;
  1864. /* GPL licensed modules can use symbols exported with
  1865. * EXPORT_SYMBOL_GPL, so ignore any GPLONLY_ prefix on the
  1866. * exported names. Non-GPL modules never see any GPLONLY_
  1867. * symbols so they cannot fudge it by adding the prefix on
  1868. * their references.
  1869. */
  1870. if (is_prefixed_with((char *)s->name, "GPLONLY_")) {
  1871. #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
  1872. if (gpl)
  1873. s->name += 8;
  1874. else
  1875. #endif
  1876. continue;
  1877. }
  1878. name = (char *)s->name;
  1879. #ifdef SYMBOL_PREFIX
  1880. /* Prepend SYMBOL_PREFIX to the symbol's name (the
  1881. kernel exports `C names', but module object files
  1882. reference `linker names'). */
  1883. size_t extra = sizeof SYMBOL_PREFIX;
  1884. size_t name_size = strlen(name) + extra;
  1885. if (name_size > name_alloced_size) {
  1886. name_alloced_size = name_size * 2;
  1887. name_buf = alloca(name_alloced_size);
  1888. }
  1889. strcpy(name_buf, SYMBOL_PREFIX);
  1890. strcpy(name_buf + extra - 1, name);
  1891. name = name_buf;
  1892. #endif
  1893. sym = obj_find_symbol(f, name);
  1894. if (sym && !(ELF_ST_BIND(sym->info) == STB_LOCAL)) {
  1895. #ifdef SYMBOL_PREFIX
  1896. /* Put NAME_BUF into more permanent storage. */
  1897. name = xmalloc(name_size);
  1898. strcpy(name, name_buf);
  1899. #endif
  1900. sym = obj_add_symbol(f, name, -1,
  1901. ELF_ST_INFO(STB_GLOBAL,
  1902. STT_NOTYPE),
  1903. idx, s->value, 0);
  1904. /* Did our symbol just get installed? If so, mark the
  1905. module as "used". */
  1906. if (sym->secidx == idx)
  1907. used = 1;
  1908. }
  1909. }
  1910. return used;
  1911. }
  1912. static void add_kernel_symbols(struct obj_file *f)
  1913. {
  1914. struct external_module *m;
  1915. int i, nused = 0;
  1916. /* Add module symbols first. */
  1917. for (i = 0, m = ext_modules; i < n_ext_modules; ++i, ++m) {
  1918. if (m->nsyms
  1919. && add_symbols_from(f, SHN_HIRESERVE + 2 + i, m->syms, m->nsyms)
  1920. ) {
  1921. m->used = 1;
  1922. ++nused;
  1923. }
  1924. }
  1925. n_ext_modules_used = nused;
  1926. /* And finally the symbols from the kernel proper. */
  1927. if (nksyms)
  1928. add_symbols_from(f, SHN_HIRESERVE + 1, ksyms, nksyms);
  1929. }
  1930. static char *get_modinfo_value(struct obj_file *f, const char *key)
  1931. {
  1932. struct obj_section *sec;
  1933. char *p, *v, *n, *ep;
  1934. size_t klen = strlen(key);
  1935. sec = obj_find_section(f, ".modinfo");
  1936. if (sec == NULL)
  1937. return NULL;
  1938. p = sec->contents;
  1939. ep = p + sec->header.sh_size;
  1940. while (p < ep) {
  1941. v = strchr(p, '=');
  1942. n = strchr(p, '\0');
  1943. if (v) {
  1944. if (p + klen == v && strncmp(p, key, klen) == 0)
  1945. return v + 1;
  1946. } else {
  1947. if (p + klen == n && strcmp(p, key) == 0)
  1948. return n;
  1949. }
  1950. p = n + 1;
  1951. }
  1952. return NULL;
  1953. }
  1954. /*======================================================================*/
  1955. /* Functions relating to module loading after 2.1.18. */
  1956. /* From Linux-2.6 sources */
  1957. /* You can use " around spaces, but can't escape ". */
  1958. /* Hyphens and underscores equivalent in parameter names. */
  1959. static char *next_arg(char *args, char **param, char **val)
  1960. {
  1961. unsigned int i, equals = 0;
  1962. int in_quote = 0, quoted = 0;
  1963. char *next;
  1964. if (*args == '"') {
  1965. args++;
  1966. in_quote = 1;
  1967. quoted = 1;
  1968. }
  1969. for (i = 0; args[i]; i++) {
  1970. if (args[i] == ' ' && !in_quote)
  1971. break;
  1972. if (equals == 0) {
  1973. if (args[i] == '=')
  1974. equals = i;
  1975. }
  1976. if (args[i] == '"')
  1977. in_quote = !in_quote;
  1978. }
  1979. *param = args;
  1980. if (!equals)
  1981. *val = NULL;
  1982. else {
  1983. args[equals] = '\0';
  1984. *val = args + equals + 1;
  1985. /* Don't include quotes in value. */
  1986. if (**val == '"') {
  1987. (*val)++;
  1988. if (args[i-1] == '"')
  1989. args[i-1] = '\0';
  1990. }
  1991. if (quoted && args[i-1] == '"')
  1992. args[i-1] = '\0';
  1993. }
  1994. if (args[i]) {
  1995. args[i] = '\0';
  1996. next = args + i + 1;
  1997. } else
  1998. next = args + i;
  1999. /* Chew up trailing spaces. */
  2000. return skip_whitespace(next);
  2001. }
  2002. static void
  2003. new_process_module_arguments(struct obj_file *f, const char *options)
  2004. {
  2005. char *xoptions, *pos;
  2006. char *param, *val;
  2007. xoptions = pos = xstrdup(skip_whitespace(options));
  2008. while (*pos) {
  2009. unsigned long charssize = 0;
  2010. char *tmp, *contents, *loc, *pinfo, *p;
  2011. struct obj_symbol *sym;
  2012. int min, max, n, len;
  2013. pos = next_arg(pos, &param, &val);
  2014. tmp = xasprintf("parm_%s", param);
  2015. pinfo = get_modinfo_value(f, tmp);
  2016. free(tmp);
  2017. if (pinfo == NULL)
  2018. bb_error_msg_and_die("invalid parameter %s", param);
  2019. #ifdef SYMBOL_PREFIX
  2020. tmp = xasprintf(SYMBOL_PREFIX "%s", param);
  2021. sym = obj_find_symbol(f, tmp);
  2022. free(tmp);
  2023. #else
  2024. sym = obj_find_symbol(f, param);
  2025. #endif
  2026. /* Also check that the parameter was not resolved from the kernel. */
  2027. if (sym == NULL || sym->secidx > SHN_HIRESERVE)
  2028. bb_error_msg_and_die("symbol for parameter %s not found", param);
  2029. /* Number of parameters */
  2030. min = max = 1;
  2031. if (isdigit(*pinfo)) {
  2032. min = max = strtoul(pinfo, &pinfo, 10);
  2033. if (*pinfo == '-')
  2034. max = strtoul(pinfo + 1, &pinfo, 10);
  2035. }
  2036. contents = f->sections[sym->secidx]->contents;
  2037. loc = contents + sym->value;
  2038. if (*pinfo == 'c') {
  2039. if (!isdigit(pinfo[1])) {
  2040. bb_error_msg_and_die("parameter type 'c' for %s must be followed by"
  2041. " the maximum size", param);
  2042. }
  2043. charssize = strtoul(pinfo + 1, NULL, 10);
  2044. }
  2045. if (val == NULL) {
  2046. if (*pinfo != 'b')
  2047. bb_error_msg_and_die("argument expected for parameter %s", param);
  2048. val = (char *) "1";
  2049. }
  2050. /* Parse parameter values */
  2051. n = 0;
  2052. p = val;
  2053. while (*p) {
  2054. char sv_ch;
  2055. char *endp;
  2056. if (++n > max)
  2057. bb_error_msg_and_die("too many values for %s (max %d)", param, max);
  2058. switch (*pinfo) {
  2059. case 's':
  2060. len = strcspn(p, ",");
  2061. sv_ch = p[len];
  2062. p[len] = '\0';
  2063. obj_string_patch(f, sym->secidx,
  2064. loc - contents, p);
  2065. loc += tgt_sizeof_char_p;
  2066. p += len;
  2067. *p = sv_ch;
  2068. break;
  2069. case 'c':
  2070. len = strcspn(p, ",");
  2071. sv_ch = p[len];
  2072. p[len] = '\0';
  2073. if (len >= charssize)
  2074. bb_error_msg_and_die("string too long for %s (max %ld)", param,
  2075. charssize - 1);
  2076. strcpy((char *) loc, p);
  2077. loc += charssize;
  2078. p += len;
  2079. *p = sv_ch;
  2080. break;
  2081. case 'b':
  2082. *loc++ = strtoul(p, &endp, 0);
  2083. p = endp; /* gcc likes temp var for &endp */
  2084. break;
  2085. case 'h':
  2086. *(short *) loc = strtoul(p, &endp, 0);
  2087. loc += tgt_sizeof_short;
  2088. p = endp;
  2089. break;
  2090. case 'i':
  2091. *(int *) loc = strtoul(p, &endp, 0);
  2092. loc += tgt_sizeof_int;
  2093. p = endp;
  2094. break;
  2095. case 'l':
  2096. *(long *) loc = strtoul(p, &endp, 0);
  2097. loc += tgt_sizeof_long;
  2098. p = endp;
  2099. break;
  2100. default:
  2101. bb_error_msg_and_die("unknown parameter type '%c' for %s",
  2102. *pinfo, param);
  2103. }
  2104. p = skip_whitespace(p);
  2105. if (*p != ',')
  2106. break;
  2107. p = skip_whitespace(p + 1);
  2108. }
  2109. if (n < min)
  2110. bb_error_msg_and_die("parameter %s requires at least %d arguments", param, min);
  2111. if (*p != '\0')
  2112. bb_error_msg_and_die("invalid argument syntax for %s", param);
  2113. }
  2114. free(xoptions);
  2115. }
  2116. #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
  2117. static int new_is_module_checksummed(struct obj_file *f)
  2118. {
  2119. const char *p = get_modinfo_value(f, "using_checksums");
  2120. if (p)
  2121. return xatoi(p);
  2122. return 0;
  2123. }
  2124. /* Get the module's kernel version in the canonical integer form. */
  2125. static int
  2126. new_get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
  2127. {
  2128. char *p, *q;
  2129. int a, b, c;
  2130. p = get_modinfo_value(f, "kernel_version");
  2131. if (p == NULL)
  2132. return -1;
  2133. safe_strncpy(str, p, STRVERSIONLEN);
  2134. a = strtoul(p, &p, 10);
  2135. if (*p != '.')
  2136. return -1;
  2137. b = strtoul(p + 1, &p, 10);
  2138. if (*p != '.')
  2139. return -1;
  2140. c = strtoul(p + 1, &q, 10);
  2141. if (p + 1 == q)
  2142. return -1;
  2143. return a << 16 | b << 8 | c;
  2144. }
  2145. #endif /* FEATURE_INSMOD_VERSION_CHECKING */
  2146. /* Fetch the loaded modules, and all currently exported symbols. */
  2147. static void new_get_kernel_symbols(void)
  2148. {
  2149. char *module_names, *mn;
  2150. struct external_module *modules, *m;
  2151. struct new_module_symbol *syms, *s;
  2152. size_t ret, bufsize, nmod, nsyms, i, j;
  2153. /* Collect the loaded modules. */
  2154. bufsize = 256;
  2155. module_names = xmalloc(bufsize);
  2156. retry_modules_load:
  2157. if (query_module(NULL, QM_MODULES, module_names, bufsize, &ret)) {
  2158. if (errno == ENOSPC && bufsize < ret) {
  2159. bufsize = ret;
  2160. module_names = xrealloc(module_names, bufsize);
  2161. goto retry_modules_load;
  2162. }
  2163. bb_perror_msg_and_die("QM_MODULES");
  2164. }
  2165. n_ext_modules = nmod = ret;
  2166. /* Collect the modules' symbols. */
  2167. if (nmod) {
  2168. ext_modules = modules = xzalloc(nmod * sizeof(*modules));
  2169. for (i = 0, mn = module_names, m = modules;
  2170. i < nmod; ++i, ++m, mn += strlen(mn) + 1) {
  2171. struct new_module_info info;
  2172. if (query_module(mn, QM_INFO, &info, sizeof(info), &ret)) {
  2173. if (errno == ENOENT) {
  2174. /* The module was removed out from underneath us. */
  2175. continue;
  2176. }
  2177. bb_perror_msg_and_die("query_module: QM_INFO: %s", mn);
  2178. }
  2179. bufsize = 1024;
  2180. syms = xmalloc(bufsize);
  2181. retry_mod_sym_load:
  2182. if (query_module(mn, QM_SYMBOLS, syms, bufsize, &ret)) {
  2183. switch (errno) {
  2184. case ENOSPC:
  2185. bufsize = ret;
  2186. syms = xrealloc(syms, bufsize);
  2187. goto retry_mod_sym_load;
  2188. case ENOENT:
  2189. /* The module was removed out from underneath us. */
  2190. continue;
  2191. default:
  2192. bb_perror_msg_and_die("query_module: QM_SYMBOLS: %s", mn);
  2193. }
  2194. }
  2195. nsyms = ret;
  2196. m->name = mn;
  2197. m->addr = info.addr;
  2198. m->nsyms = nsyms;
  2199. m->syms = syms;
  2200. for (j = 0, s = syms; j < nsyms; ++j, ++s) {
  2201. s->name += (unsigned long) syms;
  2202. }
  2203. }
  2204. }
  2205. /* Collect the kernel's symbols. */
  2206. bufsize = 16 * 1024;
  2207. syms = xmalloc(bufsize);
  2208. retry_kern_sym_load:
  2209. if (query_module(NULL, QM_SYMBOLS, syms, bufsize, &ret)) {
  2210. if (errno == ENOSPC && bufsize < ret) {
  2211. bufsize = ret;
  2212. syms = xrealloc(syms, bufsize);
  2213. goto retry_kern_sym_load;
  2214. }
  2215. bb_perror_msg_and_die("kernel: QM_SYMBOLS");
  2216. }
  2217. nksyms = nsyms = ret;
  2218. ksyms = syms;
  2219. for (j = 0, s = syms; j < nsyms; ++j, ++s) {
  2220. s->name += (unsigned long) syms;
  2221. }
  2222. }
  2223. /* Return the kernel symbol checksum version, or zero if not used. */
  2224. static int new_is_kernel_checksummed(void)
  2225. {
  2226. struct new_module_symbol *s;
  2227. size_t i;
  2228. /* Using_Versions is not the first symbol, but it should be in there. */
  2229. for (i = 0, s = ksyms; i < nksyms; ++i, ++s)
  2230. if (strcmp((char *) s->name, "Using_Versions") == 0)
  2231. return s->value;
  2232. return 0;
  2233. }
  2234. static void new_create_this_module(struct obj_file *f, const char *m_name)
  2235. {
  2236. struct obj_section *sec;
  2237. sec = obj_create_alloced_section_first(f, ".this", tgt_sizeof_long,
  2238. sizeof(struct new_module));
  2239. /* done by obj_create_alloced_section_first: */
  2240. /*memset(sec->contents, 0, sizeof(struct new_module));*/
  2241. obj_add_symbol(f, SPFX "__this_module", -1,
  2242. ELF_ST_INFO(STB_LOCAL, STT_OBJECT), sec->idx, 0,
  2243. sizeof(struct new_module));
  2244. obj_string_patch(f, sec->idx, offsetof(struct new_module, name),
  2245. m_name);
  2246. }
  2247. #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
  2248. /* add an entry to the __ksymtab section, creating it if necessary */
  2249. static void new_add_ksymtab(struct obj_file *f, struct obj_symbol *sym)
  2250. {
  2251. struct obj_section *sec;
  2252. ElfW(Addr) ofs;
  2253. /* ensure __ksymtab is allocated, EXPORT_NOSYMBOLS creates a non-alloc section.
  2254. * If __ksymtab is defined but not marked alloc, x out the first character
  2255. * (no obj_delete routine) and create a new __ksymtab with the correct
  2256. * characteristics.
  2257. */
  2258. sec = obj_find_section(f, "__ksymtab");
  2259. if (sec && !(sec->header.sh_flags & SHF_ALLOC)) {
  2260. *((char *)(sec->name)) = 'x'; /* override const */
  2261. sec = NULL;
  2262. }
  2263. if (!sec)
  2264. sec = obj_create_alloced_section(f, "__ksymtab",
  2265. tgt_sizeof_void_p, 0);
  2266. if (!sec)
  2267. return;
  2268. sec->header.sh_flags |= SHF_ALLOC;
  2269. /* Empty section might be byte-aligned */
  2270. sec->header.sh_addralign = tgt_sizeof_void_p;
  2271. ofs = sec->header.sh_size;
  2272. obj_symbol_patch(f, sec->idx, ofs, sym);
  2273. obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p, sym->name);
  2274. obj_extend_section(sec, 2 * tgt_sizeof_char_p);
  2275. }
  2276. #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
  2277. static int new_create_module_ksymtab(struct obj_file *f)
  2278. {
  2279. struct obj_section *sec;
  2280. int i;
  2281. /* We must always add the module references. */
  2282. if (n_ext_modules_used) {
  2283. struct new_module_ref *dep;
  2284. struct obj_symbol *tm;
  2285. sec = obj_create_alloced_section(f, ".kmodtab", tgt_sizeof_void_p,
  2286. (sizeof(struct new_module_ref)
  2287. * n_ext_modules_used));
  2288. if (!sec)
  2289. return 0;
  2290. tm = obj_find_symbol(f, SPFX "__this_module");
  2291. dep = (struct new_module_ref *) sec->contents;
  2292. for (i = 0; i < n_ext_modules; ++i)
  2293. if (ext_modules[i].used) {
  2294. dep->dep = ext_modules[i].addr;
  2295. obj_symbol_patch(f, sec->idx,
  2296. (char *) &dep->ref - sec->contents, tm);
  2297. dep->next_ref = 0;
  2298. ++dep;
  2299. }
  2300. }
  2301. if (!flag_noexport && !obj_find_section(f, "__ksymtab")) {
  2302. size_t nsyms;
  2303. int *loaded;
  2304. sec = obj_create_alloced_section(f, "__ksymtab", tgt_sizeof_void_p, 0);
  2305. /* We don't want to export symbols residing in sections that
  2306. aren't loaded. There are a number of these created so that
  2307. we make sure certain module options don't appear twice. */
  2308. i = f->header.e_shnum;
  2309. loaded = alloca(sizeof(int) * i);
  2310. while (--i >= 0)
  2311. loaded[i] = (f->sections[i]->header.sh_flags & SHF_ALLOC) != 0;
  2312. for (nsyms = i = 0; i < HASH_BUCKETS; ++i) {
  2313. struct obj_symbol *sym;
  2314. for (sym = f->symtab[i]; sym; sym = sym->next) {
  2315. if (ELF_ST_BIND(sym->info) != STB_LOCAL
  2316. && sym->secidx <= SHN_HIRESERVE
  2317. && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
  2318. ) {
  2319. ElfW(Addr) ofs = nsyms * 2 * tgt_sizeof_void_p;
  2320. obj_symbol_patch(f, sec->idx, ofs, sym);
  2321. obj_string_patch(f, sec->idx, ofs + tgt_sizeof_void_p,
  2322. sym->name);
  2323. nsyms++;
  2324. }
  2325. }
  2326. }
  2327. obj_extend_section(sec, nsyms * 2 * tgt_sizeof_char_p);
  2328. }
  2329. return 1;
  2330. }
  2331. static int
  2332. new_init_module(const char *m_name, struct obj_file *f, unsigned long m_size)
  2333. {
  2334. struct new_module *module;
  2335. struct obj_section *sec;
  2336. void *image;
  2337. int ret;
  2338. tgt_long m_addr;
  2339. sec = obj_find_section(f, ".this");
  2340. if (!sec || !sec->contents) {
  2341. bb_perror_msg_and_die("corrupt module %s?", m_name);
  2342. }
  2343. module = (struct new_module *) sec->contents;
  2344. m_addr = sec->header.sh_addr;
  2345. module->size_of_struct = sizeof(*module);
  2346. module->size = m_size;
  2347. module->flags = flag_autoclean ? NEW_MOD_AUTOCLEAN : 0;
  2348. sec = obj_find_section(f, "__ksymtab");
  2349. if (sec && sec->header.sh_size) {
  2350. module->syms = sec->header.sh_addr;
  2351. module->nsyms = sec->header.sh_size / (2 * tgt_sizeof_char_p);
  2352. }
  2353. if (n_ext_modules_used) {
  2354. sec = obj_find_section(f, ".kmodtab");
  2355. module->deps = sec->header.sh_addr;
  2356. module->ndeps = n_ext_modules_used;
  2357. }
  2358. module->init = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "init_module"));
  2359. module->cleanup = obj_symbol_final_value(f, obj_find_symbol(f, SPFX "cleanup_module"));
  2360. sec = obj_find_section(f, "__ex_table");
  2361. if (sec) {
  2362. module->ex_table_start = sec->header.sh_addr;
  2363. module->ex_table_end = sec->header.sh_addr + sec->header.sh_size;
  2364. }
  2365. sec = obj_find_section(f, ".text.init");
  2366. if (sec) {
  2367. module->runsize = sec->header.sh_addr - m_addr;
  2368. }
  2369. sec = obj_find_section(f, ".data.init");
  2370. if (sec) {
  2371. if (!module->runsize
  2372. || module->runsize > sec->header.sh_addr - m_addr
  2373. ) {
  2374. module->runsize = sec->header.sh_addr - m_addr;
  2375. }
  2376. }
  2377. sec = obj_find_section(f, ARCHDATA_SEC_NAME);
  2378. if (sec && sec->header.sh_size) {
  2379. module->archdata_start = (void*)sec->header.sh_addr;
  2380. module->archdata_end = module->archdata_start + sec->header.sh_size;
  2381. }
  2382. sec = obj_find_section(f, KALLSYMS_SEC_NAME);
  2383. if (sec && sec->header.sh_size) {
  2384. module->kallsyms_start = (void*)sec->header.sh_addr;
  2385. module->kallsyms_end = module->kallsyms_start + sec->header.sh_size;
  2386. }
  2387. /* Whew! All of the initialization is complete. Collect the final
  2388. module image and give it to the kernel. */
  2389. image = xmalloc(m_size);
  2390. obj_create_image(f, image);
  2391. ret = init_module(m_name, (struct new_module *) image);
  2392. if (ret)
  2393. bb_perror_msg("init_module: %s", m_name);
  2394. free(image);
  2395. return ret == 0;
  2396. }
  2397. /*======================================================================*/
  2398. static void
  2399. obj_string_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
  2400. const char *string)
  2401. {
  2402. struct obj_string_patch *p;
  2403. struct obj_section *strsec;
  2404. size_t len = strlen(string) + 1;
  2405. char *loc;
  2406. p = xzalloc(sizeof(*p));
  2407. p->next = f->string_patches;
  2408. p->reloc_secidx = secidx;
  2409. p->reloc_offset = offset;
  2410. f->string_patches = p;
  2411. strsec = obj_find_section(f, ".kstrtab");
  2412. if (strsec == NULL) {
  2413. strsec = obj_create_alloced_section(f, ".kstrtab", 1, len);
  2414. /*p->string_offset = 0;*/
  2415. loc = strsec->contents;
  2416. } else {
  2417. p->string_offset = strsec->header.sh_size;
  2418. loc = obj_extend_section(strsec, len);
  2419. }
  2420. memcpy(loc, string, len);
  2421. }
  2422. static void
  2423. obj_symbol_patch(struct obj_file *f, int secidx, ElfW(Addr) offset,
  2424. struct obj_symbol *sym)
  2425. {
  2426. struct obj_symbol_patch *p;
  2427. p = xmalloc(sizeof(*p));
  2428. p->next = f->symbol_patches;
  2429. p->reloc_secidx = secidx;
  2430. p->reloc_offset = offset;
  2431. p->sym = sym;
  2432. f->symbol_patches = p;
  2433. }
  2434. static void obj_check_undefineds(struct obj_file *f)
  2435. {
  2436. unsigned i;
  2437. for (i = 0; i < HASH_BUCKETS; ++i) {
  2438. struct obj_symbol *sym;
  2439. for (sym = f->symtab[i]; sym; sym = sym->next) {
  2440. if (sym->secidx == SHN_UNDEF) {
  2441. if (ELF_ST_BIND(sym->info) == STB_WEAK) {
  2442. sym->secidx = SHN_ABS;
  2443. sym->value = 0;
  2444. } else {
  2445. if (!flag_quiet)
  2446. bb_error_msg_and_die("unresolved symbol %s", sym->name);
  2447. }
  2448. }
  2449. }
  2450. }
  2451. }
  2452. static void obj_allocate_commons(struct obj_file *f)
  2453. {
  2454. struct common_entry {
  2455. struct common_entry *next;
  2456. struct obj_symbol *sym;
  2457. } *common_head = NULL;
  2458. unsigned long i;
  2459. for (i = 0; i < HASH_BUCKETS; ++i) {
  2460. struct obj_symbol *sym;
  2461. for (sym = f->symtab[i]; sym; sym = sym->next) {
  2462. if (sym->secidx == SHN_COMMON) {
  2463. /* Collect all COMMON symbols and sort them by size so as to
  2464. minimize space wasted by alignment requirements. */
  2465. struct common_entry **p, *n;
  2466. for (p = &common_head; *p; p = &(*p)->next)
  2467. if (sym->size <= (*p)->sym->size)
  2468. break;
  2469. n = alloca(sizeof(*n));
  2470. n->next = *p;
  2471. n->sym = sym;
  2472. *p = n;
  2473. }
  2474. }
  2475. }
  2476. for (i = 1; i < f->local_symtab_size; ++i) {
  2477. struct obj_symbol *sym = f->local_symtab[i];
  2478. if (sym && sym->secidx == SHN_COMMON) {
  2479. struct common_entry **p, *n;
  2480. for (p = &common_head; *p; p = &(*p)->next) {
  2481. if (sym == (*p)->sym)
  2482. break;
  2483. if (sym->size < (*p)->sym->size) {
  2484. n = alloca(sizeof(*n));
  2485. n->next = *p;
  2486. n->sym = sym;
  2487. *p = n;
  2488. break;
  2489. }
  2490. }
  2491. }
  2492. }
  2493. if (common_head) {
  2494. /* Find the bss section. */
  2495. for (i = 0; i < f->header.e_shnum; ++i)
  2496. if (f->sections[i]->header.sh_type == SHT_NOBITS)
  2497. break;
  2498. /* If for some reason there hadn't been one, create one. */
  2499. if (i == f->header.e_shnum) {
  2500. struct obj_section *sec;
  2501. f->header.e_shnum++;
  2502. f->sections = xrealloc_vector(f->sections, 2, i);
  2503. f->sections[i] = sec = arch_new_section();
  2504. sec->header.sh_type = SHT_PROGBITS;
  2505. sec->header.sh_flags = SHF_WRITE | SHF_ALLOC;
  2506. sec->name = ".bss";
  2507. sec->idx = i;
  2508. }
  2509. /* Allocate the COMMONS. */
  2510. {
  2511. ElfW(Addr) bss_size = f->sections[i]->header.sh_size;
  2512. ElfW(Addr) max_align = f->sections[i]->header.sh_addralign;
  2513. struct common_entry *c;
  2514. for (c = common_head; c; c = c->next) {
  2515. ElfW(Addr) align = c->sym->value;
  2516. if (align > max_align)
  2517. max_align = align;
  2518. if (bss_size & (align - 1))
  2519. bss_size = (bss_size | (align - 1)) + 1;
  2520. c->sym->secidx = i;
  2521. c->sym->value = bss_size;
  2522. bss_size += c->sym->size;
  2523. }
  2524. f->sections[i]->header.sh_size = bss_size;
  2525. f->sections[i]->header.sh_addralign = max_align;
  2526. }
  2527. }
  2528. /* For the sake of patch relocation and parameter initialization,
  2529. allocate zeroed data for NOBITS sections now. Note that after
  2530. this we cannot assume NOBITS are really empty. */
  2531. for (i = 0; i < f->header.e_shnum; ++i) {
  2532. struct obj_section *s = f->sections[i];
  2533. if (s->header.sh_type == SHT_NOBITS) {
  2534. s->contents = NULL;
  2535. if (s->header.sh_size != 0)
  2536. s->contents = xzalloc(s->header.sh_size);
  2537. s->header.sh_type = SHT_PROGBITS;
  2538. }
  2539. }
  2540. }
  2541. static unsigned long obj_load_size(struct obj_file *f)
  2542. {
  2543. unsigned long dot = 0;
  2544. struct obj_section *sec;
  2545. /* Finalize the positions of the sections relative to one another. */
  2546. for (sec = f->load_order; sec; sec = sec->load_next) {
  2547. ElfW(Addr) align;
  2548. align = sec->header.sh_addralign;
  2549. if (align && (dot & (align - 1)))
  2550. dot = (dot | (align - 1)) + 1;
  2551. sec->header.sh_addr = dot;
  2552. dot += sec->header.sh_size;
  2553. }
  2554. return dot;
  2555. }
  2556. static int obj_relocate(struct obj_file *f, ElfW(Addr) base)
  2557. {
  2558. int i, n = f->header.e_shnum;
  2559. int ret = 1;
  2560. /* Finalize the addresses of the sections. */
  2561. f->baseaddr = base;
  2562. for (i = 0; i < n; ++i)
  2563. f->sections[i]->header.sh_addr += base;
  2564. /* And iterate over all of the relocations. */
  2565. for (i = 0; i < n; ++i) {
  2566. struct obj_section *relsec, *symsec, *targsec, *strsec;
  2567. ElfW(RelM) * rel, *relend;
  2568. ElfW(Sym) * symtab;
  2569. const char *strtab;
  2570. relsec = f->sections[i];
  2571. if (relsec->header.sh_type != SHT_RELM)
  2572. continue;
  2573. symsec = f->sections[relsec->header.sh_link];
  2574. targsec = f->sections[relsec->header.sh_info];
  2575. strsec = f->sections[symsec->header.sh_link];
  2576. rel = (ElfW(RelM) *) relsec->contents;
  2577. relend = rel + (relsec->header.sh_size / sizeof(ElfW(RelM)));
  2578. symtab = (ElfW(Sym) *) symsec->contents;
  2579. strtab = (const char *) strsec->contents;
  2580. for (; rel < relend; ++rel) {
  2581. ElfW(Addr) value = 0;
  2582. struct obj_symbol *intsym = NULL;
  2583. unsigned long symndx;
  2584. ElfW(Sym) *extsym = NULL;
  2585. const char *errmsg;
  2586. /* Attempt to find a value to use for this relocation. */
  2587. symndx = ELF_R_SYM(rel->r_info);
  2588. if (symndx) {
  2589. /* Note we've already checked for undefined symbols. */
  2590. extsym = &symtab[symndx];
  2591. if (ELF_ST_BIND(extsym->st_info) == STB_LOCAL) {
  2592. /* Local symbols we look up in the local table to be sure
  2593. we get the one that is really intended. */
  2594. intsym = f->local_symtab[symndx];
  2595. } else {
  2596. /* Others we look up in the hash table. */
  2597. const char *name;
  2598. if (extsym->st_name)
  2599. name = strtab + extsym->st_name;
  2600. else
  2601. name = f->sections[extsym->st_shndx]->name;
  2602. intsym = obj_find_symbol(f, name);
  2603. }
  2604. value = obj_symbol_final_value(f, intsym);
  2605. intsym->referenced = 1;
  2606. }
  2607. #if SHT_RELM == SHT_RELA
  2608. #if defined(__alpha__) && defined(AXP_BROKEN_GAS)
  2609. /* Work around a nasty GAS bug, that is fixed as of 2.7.0.9. */
  2610. if (!extsym || !extsym->st_name
  2611. || ELF_ST_BIND(extsym->st_info) != STB_LOCAL)
  2612. #endif
  2613. value += rel->r_addend;
  2614. #endif
  2615. /* Do it! */
  2616. switch (arch_apply_relocation
  2617. (f, targsec, /*symsec,*/ intsym, rel, value)
  2618. ) {
  2619. case obj_reloc_ok:
  2620. break;
  2621. case obj_reloc_overflow:
  2622. errmsg = "Relocation overflow";
  2623. goto bad_reloc;
  2624. case obj_reloc_dangerous:
  2625. errmsg = "Dangerous relocation";
  2626. goto bad_reloc;
  2627. case obj_reloc_unhandled:
  2628. errmsg = "Unhandled relocation";
  2629. bad_reloc:
  2630. if (extsym) {
  2631. bb_error_msg("%s of type %ld for %s", errmsg,
  2632. (long) ELF_R_TYPE(rel->r_info),
  2633. strtab + extsym->st_name);
  2634. } else {
  2635. bb_error_msg("%s of type %ld", errmsg,
  2636. (long) ELF_R_TYPE(rel->r_info));
  2637. }
  2638. ret = 0;
  2639. break;
  2640. }
  2641. }
  2642. }
  2643. /* Finally, take care of the patches. */
  2644. if (f->string_patches) {
  2645. struct obj_string_patch *p;
  2646. struct obj_section *strsec;
  2647. ElfW(Addr) strsec_base;
  2648. strsec = obj_find_section(f, ".kstrtab");
  2649. strsec_base = strsec->header.sh_addr;
  2650. for (p = f->string_patches; p; p = p->next) {
  2651. struct obj_section *targsec = f->sections[p->reloc_secidx];
  2652. *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
  2653. = strsec_base + p->string_offset;
  2654. }
  2655. }
  2656. if (f->symbol_patches) {
  2657. struct obj_symbol_patch *p;
  2658. for (p = f->symbol_patches; p; p = p->next) {
  2659. struct obj_section *targsec = f->sections[p->reloc_secidx];
  2660. *(ElfW(Addr) *) (targsec->contents + p->reloc_offset)
  2661. = obj_symbol_final_value(f, p->sym);
  2662. }
  2663. }
  2664. return ret;
  2665. }
  2666. static int obj_create_image(struct obj_file *f, char *image)
  2667. {
  2668. struct obj_section *sec;
  2669. ElfW(Addr) base = f->baseaddr;
  2670. for (sec = f->load_order; sec; sec = sec->load_next) {
  2671. char *secimg;
  2672. if (sec->contents == 0 || sec->header.sh_size == 0)
  2673. continue;
  2674. secimg = image + (sec->header.sh_addr - base);
  2675. /* Note that we allocated data for NOBITS sections earlier. */
  2676. memcpy(secimg, sec->contents, sec->header.sh_size);
  2677. }
  2678. return 1;
  2679. }
  2680. /*======================================================================*/
  2681. static struct obj_file *obj_load(char *image, size_t image_size, int loadprogbits)
  2682. {
  2683. typedef uint32_t aliased_uint32_t FIX_ALIASING;
  2684. #if BB_LITTLE_ENDIAN
  2685. # define ELFMAG_U32 ((uint32_t)(ELFMAG0 + 0x100 * (ELFMAG1 + (0x100 * (ELFMAG2 + 0x100 * ELFMAG3)))))
  2686. #else
  2687. # define ELFMAG_U32 ((uint32_t)((((ELFMAG0 * 0x100) + ELFMAG1) * 0x100 + ELFMAG2) * 0x100 + ELFMAG3))
  2688. #endif
  2689. struct obj_file *f;
  2690. ElfW(Shdr) * section_headers;
  2691. size_t shnum, i;
  2692. char *shstrtab;
  2693. /* Read the file header. */
  2694. f = arch_new_file();
  2695. f->symbol_cmp = strcmp;
  2696. f->symbol_hash = obj_elf_hash;
  2697. f->load_order_search_start = &f->load_order;
  2698. if (image_size < sizeof(f->header))
  2699. bb_error_msg_and_die("error while loading ELF header");
  2700. memcpy(&f->header, image, sizeof(f->header));
  2701. if (*(aliased_uint32_t*)(&f->header.e_ident) != ELFMAG_U32) {
  2702. bb_error_msg_and_die("not an ELF file");
  2703. }
  2704. if (f->header.e_ident[EI_CLASS] != ELFCLASSM
  2705. || f->header.e_ident[EI_DATA] != (BB_BIG_ENDIAN ? ELFDATA2MSB : ELFDATA2LSB)
  2706. || f->header.e_ident[EI_VERSION] != EV_CURRENT
  2707. || !MATCH_MACHINE(f->header.e_machine)
  2708. ) {
  2709. bb_error_msg_and_die("ELF file not for this architecture");
  2710. }
  2711. if (f->header.e_type != ET_REL) {
  2712. bb_error_msg_and_die("ELF file not a relocatable object");
  2713. }
  2714. /* Read the section headers. */
  2715. if (f->header.e_shentsize != sizeof(ElfW(Shdr))) {
  2716. bb_error_msg_and_die("section header size mismatch: %lu != %lu",
  2717. (unsigned long) f->header.e_shentsize,
  2718. (unsigned long) sizeof(ElfW(Shdr)));
  2719. }
  2720. shnum = f->header.e_shnum;
  2721. /* Growth of ->sections vector will be done by
  2722. * xrealloc_vector(..., 2, ...), therefore we must allocate
  2723. * at least 2^2 = 4 extra elements here. */
  2724. f->sections = xzalloc(sizeof(f->sections[0]) * (shnum + 4));
  2725. section_headers = alloca(sizeof(ElfW(Shdr)) * shnum);
  2726. if (image_size < f->header.e_shoff + sizeof(ElfW(Shdr)) * shnum)
  2727. bb_error_msg_and_die("error while loading section headers");
  2728. memcpy(section_headers, image + f->header.e_shoff, sizeof(ElfW(Shdr)) * shnum);
  2729. /* Read the section data. */
  2730. for (i = 0; i < shnum; ++i) {
  2731. struct obj_section *sec;
  2732. f->sections[i] = sec = arch_new_section();
  2733. sec->header = section_headers[i];
  2734. sec->idx = i;
  2735. if (sec->header.sh_size) {
  2736. switch (sec->header.sh_type) {
  2737. case SHT_NULL:
  2738. case SHT_NOTE:
  2739. case SHT_NOBITS:
  2740. /* ignore */
  2741. break;
  2742. case SHT_PROGBITS:
  2743. #if LOADBITS
  2744. if (!loadprogbits) {
  2745. sec->contents = NULL;
  2746. break;
  2747. }
  2748. #endif
  2749. case SHT_SYMTAB:
  2750. case SHT_STRTAB:
  2751. case SHT_RELM:
  2752. #if defined(__mips__)
  2753. case SHT_MIPS_DWARF:
  2754. #endif
  2755. sec->contents = NULL;
  2756. if (sec->header.sh_size > 0) {
  2757. sec->contents = xmalloc(sec->header.sh_size);
  2758. if (image_size < (sec->header.sh_offset + sec->header.sh_size))
  2759. bb_error_msg_and_die("error while loading section data");
  2760. memcpy(sec->contents, image + sec->header.sh_offset, sec->header.sh_size);
  2761. }
  2762. break;
  2763. #if SHT_RELM == SHT_REL
  2764. case SHT_RELA:
  2765. bb_error_msg_and_die("RELA relocations not supported on this architecture");
  2766. #else
  2767. case SHT_REL:
  2768. bb_error_msg_and_die("REL relocations not supported on this architecture");
  2769. #endif
  2770. default:
  2771. if (sec->header.sh_type >= SHT_LOPROC) {
  2772. /* Assume processor specific section types are debug
  2773. info and can safely be ignored. If this is ever not
  2774. the case (Hello MIPS?), don't put ifdefs here but
  2775. create an arch_load_proc_section(). */
  2776. break;
  2777. }
  2778. bb_error_msg_and_die("can't handle sections of type %ld",
  2779. (long) sec->header.sh_type);
  2780. }
  2781. }
  2782. }
  2783. /* Do what sort of interpretation as needed by each section. */
  2784. shstrtab = f->sections[f->header.e_shstrndx]->contents;
  2785. for (i = 0; i < shnum; ++i) {
  2786. struct obj_section *sec = f->sections[i];
  2787. sec->name = shstrtab + sec->header.sh_name;
  2788. }
  2789. for (i = 0; i < shnum; ++i) {
  2790. struct obj_section *sec = f->sections[i];
  2791. /* .modinfo should be contents only but gcc has no attribute for that.
  2792. * The kernel may have marked .modinfo as ALLOC, ignore this bit.
  2793. */
  2794. if (strcmp(sec->name, ".modinfo") == 0)
  2795. sec->header.sh_flags &= ~SHF_ALLOC;
  2796. if (sec->header.sh_flags & SHF_ALLOC)
  2797. obj_insert_section_load_order(f, sec);
  2798. switch (sec->header.sh_type) {
  2799. case SHT_SYMTAB:
  2800. {
  2801. unsigned long nsym, j;
  2802. char *strtab;
  2803. ElfW(Sym) * sym;
  2804. if (sec->header.sh_entsize != sizeof(ElfW(Sym))) {
  2805. bb_error_msg_and_die("symbol size mismatch: %lu != %lu",
  2806. (unsigned long) sec->header.sh_entsize,
  2807. (unsigned long) sizeof(ElfW(Sym)));
  2808. }
  2809. nsym = sec->header.sh_size / sizeof(ElfW(Sym));
  2810. strtab = f->sections[sec->header.sh_link]->contents;
  2811. sym = (ElfW(Sym) *) sec->contents;
  2812. /* Allocate space for a table of local symbols. */
  2813. j = f->local_symtab_size = sec->header.sh_info;
  2814. f->local_symtab = xzalloc(j * sizeof(struct obj_symbol *));
  2815. /* Insert all symbols into the hash table. */
  2816. for (j = 1, ++sym; j < nsym; ++j, ++sym) {
  2817. ElfW(Addr) val = sym->st_value;
  2818. const char *name;
  2819. if (sym->st_name)
  2820. name = strtab + sym->st_name;
  2821. else if (sym->st_shndx < shnum)
  2822. name = f->sections[sym->st_shndx]->name;
  2823. else
  2824. continue;
  2825. #if defined(__SH5__)
  2826. /*
  2827. * For sh64 it is possible that the target of a branch
  2828. * requires a mode switch (32 to 16 and back again).
  2829. *
  2830. * This is implied by the lsb being set in the target
  2831. * address for SHmedia mode and clear for SHcompact.
  2832. */
  2833. val |= sym->st_other & 4;
  2834. #endif
  2835. obj_add_symbol(f, name, j, sym->st_info, sym->st_shndx,
  2836. val, sym->st_size);
  2837. }
  2838. }
  2839. break;
  2840. case SHT_RELM:
  2841. if (sec->header.sh_entsize != sizeof(ElfW(RelM))) {
  2842. bb_error_msg_and_die("relocation entry size mismatch: %lu != %lu",
  2843. (unsigned long) sec->header.sh_entsize,
  2844. (unsigned long) sizeof(ElfW(RelM)));
  2845. }
  2846. break;
  2847. /* XXX Relocation code from modutils-2.3.19 is not here.
  2848. * Why? That's about 20 lines of code from obj/obj_load.c,
  2849. * which gets done in a second pass through the sections.
  2850. * This BusyBox insmod does similar work in obj_relocate(). */
  2851. }
  2852. }
  2853. return f;
  2854. }
  2855. #if ENABLE_FEATURE_INSMOD_LOADINKMEM
  2856. /*
  2857. * load the unloaded sections directly into the memory allocated by
  2858. * kernel for the module
  2859. */
  2860. static int obj_load_progbits(char *image, size_t image_size, struct obj_file *f, char *imagebase)
  2861. {
  2862. ElfW(Addr) base = f->baseaddr;
  2863. struct obj_section* sec;
  2864. for (sec = f->load_order; sec; sec = sec->load_next) {
  2865. /* section already loaded? */
  2866. if (sec->contents != NULL)
  2867. continue;
  2868. if (sec->header.sh_size == 0)
  2869. continue;
  2870. sec->contents = imagebase + (sec->header.sh_addr - base);
  2871. if (image_size < (sec->header.sh_offset + sec->header.sh_size)) {
  2872. bb_error_msg("error reading ELF section data");
  2873. return 0; /* need to delete half-loaded module! */
  2874. }
  2875. memcpy(sec->contents, image + sec->header.sh_offset, sec->header.sh_size);
  2876. }
  2877. return 1;
  2878. }
  2879. #endif
  2880. static void hide_special_symbols(struct obj_file *f)
  2881. {
  2882. static const char *const specials[] = {
  2883. SPFX "cleanup_module",
  2884. SPFX "init_module",
  2885. SPFX "kernel_version",
  2886. NULL
  2887. };
  2888. struct obj_symbol *sym;
  2889. const char *const *p;
  2890. for (p = specials; *p; ++p) {
  2891. sym = obj_find_symbol(f, *p);
  2892. if (sym != NULL)
  2893. sym->info = ELF_ST_INFO(STB_LOCAL, ELF_ST_TYPE(sym->info));
  2894. }
  2895. }
  2896. #if ENABLE_FEATURE_CHECK_TAINTED_MODULE
  2897. static int obj_gpl_license(struct obj_file *f, const char **license)
  2898. {
  2899. struct obj_section *sec;
  2900. /* This list must match *exactly* the list of allowable licenses in
  2901. * linux/include/linux/module.h. Checking for leading "GPL" will not
  2902. * work, somebody will use "GPL sucks, this is proprietary".
  2903. */
  2904. static const char *const gpl_licenses[] = {
  2905. "GPL",
  2906. "GPL v2",
  2907. "GPL and additional rights",
  2908. "Dual BSD/GPL",
  2909. "Dual MPL/GPL"
  2910. };
  2911. sec = obj_find_section(f, ".modinfo");
  2912. if (sec) {
  2913. const char *value, *ptr, *endptr;
  2914. ptr = sec->contents;
  2915. endptr = ptr + sec->header.sh_size;
  2916. while (ptr < endptr) {
  2917. value = strchr(ptr, '=');
  2918. if (value && strncmp(ptr, "license", value-ptr) == 0) {
  2919. unsigned i;
  2920. if (license)
  2921. *license = value+1;
  2922. for (i = 0; i < ARRAY_SIZE(gpl_licenses); ++i) {
  2923. if (strcmp(value+1, gpl_licenses[i]) == 0)
  2924. return 0;
  2925. }
  2926. return 2;
  2927. }
  2928. ptr = strchr(ptr, '\0');
  2929. if (ptr)
  2930. ptr++;
  2931. else
  2932. ptr = endptr;
  2933. }
  2934. }
  2935. return 1;
  2936. }
  2937. #define TAINT_FILENAME "/proc/sys/kernel/tainted"
  2938. #define TAINT_PROPRIETORY_MODULE (1 << 0)
  2939. #define TAINT_FORCED_MODULE (1 << 1)
  2940. #define TAINT_UNSAFE_SMP (1 << 2)
  2941. #define TAINT_URL "http://www.tux.org/lkml/#export-tainted"
  2942. static void set_tainted(int fd, const char *m_name,
  2943. int kernel_has_tainted, int taint,
  2944. const char *text1, const char *text2)
  2945. {
  2946. static smallint printed_info;
  2947. char buf[80];
  2948. int oldval;
  2949. if (fd < 0 && !kernel_has_tainted)
  2950. return; /* New modutils on old kernel */
  2951. printf("Warning: loading %s will taint the kernel: %s%s\n",
  2952. m_name, text1, text2);
  2953. if (!printed_info) {
  2954. printf(" See %s for information about tainted modules\n", TAINT_URL);
  2955. printed_info = 1;
  2956. }
  2957. if (fd >= 0) {
  2958. read(fd, buf, sizeof(buf)-1);
  2959. buf[sizeof(buf)-1] = '\0';
  2960. oldval = strtoul(buf, NULL, 10);
  2961. sprintf(buf, "%d\n", oldval | taint);
  2962. xwrite_str(fd, buf);
  2963. }
  2964. }
  2965. /* Check if loading this module will taint the kernel. */
  2966. static void check_tainted_module(struct obj_file *f, const char *m_name)
  2967. {
  2968. int fd, kernel_has_tainted;
  2969. const char *ptr;
  2970. kernel_has_tainted = 1;
  2971. fd = open(TAINT_FILENAME, O_RDWR);
  2972. if (fd < 0) {
  2973. if (errno == ENOENT)
  2974. kernel_has_tainted = 0;
  2975. else if (errno == EACCES)
  2976. kernel_has_tainted = 1;
  2977. else {
  2978. bb_simple_perror_msg(TAINT_FILENAME);
  2979. kernel_has_tainted = 0;
  2980. }
  2981. }
  2982. switch (obj_gpl_license(f, &ptr)) {
  2983. case 0:
  2984. break;
  2985. case 1:
  2986. set_tainted(fd, m_name, kernel_has_tainted, TAINT_PROPRIETORY_MODULE, "no license", "");
  2987. break;
  2988. default: /* case 2: */
  2989. /* The module has a non-GPL license so we pretend that the
  2990. * kernel always has a taint flag to get a warning even on
  2991. * kernels without the proc flag.
  2992. */
  2993. set_tainted(fd, m_name, 1, TAINT_PROPRIETORY_MODULE, "non-GPL license - ", ptr);
  2994. break;
  2995. }
  2996. if (flag_force_load)
  2997. set_tainted(fd, m_name, 1, TAINT_FORCED_MODULE, "forced load", "");
  2998. if (fd >= 0)
  2999. close(fd);
  3000. }
  3001. #else /* !FEATURE_CHECK_TAINTED_MODULE */
  3002. #define check_tainted_module(x, y) do { } while (0);
  3003. #endif
  3004. #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
  3005. /* add module source, timestamp, kernel version and a symbol for the
  3006. * start of some sections. this info is used by ksymoops to do better
  3007. * debugging.
  3008. */
  3009. #if !ENABLE_FEATURE_INSMOD_VERSION_CHECKING
  3010. #define get_module_version(f, str) get_module_version(str)
  3011. #endif
  3012. static int
  3013. get_module_version(struct obj_file *f, char str[STRVERSIONLEN])
  3014. {
  3015. #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
  3016. return new_get_module_version(f, str);
  3017. #else
  3018. strncpy(str, "???", sizeof(str));
  3019. return -1;
  3020. #endif
  3021. }
  3022. /* add module source, timestamp, kernel version and a symbol for the
  3023. * start of some sections. this info is used by ksymoops to do better
  3024. * debugging.
  3025. */
  3026. static void
  3027. add_ksymoops_symbols(struct obj_file *f, const char *filename,
  3028. const char *m_name)
  3029. {
  3030. static const char symprefix[] ALIGN1 = "__insmod_";
  3031. static const char section_names[][8] = {
  3032. ".text",
  3033. ".rodata",
  3034. ".data",
  3035. ".bss",
  3036. ".sbss"
  3037. };
  3038. struct obj_section *sec;
  3039. struct obj_symbol *sym;
  3040. char *name, *absolute_filename;
  3041. char str[STRVERSIONLEN];
  3042. unsigned i;
  3043. int lm_name, lfilename, use_ksymtab, version;
  3044. struct stat statbuf;
  3045. /* WARNING: was using realpath, but replaced by readlink to stop using
  3046. * lots of stack. But here it seems to be able to cause problems? */
  3047. absolute_filename = xmalloc_readlink(filename);
  3048. if (!absolute_filename)
  3049. absolute_filename = xstrdup(filename);
  3050. lm_name = strlen(m_name);
  3051. lfilename = strlen(absolute_filename);
  3052. /* add to ksymtab if it already exists or there is no ksymtab and other symbols
  3053. * are not to be exported. otherwise leave ksymtab alone for now, the
  3054. * "export all symbols" compatibility code will export these symbols later.
  3055. */
  3056. use_ksymtab = obj_find_section(f, "__ksymtab") || flag_noexport;
  3057. sec = obj_find_section(f, ".this");
  3058. if (sec) {
  3059. /* tag the module header with the object name, last modified
  3060. * timestamp and module version. worst case for module version
  3061. * is 0xffffff, decimal 16777215. putting all three fields in
  3062. * one symbol is less readable but saves kernel space.
  3063. */
  3064. if (stat(absolute_filename, &statbuf) != 0)
  3065. statbuf.st_mtime = 0;
  3066. version = get_module_version(f, str); /* -1 if not found */
  3067. name = xasprintf("%s%s_O%s_M%0*lX_V%d",
  3068. symprefix, m_name, absolute_filename,
  3069. (int)(2 * sizeof(statbuf.st_mtime)),
  3070. (long)statbuf.st_mtime,
  3071. version);
  3072. sym = obj_add_symbol(f, name, -1,
  3073. ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
  3074. sec->idx, sec->header.sh_addr, 0);
  3075. if (use_ksymtab)
  3076. new_add_ksymtab(f, sym);
  3077. }
  3078. free(absolute_filename);
  3079. #ifdef _NOT_SUPPORTED_
  3080. /* record where the persistent data is going, same address as previous symbol */
  3081. if (f->persist) {
  3082. name = xasprintf("%s%s_P%s",
  3083. symprefix, m_name, f->persist);
  3084. sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
  3085. sec->idx, sec->header.sh_addr, 0);
  3086. if (use_ksymtab)
  3087. new_add_ksymtab(f, sym);
  3088. }
  3089. #endif
  3090. /* tag the desired sections if size is non-zero */
  3091. for (i = 0; i < ARRAY_SIZE(section_names); ++i) {
  3092. sec = obj_find_section(f, section_names[i]);
  3093. if (sec && sec->header.sh_size) {
  3094. name = xasprintf("%s%s_S%s_L%ld",
  3095. symprefix, m_name, sec->name,
  3096. (long)sec->header.sh_size);
  3097. sym = obj_add_symbol(f, name, -1, ELF_ST_INFO(STB_GLOBAL, STT_NOTYPE),
  3098. sec->idx, sec->header.sh_addr, 0);
  3099. if (use_ksymtab)
  3100. new_add_ksymtab(f, sym);
  3101. }
  3102. }
  3103. }
  3104. #endif /* FEATURE_INSMOD_KSYMOOPS_SYMBOLS */
  3105. #if ENABLE_FEATURE_INSMOD_LOAD_MAP
  3106. static void print_load_map(struct obj_file *f)
  3107. {
  3108. struct obj_section *sec;
  3109. #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
  3110. struct obj_symbol **all, **p;
  3111. int i, nsyms;
  3112. char *loaded; /* array of booleans */
  3113. struct obj_symbol *sym;
  3114. #endif
  3115. /* Report on the section layout. */
  3116. printf("Sections: Size %-*s Align\n",
  3117. (int) (2 * sizeof(void *)), "Address");
  3118. for (sec = f->load_order; sec; sec = sec->load_next) {
  3119. int a;
  3120. unsigned long tmp;
  3121. for (a = -1, tmp = sec->header.sh_addralign; tmp; ++a)
  3122. tmp >>= 1;
  3123. if (a == -1)
  3124. a = 0;
  3125. printf("%-15s %08lx %0*lx 2**%d\n",
  3126. sec->name,
  3127. (long)sec->header.sh_size,
  3128. (int) (2 * sizeof(void *)),
  3129. (long)sec->header.sh_addr,
  3130. a);
  3131. }
  3132. #if ENABLE_FEATURE_INSMOD_LOAD_MAP_FULL
  3133. /* Quick reference which section indices are loaded. */
  3134. i = f->header.e_shnum;
  3135. loaded = alloca(i * sizeof(loaded[0]));
  3136. while (--i >= 0)
  3137. loaded[i] = ((f->sections[i]->header.sh_flags & SHF_ALLOC) != 0);
  3138. /* Collect the symbols we'll be listing. */
  3139. for (nsyms = i = 0; i < HASH_BUCKETS; ++i)
  3140. for (sym = f->symtab[i]; sym; sym = sym->next)
  3141. if (sym->secidx <= SHN_HIRESERVE
  3142. && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
  3143. ) {
  3144. ++nsyms;
  3145. }
  3146. all = alloca(nsyms * sizeof(all[0]));
  3147. for (i = 0, p = all; i < HASH_BUCKETS; ++i)
  3148. for (sym = f->symtab[i]; sym; sym = sym->next)
  3149. if (sym->secidx <= SHN_HIRESERVE
  3150. && (sym->secidx >= SHN_LORESERVE || loaded[sym->secidx])
  3151. ) {
  3152. *p++ = sym;
  3153. }
  3154. /* And list them. */
  3155. printf("\nSymbols:\n");
  3156. for (p = all; p < all + nsyms; ++p) {
  3157. char type = '?';
  3158. unsigned long value;
  3159. sym = *p;
  3160. if (sym->secidx == SHN_ABS) {
  3161. type = 'A';
  3162. value = sym->value;
  3163. } else if (sym->secidx == SHN_UNDEF) {
  3164. type = 'U';
  3165. value = 0;
  3166. } else {
  3167. sec = f->sections[sym->secidx];
  3168. if (sec->header.sh_type == SHT_NOBITS)
  3169. type = 'B';
  3170. else if (sec->header.sh_flags & SHF_ALLOC) {
  3171. if (sec->header.sh_flags & SHF_EXECINSTR)
  3172. type = 'T';
  3173. else if (sec->header.sh_flags & SHF_WRITE)
  3174. type = 'D';
  3175. else
  3176. type = 'R';
  3177. }
  3178. value = sym->value + sec->header.sh_addr;
  3179. }
  3180. if (ELF_ST_BIND(sym->info) == STB_LOCAL)
  3181. type |= 0x20; /* tolower. safe for '?' too */
  3182. printf("%0*lx %c %s\n", (int) (2 * sizeof(void *)), value,
  3183. type, sym->name);
  3184. }
  3185. #endif
  3186. }
  3187. #else /* !FEATURE_INSMOD_LOAD_MAP */
  3188. static void print_load_map(struct obj_file *f UNUSED_PARAM)
  3189. {
  3190. }
  3191. #endif
  3192. int FAST_FUNC bb_init_module_24(const char *m_filename, const char *options)
  3193. {
  3194. int k_crcs;
  3195. unsigned long m_size;
  3196. ElfW(Addr) m_addr;
  3197. struct obj_file *f;
  3198. int exit_status = EXIT_FAILURE;
  3199. char *m_name;
  3200. #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
  3201. int m_has_modinfo;
  3202. #endif
  3203. char *image;
  3204. size_t image_size;
  3205. bool mmaped;
  3206. image_size = INT_MAX - 4095;
  3207. mmaped = 0;
  3208. image = try_to_mmap_module(m_filename, &image_size);
  3209. if (image) {
  3210. mmaped = 1;
  3211. } else {
  3212. /* Load module into memory and unzip if compressed */
  3213. image = xmalloc_open_zipped_read_close(m_filename, &image_size);
  3214. if (!image)
  3215. return EXIT_FAILURE;
  3216. }
  3217. m_name = xstrdup(bb_basename(m_filename));
  3218. /* "module.o[.gz]" -> "module" */
  3219. *strchrnul(m_name, '.') = '\0';
  3220. f = obj_load(image, image_size, LOADBITS);
  3221. #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
  3222. /* Version correspondence? */
  3223. m_has_modinfo = (get_modinfo_value(f, "kernel_version") != NULL);
  3224. if (!flag_quiet) {
  3225. char m_strversion[STRVERSIONLEN];
  3226. struct utsname uts;
  3227. if (m_has_modinfo) {
  3228. int m_version = new_get_module_version(f, m_strversion);
  3229. if (m_version == -1) {
  3230. bb_error_msg_and_die("can't find the kernel version "
  3231. "the module was compiled for");
  3232. }
  3233. }
  3234. uname(&uts);
  3235. if (strncmp(uts.release, m_strversion, STRVERSIONLEN) != 0) {
  3236. bb_error_msg("%skernel-module version mismatch\n"
  3237. "\t%s was compiled for kernel version %s\n"
  3238. "\twhile this kernel is version %s",
  3239. flag_force_load ? "warning: " : "",
  3240. m_name, m_strversion, uts.release);
  3241. if (!flag_force_load)
  3242. goto out;
  3243. }
  3244. }
  3245. #endif
  3246. if (query_module(NULL, 0, NULL, 0, NULL))
  3247. bb_error_msg_and_die("old (unsupported) kernel");
  3248. new_get_kernel_symbols();
  3249. k_crcs = new_is_kernel_checksummed();
  3250. #if ENABLE_FEATURE_INSMOD_VERSION_CHECKING
  3251. {
  3252. int m_crcs = 0;
  3253. if (m_has_modinfo)
  3254. m_crcs = new_is_module_checksummed(f);
  3255. if (m_crcs != k_crcs)
  3256. obj_set_symbol_compare(f, ncv_strcmp, ncv_symbol_hash);
  3257. }
  3258. #endif
  3259. /* Let the module know about the kernel symbols. */
  3260. add_kernel_symbols(f);
  3261. /* Allocate common symbols, symbol tables, and string tables. */
  3262. new_create_this_module(f, m_name);
  3263. obj_check_undefineds(f);
  3264. obj_allocate_commons(f);
  3265. check_tainted_module(f, m_name);
  3266. /* Done with the module name, on to the optional var=value arguments */
  3267. new_process_module_arguments(f, options);
  3268. arch_create_got(f);
  3269. hide_special_symbols(f);
  3270. #if ENABLE_FEATURE_INSMOD_KSYMOOPS_SYMBOLS
  3271. add_ksymoops_symbols(f, m_filename, m_name);
  3272. #endif
  3273. new_create_module_ksymtab(f);
  3274. /* Find current size of the module */
  3275. m_size = obj_load_size(f);
  3276. m_addr = create_module(m_name, m_size);
  3277. if (m_addr == (ElfW(Addr))(-1)) switch (errno) {
  3278. case EEXIST:
  3279. bb_error_msg_and_die("a module named %s already exists", m_name);
  3280. case ENOMEM:
  3281. bb_error_msg_and_die("can't allocate kernel memory for module; needed %lu bytes",
  3282. m_size);
  3283. default:
  3284. bb_perror_msg_and_die("create_module: %s", m_name);
  3285. }
  3286. #if !LOADBITS
  3287. /*
  3288. * the PROGBITS section was not loaded by the obj_load
  3289. * now we can load them directly into the kernel memory
  3290. */
  3291. if (!obj_load_progbits(image, image_size, f, (char*)m_addr)) {
  3292. delete_module(m_name, 0);
  3293. goto out;
  3294. }
  3295. #endif
  3296. if (!obj_relocate(f, m_addr)) {
  3297. delete_module(m_name, 0);
  3298. goto out;
  3299. }
  3300. if (!new_init_module(m_name, f, m_size)) {
  3301. delete_module(m_name, 0);
  3302. goto out;
  3303. }
  3304. if (flag_print_load_map)
  3305. print_load_map(f);
  3306. exit_status = EXIT_SUCCESS;
  3307. out:
  3308. if (mmaped)
  3309. munmap(image, image_size);
  3310. else
  3311. free(image);
  3312. free(m_name);
  3313. return exit_status;
  3314. }