insmod.c 98 KB

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