insmod.c 103 KB

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