modutils-24.c 95 KB

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