insmod.c 102 KB

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