3
0

modutils-24.c 94 KB

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