3
0

modutils-24.c 95 KB

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