insmod.c 102 KB

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