elfload.c 116 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667366836693670367136723673367436753676367736783679368036813682368336843685368636873688368936903691369236933694369536963697369836993700370137023703370437053706370737083709371037113712371337143715371637173718371937203721372237233724372537263727372837293730373137323733373437353736373737383739374037413742374337443745374637473748374937503751375237533754375537563757375837593760376137623763376437653766376737683769377037713772377337743775377637773778377937803781378237833784378537863787378837893790379137923793379437953796379737983799380038013802380338043805380638073808380938103811381238133814381538163817381838193820382138223823382438253826
  1. /* This is the Linux kernel elf-loading code, ported into user space */
  2. #include "qemu/osdep.h"
  3. #include <sys/param.h>
  4. #include <sys/resource.h>
  5. #include <sys/shm.h>
  6. #include "qemu.h"
  7. #include "disas/disas.h"
  8. #include "qemu/path.h"
  9. #include "qemu/queue.h"
  10. #include "qemu/guest-random.h"
  11. #include "qemu/units.h"
  12. #include "qemu/selfmap.h"
  13. #ifdef _ARCH_PPC64
  14. #undef ARCH_DLINFO
  15. #undef ELF_PLATFORM
  16. #undef ELF_HWCAP
  17. #undef ELF_HWCAP2
  18. #undef ELF_CLASS
  19. #undef ELF_DATA
  20. #undef ELF_ARCH
  21. #endif
  22. #define ELF_OSABI ELFOSABI_SYSV
  23. /* from personality.h */
  24. /*
  25. * Flags for bug emulation.
  26. *
  27. * These occupy the top three bytes.
  28. */
  29. enum {
  30. ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
  31. FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
  32. descriptors (signal handling) */
  33. MMAP_PAGE_ZERO = 0x0100000,
  34. ADDR_COMPAT_LAYOUT = 0x0200000,
  35. READ_IMPLIES_EXEC = 0x0400000,
  36. ADDR_LIMIT_32BIT = 0x0800000,
  37. SHORT_INODE = 0x1000000,
  38. WHOLE_SECONDS = 0x2000000,
  39. STICKY_TIMEOUTS = 0x4000000,
  40. ADDR_LIMIT_3GB = 0x8000000,
  41. };
  42. /*
  43. * Personality types.
  44. *
  45. * These go in the low byte. Avoid using the top bit, it will
  46. * conflict with error returns.
  47. */
  48. enum {
  49. PER_LINUX = 0x0000,
  50. PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
  51. PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
  52. PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
  53. PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
  54. PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
  55. PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
  56. PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
  57. PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
  58. PER_BSD = 0x0006,
  59. PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
  60. PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
  61. PER_LINUX32 = 0x0008,
  62. PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
  63. PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
  64. PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
  65. PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
  66. PER_RISCOS = 0x000c,
  67. PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
  68. PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
  69. PER_OSF4 = 0x000f, /* OSF/1 v4 */
  70. PER_HPUX = 0x0010,
  71. PER_MASK = 0x00ff,
  72. };
  73. /*
  74. * Return the base personality without flags.
  75. */
  76. #define personality(pers) (pers & PER_MASK)
  77. int info_is_fdpic(struct image_info *info)
  78. {
  79. return info->personality == PER_LINUX_FDPIC;
  80. }
  81. /* this flag is uneffective under linux too, should be deleted */
  82. #ifndef MAP_DENYWRITE
  83. #define MAP_DENYWRITE 0
  84. #endif
  85. /* should probably go in elf.h */
  86. #ifndef ELIBBAD
  87. #define ELIBBAD 80
  88. #endif
  89. #ifdef TARGET_WORDS_BIGENDIAN
  90. #define ELF_DATA ELFDATA2MSB
  91. #else
  92. #define ELF_DATA ELFDATA2LSB
  93. #endif
  94. #ifdef TARGET_ABI_MIPSN32
  95. typedef abi_ullong target_elf_greg_t;
  96. #define tswapreg(ptr) tswap64(ptr)
  97. #else
  98. typedef abi_ulong target_elf_greg_t;
  99. #define tswapreg(ptr) tswapal(ptr)
  100. #endif
  101. #ifdef USE_UID16
  102. typedef abi_ushort target_uid_t;
  103. typedef abi_ushort target_gid_t;
  104. #else
  105. typedef abi_uint target_uid_t;
  106. typedef abi_uint target_gid_t;
  107. #endif
  108. typedef abi_int target_pid_t;
  109. #ifdef TARGET_I386
  110. #define ELF_PLATFORM get_elf_platform()
  111. static const char *get_elf_platform(void)
  112. {
  113. static char elf_platform[] = "i386";
  114. int family = object_property_get_int(OBJECT(thread_cpu), "family", NULL);
  115. if (family > 6)
  116. family = 6;
  117. if (family >= 3)
  118. elf_platform[1] = '0' + family;
  119. return elf_platform;
  120. }
  121. #define ELF_HWCAP get_elf_hwcap()
  122. static uint32_t get_elf_hwcap(void)
  123. {
  124. X86CPU *cpu = X86_CPU(thread_cpu);
  125. return cpu->env.features[FEAT_1_EDX];
  126. }
  127. #ifdef TARGET_X86_64
  128. #define ELF_START_MMAP 0x2aaaaab000ULL
  129. #define ELF_CLASS ELFCLASS64
  130. #define ELF_ARCH EM_X86_64
  131. static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
  132. {
  133. regs->rax = 0;
  134. regs->rsp = infop->start_stack;
  135. regs->rip = infop->entry;
  136. }
  137. #define ELF_NREG 27
  138. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  139. /*
  140. * Note that ELF_NREG should be 29 as there should be place for
  141. * TRAPNO and ERR "registers" as well but linux doesn't dump
  142. * those.
  143. *
  144. * See linux kernel: arch/x86/include/asm/elf.h
  145. */
  146. static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
  147. {
  148. (*regs)[0] = env->regs[15];
  149. (*regs)[1] = env->regs[14];
  150. (*regs)[2] = env->regs[13];
  151. (*regs)[3] = env->regs[12];
  152. (*regs)[4] = env->regs[R_EBP];
  153. (*regs)[5] = env->regs[R_EBX];
  154. (*regs)[6] = env->regs[11];
  155. (*regs)[7] = env->regs[10];
  156. (*regs)[8] = env->regs[9];
  157. (*regs)[9] = env->regs[8];
  158. (*regs)[10] = env->regs[R_EAX];
  159. (*regs)[11] = env->regs[R_ECX];
  160. (*regs)[12] = env->regs[R_EDX];
  161. (*regs)[13] = env->regs[R_ESI];
  162. (*regs)[14] = env->regs[R_EDI];
  163. (*regs)[15] = env->regs[R_EAX]; /* XXX */
  164. (*regs)[16] = env->eip;
  165. (*regs)[17] = env->segs[R_CS].selector & 0xffff;
  166. (*regs)[18] = env->eflags;
  167. (*regs)[19] = env->regs[R_ESP];
  168. (*regs)[20] = env->segs[R_SS].selector & 0xffff;
  169. (*regs)[21] = env->segs[R_FS].selector & 0xffff;
  170. (*regs)[22] = env->segs[R_GS].selector & 0xffff;
  171. (*regs)[23] = env->segs[R_DS].selector & 0xffff;
  172. (*regs)[24] = env->segs[R_ES].selector & 0xffff;
  173. (*regs)[25] = env->segs[R_FS].selector & 0xffff;
  174. (*regs)[26] = env->segs[R_GS].selector & 0xffff;
  175. }
  176. #else
  177. #define ELF_START_MMAP 0x80000000
  178. /*
  179. * This is used to ensure we don't load something for the wrong architecture.
  180. */
  181. #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
  182. /*
  183. * These are used to set parameters in the core dumps.
  184. */
  185. #define ELF_CLASS ELFCLASS32
  186. #define ELF_ARCH EM_386
  187. static inline void init_thread(struct target_pt_regs *regs,
  188. struct image_info *infop)
  189. {
  190. regs->esp = infop->start_stack;
  191. regs->eip = infop->entry;
  192. /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
  193. starts %edx contains a pointer to a function which might be
  194. registered using `atexit'. This provides a mean for the
  195. dynamic linker to call DT_FINI functions for shared libraries
  196. that have been loaded before the code runs.
  197. A value of 0 tells we have no such handler. */
  198. regs->edx = 0;
  199. }
  200. #define ELF_NREG 17
  201. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  202. /*
  203. * Note that ELF_NREG should be 19 as there should be place for
  204. * TRAPNO and ERR "registers" as well but linux doesn't dump
  205. * those.
  206. *
  207. * See linux kernel: arch/x86/include/asm/elf.h
  208. */
  209. static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
  210. {
  211. (*regs)[0] = env->regs[R_EBX];
  212. (*regs)[1] = env->regs[R_ECX];
  213. (*regs)[2] = env->regs[R_EDX];
  214. (*regs)[3] = env->regs[R_ESI];
  215. (*regs)[4] = env->regs[R_EDI];
  216. (*regs)[5] = env->regs[R_EBP];
  217. (*regs)[6] = env->regs[R_EAX];
  218. (*regs)[7] = env->segs[R_DS].selector & 0xffff;
  219. (*regs)[8] = env->segs[R_ES].selector & 0xffff;
  220. (*regs)[9] = env->segs[R_FS].selector & 0xffff;
  221. (*regs)[10] = env->segs[R_GS].selector & 0xffff;
  222. (*regs)[11] = env->regs[R_EAX]; /* XXX */
  223. (*regs)[12] = env->eip;
  224. (*regs)[13] = env->segs[R_CS].selector & 0xffff;
  225. (*regs)[14] = env->eflags;
  226. (*regs)[15] = env->regs[R_ESP];
  227. (*regs)[16] = env->segs[R_SS].selector & 0xffff;
  228. }
  229. #endif
  230. #define USE_ELF_CORE_DUMP
  231. #define ELF_EXEC_PAGESIZE 4096
  232. #endif
  233. #ifdef TARGET_ARM
  234. #ifndef TARGET_AARCH64
  235. /* 32 bit ARM definitions */
  236. #define ELF_START_MMAP 0x80000000
  237. #define ELF_ARCH EM_ARM
  238. #define ELF_CLASS ELFCLASS32
  239. static inline void init_thread(struct target_pt_regs *regs,
  240. struct image_info *infop)
  241. {
  242. abi_long stack = infop->start_stack;
  243. memset(regs, 0, sizeof(*regs));
  244. regs->uregs[16] = ARM_CPU_MODE_USR;
  245. if (infop->entry & 1) {
  246. regs->uregs[16] |= CPSR_T;
  247. }
  248. regs->uregs[15] = infop->entry & 0xfffffffe;
  249. regs->uregs[13] = infop->start_stack;
  250. /* FIXME - what to for failure of get_user()? */
  251. get_user_ual(regs->uregs[2], stack + 8); /* envp */
  252. get_user_ual(regs->uregs[1], stack + 4); /* envp */
  253. /* XXX: it seems that r0 is zeroed after ! */
  254. regs->uregs[0] = 0;
  255. /* For uClinux PIC binaries. */
  256. /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
  257. regs->uregs[10] = infop->start_data;
  258. /* Support ARM FDPIC. */
  259. if (info_is_fdpic(infop)) {
  260. /* As described in the ABI document, r7 points to the loadmap info
  261. * prepared by the kernel. If an interpreter is needed, r8 points
  262. * to the interpreter loadmap and r9 points to the interpreter
  263. * PT_DYNAMIC info. If no interpreter is needed, r8 is zero, and
  264. * r9 points to the main program PT_DYNAMIC info.
  265. */
  266. regs->uregs[7] = infop->loadmap_addr;
  267. if (infop->interpreter_loadmap_addr) {
  268. /* Executable is dynamically loaded. */
  269. regs->uregs[8] = infop->interpreter_loadmap_addr;
  270. regs->uregs[9] = infop->interpreter_pt_dynamic_addr;
  271. } else {
  272. regs->uregs[8] = 0;
  273. regs->uregs[9] = infop->pt_dynamic_addr;
  274. }
  275. }
  276. }
  277. #define ELF_NREG 18
  278. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  279. static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
  280. {
  281. (*regs)[0] = tswapreg(env->regs[0]);
  282. (*regs)[1] = tswapreg(env->regs[1]);
  283. (*regs)[2] = tswapreg(env->regs[2]);
  284. (*regs)[3] = tswapreg(env->regs[3]);
  285. (*regs)[4] = tswapreg(env->regs[4]);
  286. (*regs)[5] = tswapreg(env->regs[5]);
  287. (*regs)[6] = tswapreg(env->regs[6]);
  288. (*regs)[7] = tswapreg(env->regs[7]);
  289. (*regs)[8] = tswapreg(env->regs[8]);
  290. (*regs)[9] = tswapreg(env->regs[9]);
  291. (*regs)[10] = tswapreg(env->regs[10]);
  292. (*regs)[11] = tswapreg(env->regs[11]);
  293. (*regs)[12] = tswapreg(env->regs[12]);
  294. (*regs)[13] = tswapreg(env->regs[13]);
  295. (*regs)[14] = tswapreg(env->regs[14]);
  296. (*regs)[15] = tswapreg(env->regs[15]);
  297. (*regs)[16] = tswapreg(cpsr_read((CPUARMState *)env));
  298. (*regs)[17] = tswapreg(env->regs[0]); /* XXX */
  299. }
  300. #define USE_ELF_CORE_DUMP
  301. #define ELF_EXEC_PAGESIZE 4096
  302. enum
  303. {
  304. ARM_HWCAP_ARM_SWP = 1 << 0,
  305. ARM_HWCAP_ARM_HALF = 1 << 1,
  306. ARM_HWCAP_ARM_THUMB = 1 << 2,
  307. ARM_HWCAP_ARM_26BIT = 1 << 3,
  308. ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
  309. ARM_HWCAP_ARM_FPA = 1 << 5,
  310. ARM_HWCAP_ARM_VFP = 1 << 6,
  311. ARM_HWCAP_ARM_EDSP = 1 << 7,
  312. ARM_HWCAP_ARM_JAVA = 1 << 8,
  313. ARM_HWCAP_ARM_IWMMXT = 1 << 9,
  314. ARM_HWCAP_ARM_CRUNCH = 1 << 10,
  315. ARM_HWCAP_ARM_THUMBEE = 1 << 11,
  316. ARM_HWCAP_ARM_NEON = 1 << 12,
  317. ARM_HWCAP_ARM_VFPv3 = 1 << 13,
  318. ARM_HWCAP_ARM_VFPv3D16 = 1 << 14,
  319. ARM_HWCAP_ARM_TLS = 1 << 15,
  320. ARM_HWCAP_ARM_VFPv4 = 1 << 16,
  321. ARM_HWCAP_ARM_IDIVA = 1 << 17,
  322. ARM_HWCAP_ARM_IDIVT = 1 << 18,
  323. ARM_HWCAP_ARM_VFPD32 = 1 << 19,
  324. ARM_HWCAP_ARM_LPAE = 1 << 20,
  325. ARM_HWCAP_ARM_EVTSTRM = 1 << 21,
  326. };
  327. enum {
  328. ARM_HWCAP2_ARM_AES = 1 << 0,
  329. ARM_HWCAP2_ARM_PMULL = 1 << 1,
  330. ARM_HWCAP2_ARM_SHA1 = 1 << 2,
  331. ARM_HWCAP2_ARM_SHA2 = 1 << 3,
  332. ARM_HWCAP2_ARM_CRC32 = 1 << 4,
  333. };
  334. /* The commpage only exists for 32 bit kernels */
  335. #define ARM_COMMPAGE (intptr_t)0xffff0f00u
  336. static bool init_guest_commpage(void)
  337. {
  338. void *want = g2h(ARM_COMMPAGE & -qemu_host_page_size);
  339. void *addr = mmap(want, qemu_host_page_size, PROT_READ | PROT_WRITE,
  340. MAP_ANONYMOUS | MAP_PRIVATE | MAP_FIXED, -1, 0);
  341. if (addr == MAP_FAILED) {
  342. perror("Allocating guest commpage");
  343. exit(EXIT_FAILURE);
  344. }
  345. if (addr != want) {
  346. return false;
  347. }
  348. /* Set kernel helper versions; rest of page is 0. */
  349. __put_user(5, (uint32_t *)g2h(0xffff0ffcu));
  350. if (mprotect(addr, qemu_host_page_size, PROT_READ)) {
  351. perror("Protecting guest commpage");
  352. exit(EXIT_FAILURE);
  353. }
  354. return true;
  355. }
  356. #define ELF_HWCAP get_elf_hwcap()
  357. #define ELF_HWCAP2 get_elf_hwcap2()
  358. static uint32_t get_elf_hwcap(void)
  359. {
  360. ARMCPU *cpu = ARM_CPU(thread_cpu);
  361. uint32_t hwcaps = 0;
  362. hwcaps |= ARM_HWCAP_ARM_SWP;
  363. hwcaps |= ARM_HWCAP_ARM_HALF;
  364. hwcaps |= ARM_HWCAP_ARM_THUMB;
  365. hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
  366. /* probe for the extra features */
  367. #define GET_FEATURE(feat, hwcap) \
  368. do { if (arm_feature(&cpu->env, feat)) { hwcaps |= hwcap; } } while (0)
  369. #define GET_FEATURE_ID(feat, hwcap) \
  370. do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
  371. /* EDSP is in v5TE and above, but all our v5 CPUs are v5TE */
  372. GET_FEATURE(ARM_FEATURE_V5, ARM_HWCAP_ARM_EDSP);
  373. GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
  374. GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
  375. GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
  376. GET_FEATURE(ARM_FEATURE_V6K, ARM_HWCAP_ARM_TLS);
  377. GET_FEATURE(ARM_FEATURE_LPAE, ARM_HWCAP_ARM_LPAE);
  378. GET_FEATURE_ID(aa32_arm_div, ARM_HWCAP_ARM_IDIVA);
  379. GET_FEATURE_ID(aa32_thumb_div, ARM_HWCAP_ARM_IDIVT);
  380. GET_FEATURE_ID(aa32_vfp, ARM_HWCAP_ARM_VFP);
  381. if (cpu_isar_feature(aa32_fpsp_v3, cpu) ||
  382. cpu_isar_feature(aa32_fpdp_v3, cpu)) {
  383. hwcaps |= ARM_HWCAP_ARM_VFPv3;
  384. if (cpu_isar_feature(aa32_simd_r32, cpu)) {
  385. hwcaps |= ARM_HWCAP_ARM_VFPD32;
  386. } else {
  387. hwcaps |= ARM_HWCAP_ARM_VFPv3D16;
  388. }
  389. }
  390. GET_FEATURE_ID(aa32_simdfmac, ARM_HWCAP_ARM_VFPv4);
  391. return hwcaps;
  392. }
  393. static uint32_t get_elf_hwcap2(void)
  394. {
  395. ARMCPU *cpu = ARM_CPU(thread_cpu);
  396. uint32_t hwcaps = 0;
  397. GET_FEATURE_ID(aa32_aes, ARM_HWCAP2_ARM_AES);
  398. GET_FEATURE_ID(aa32_pmull, ARM_HWCAP2_ARM_PMULL);
  399. GET_FEATURE_ID(aa32_sha1, ARM_HWCAP2_ARM_SHA1);
  400. GET_FEATURE_ID(aa32_sha2, ARM_HWCAP2_ARM_SHA2);
  401. GET_FEATURE_ID(aa32_crc32, ARM_HWCAP2_ARM_CRC32);
  402. return hwcaps;
  403. }
  404. #undef GET_FEATURE
  405. #undef GET_FEATURE_ID
  406. #define ELF_PLATFORM get_elf_platform()
  407. static const char *get_elf_platform(void)
  408. {
  409. CPUARMState *env = thread_cpu->env_ptr;
  410. #ifdef TARGET_WORDS_BIGENDIAN
  411. # define END "b"
  412. #else
  413. # define END "l"
  414. #endif
  415. if (arm_feature(env, ARM_FEATURE_V8)) {
  416. return "v8" END;
  417. } else if (arm_feature(env, ARM_FEATURE_V7)) {
  418. if (arm_feature(env, ARM_FEATURE_M)) {
  419. return "v7m" END;
  420. } else {
  421. return "v7" END;
  422. }
  423. } else if (arm_feature(env, ARM_FEATURE_V6)) {
  424. return "v6" END;
  425. } else if (arm_feature(env, ARM_FEATURE_V5)) {
  426. return "v5" END;
  427. } else {
  428. return "v4" END;
  429. }
  430. #undef END
  431. }
  432. #else
  433. /* 64 bit ARM definitions */
  434. #define ELF_START_MMAP 0x80000000
  435. #define ELF_ARCH EM_AARCH64
  436. #define ELF_CLASS ELFCLASS64
  437. #ifdef TARGET_WORDS_BIGENDIAN
  438. # define ELF_PLATFORM "aarch64_be"
  439. #else
  440. # define ELF_PLATFORM "aarch64"
  441. #endif
  442. static inline void init_thread(struct target_pt_regs *regs,
  443. struct image_info *infop)
  444. {
  445. abi_long stack = infop->start_stack;
  446. memset(regs, 0, sizeof(*regs));
  447. regs->pc = infop->entry & ~0x3ULL;
  448. regs->sp = stack;
  449. }
  450. #define ELF_NREG 34
  451. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  452. static void elf_core_copy_regs(target_elf_gregset_t *regs,
  453. const CPUARMState *env)
  454. {
  455. int i;
  456. for (i = 0; i < 32; i++) {
  457. (*regs)[i] = tswapreg(env->xregs[i]);
  458. }
  459. (*regs)[32] = tswapreg(env->pc);
  460. (*regs)[33] = tswapreg(pstate_read((CPUARMState *)env));
  461. }
  462. #define USE_ELF_CORE_DUMP
  463. #define ELF_EXEC_PAGESIZE 4096
  464. enum {
  465. ARM_HWCAP_A64_FP = 1 << 0,
  466. ARM_HWCAP_A64_ASIMD = 1 << 1,
  467. ARM_HWCAP_A64_EVTSTRM = 1 << 2,
  468. ARM_HWCAP_A64_AES = 1 << 3,
  469. ARM_HWCAP_A64_PMULL = 1 << 4,
  470. ARM_HWCAP_A64_SHA1 = 1 << 5,
  471. ARM_HWCAP_A64_SHA2 = 1 << 6,
  472. ARM_HWCAP_A64_CRC32 = 1 << 7,
  473. ARM_HWCAP_A64_ATOMICS = 1 << 8,
  474. ARM_HWCAP_A64_FPHP = 1 << 9,
  475. ARM_HWCAP_A64_ASIMDHP = 1 << 10,
  476. ARM_HWCAP_A64_CPUID = 1 << 11,
  477. ARM_HWCAP_A64_ASIMDRDM = 1 << 12,
  478. ARM_HWCAP_A64_JSCVT = 1 << 13,
  479. ARM_HWCAP_A64_FCMA = 1 << 14,
  480. ARM_HWCAP_A64_LRCPC = 1 << 15,
  481. ARM_HWCAP_A64_DCPOP = 1 << 16,
  482. ARM_HWCAP_A64_SHA3 = 1 << 17,
  483. ARM_HWCAP_A64_SM3 = 1 << 18,
  484. ARM_HWCAP_A64_SM4 = 1 << 19,
  485. ARM_HWCAP_A64_ASIMDDP = 1 << 20,
  486. ARM_HWCAP_A64_SHA512 = 1 << 21,
  487. ARM_HWCAP_A64_SVE = 1 << 22,
  488. ARM_HWCAP_A64_ASIMDFHM = 1 << 23,
  489. ARM_HWCAP_A64_DIT = 1 << 24,
  490. ARM_HWCAP_A64_USCAT = 1 << 25,
  491. ARM_HWCAP_A64_ILRCPC = 1 << 26,
  492. ARM_HWCAP_A64_FLAGM = 1 << 27,
  493. ARM_HWCAP_A64_SSBS = 1 << 28,
  494. ARM_HWCAP_A64_SB = 1 << 29,
  495. ARM_HWCAP_A64_PACA = 1 << 30,
  496. ARM_HWCAP_A64_PACG = 1UL << 31,
  497. ARM_HWCAP2_A64_DCPODP = 1 << 0,
  498. ARM_HWCAP2_A64_SVE2 = 1 << 1,
  499. ARM_HWCAP2_A64_SVEAES = 1 << 2,
  500. ARM_HWCAP2_A64_SVEPMULL = 1 << 3,
  501. ARM_HWCAP2_A64_SVEBITPERM = 1 << 4,
  502. ARM_HWCAP2_A64_SVESHA3 = 1 << 5,
  503. ARM_HWCAP2_A64_SVESM4 = 1 << 6,
  504. ARM_HWCAP2_A64_FLAGM2 = 1 << 7,
  505. ARM_HWCAP2_A64_FRINT = 1 << 8,
  506. };
  507. #define ELF_HWCAP get_elf_hwcap()
  508. #define ELF_HWCAP2 get_elf_hwcap2()
  509. #define GET_FEATURE_ID(feat, hwcap) \
  510. do { if (cpu_isar_feature(feat, cpu)) { hwcaps |= hwcap; } } while (0)
  511. static uint32_t get_elf_hwcap(void)
  512. {
  513. ARMCPU *cpu = ARM_CPU(thread_cpu);
  514. uint32_t hwcaps = 0;
  515. hwcaps |= ARM_HWCAP_A64_FP;
  516. hwcaps |= ARM_HWCAP_A64_ASIMD;
  517. hwcaps |= ARM_HWCAP_A64_CPUID;
  518. /* probe for the extra features */
  519. GET_FEATURE_ID(aa64_aes, ARM_HWCAP_A64_AES);
  520. GET_FEATURE_ID(aa64_pmull, ARM_HWCAP_A64_PMULL);
  521. GET_FEATURE_ID(aa64_sha1, ARM_HWCAP_A64_SHA1);
  522. GET_FEATURE_ID(aa64_sha256, ARM_HWCAP_A64_SHA2);
  523. GET_FEATURE_ID(aa64_sha512, ARM_HWCAP_A64_SHA512);
  524. GET_FEATURE_ID(aa64_crc32, ARM_HWCAP_A64_CRC32);
  525. GET_FEATURE_ID(aa64_sha3, ARM_HWCAP_A64_SHA3);
  526. GET_FEATURE_ID(aa64_sm3, ARM_HWCAP_A64_SM3);
  527. GET_FEATURE_ID(aa64_sm4, ARM_HWCAP_A64_SM4);
  528. GET_FEATURE_ID(aa64_fp16, ARM_HWCAP_A64_FPHP | ARM_HWCAP_A64_ASIMDHP);
  529. GET_FEATURE_ID(aa64_atomics, ARM_HWCAP_A64_ATOMICS);
  530. GET_FEATURE_ID(aa64_rdm, ARM_HWCAP_A64_ASIMDRDM);
  531. GET_FEATURE_ID(aa64_dp, ARM_HWCAP_A64_ASIMDDP);
  532. GET_FEATURE_ID(aa64_fcma, ARM_HWCAP_A64_FCMA);
  533. GET_FEATURE_ID(aa64_sve, ARM_HWCAP_A64_SVE);
  534. GET_FEATURE_ID(aa64_pauth, ARM_HWCAP_A64_PACA | ARM_HWCAP_A64_PACG);
  535. GET_FEATURE_ID(aa64_fhm, ARM_HWCAP_A64_ASIMDFHM);
  536. GET_FEATURE_ID(aa64_jscvt, ARM_HWCAP_A64_JSCVT);
  537. GET_FEATURE_ID(aa64_sb, ARM_HWCAP_A64_SB);
  538. GET_FEATURE_ID(aa64_condm_4, ARM_HWCAP_A64_FLAGM);
  539. GET_FEATURE_ID(aa64_dcpop, ARM_HWCAP_A64_DCPOP);
  540. GET_FEATURE_ID(aa64_rcpc_8_3, ARM_HWCAP_A64_LRCPC);
  541. GET_FEATURE_ID(aa64_rcpc_8_4, ARM_HWCAP_A64_ILRCPC);
  542. return hwcaps;
  543. }
  544. static uint32_t get_elf_hwcap2(void)
  545. {
  546. ARMCPU *cpu = ARM_CPU(thread_cpu);
  547. uint32_t hwcaps = 0;
  548. GET_FEATURE_ID(aa64_dcpodp, ARM_HWCAP2_A64_DCPODP);
  549. GET_FEATURE_ID(aa64_condm_5, ARM_HWCAP2_A64_FLAGM2);
  550. GET_FEATURE_ID(aa64_frint, ARM_HWCAP2_A64_FRINT);
  551. return hwcaps;
  552. }
  553. #undef GET_FEATURE_ID
  554. #endif /* not TARGET_AARCH64 */
  555. #endif /* TARGET_ARM */
  556. #ifdef TARGET_SPARC
  557. #ifdef TARGET_SPARC64
  558. #define ELF_START_MMAP 0x80000000
  559. #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
  560. | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
  561. #ifndef TARGET_ABI32
  562. #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
  563. #else
  564. #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
  565. #endif
  566. #define ELF_CLASS ELFCLASS64
  567. #define ELF_ARCH EM_SPARCV9
  568. #define STACK_BIAS 2047
  569. static inline void init_thread(struct target_pt_regs *regs,
  570. struct image_info *infop)
  571. {
  572. #ifndef TARGET_ABI32
  573. regs->tstate = 0;
  574. #endif
  575. regs->pc = infop->entry;
  576. regs->npc = regs->pc + 4;
  577. regs->y = 0;
  578. #ifdef TARGET_ABI32
  579. regs->u_regs[14] = infop->start_stack - 16 * 4;
  580. #else
  581. if (personality(infop->personality) == PER_LINUX32)
  582. regs->u_regs[14] = infop->start_stack - 16 * 4;
  583. else
  584. regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
  585. #endif
  586. }
  587. #else
  588. #define ELF_START_MMAP 0x80000000
  589. #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
  590. | HWCAP_SPARC_MULDIV)
  591. #define ELF_CLASS ELFCLASS32
  592. #define ELF_ARCH EM_SPARC
  593. static inline void init_thread(struct target_pt_regs *regs,
  594. struct image_info *infop)
  595. {
  596. regs->psr = 0;
  597. regs->pc = infop->entry;
  598. regs->npc = regs->pc + 4;
  599. regs->y = 0;
  600. regs->u_regs[14] = infop->start_stack - 16 * 4;
  601. }
  602. #endif
  603. #endif
  604. #ifdef TARGET_PPC
  605. #define ELF_MACHINE PPC_ELF_MACHINE
  606. #define ELF_START_MMAP 0x80000000
  607. #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
  608. #define elf_check_arch(x) ( (x) == EM_PPC64 )
  609. #define ELF_CLASS ELFCLASS64
  610. #else
  611. #define ELF_CLASS ELFCLASS32
  612. #endif
  613. #define ELF_ARCH EM_PPC
  614. /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
  615. See arch/powerpc/include/asm/cputable.h. */
  616. enum {
  617. QEMU_PPC_FEATURE_32 = 0x80000000,
  618. QEMU_PPC_FEATURE_64 = 0x40000000,
  619. QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
  620. QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
  621. QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
  622. QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
  623. QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
  624. QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
  625. QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
  626. QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
  627. QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
  628. QEMU_PPC_FEATURE_NO_TB = 0x00100000,
  629. QEMU_PPC_FEATURE_POWER4 = 0x00080000,
  630. QEMU_PPC_FEATURE_POWER5 = 0x00040000,
  631. QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
  632. QEMU_PPC_FEATURE_CELL = 0x00010000,
  633. QEMU_PPC_FEATURE_BOOKE = 0x00008000,
  634. QEMU_PPC_FEATURE_SMT = 0x00004000,
  635. QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
  636. QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
  637. QEMU_PPC_FEATURE_PA6T = 0x00000800,
  638. QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
  639. QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
  640. QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
  641. QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
  642. QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
  643. QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
  644. QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
  645. /* Feature definitions in AT_HWCAP2. */
  646. QEMU_PPC_FEATURE2_ARCH_2_07 = 0x80000000, /* ISA 2.07 */
  647. QEMU_PPC_FEATURE2_HAS_HTM = 0x40000000, /* Hardware Transactional Memory */
  648. QEMU_PPC_FEATURE2_HAS_DSCR = 0x20000000, /* Data Stream Control Register */
  649. QEMU_PPC_FEATURE2_HAS_EBB = 0x10000000, /* Event Base Branching */
  650. QEMU_PPC_FEATURE2_HAS_ISEL = 0x08000000, /* Integer Select */
  651. QEMU_PPC_FEATURE2_HAS_TAR = 0x04000000, /* Target Address Register */
  652. QEMU_PPC_FEATURE2_VEC_CRYPTO = 0x02000000,
  653. QEMU_PPC_FEATURE2_HTM_NOSC = 0x01000000,
  654. QEMU_PPC_FEATURE2_ARCH_3_00 = 0x00800000, /* ISA 3.00 */
  655. QEMU_PPC_FEATURE2_HAS_IEEE128 = 0x00400000, /* VSX IEEE Bin Float 128-bit */
  656. QEMU_PPC_FEATURE2_DARN = 0x00200000, /* darn random number insn */
  657. QEMU_PPC_FEATURE2_SCV = 0x00100000, /* scv syscall */
  658. QEMU_PPC_FEATURE2_HTM_NO_SUSPEND = 0x00080000, /* TM w/o suspended state */
  659. };
  660. #define ELF_HWCAP get_elf_hwcap()
  661. static uint32_t get_elf_hwcap(void)
  662. {
  663. PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
  664. uint32_t features = 0;
  665. /* We don't have to be terribly complete here; the high points are
  666. Altivec/FP/SPE support. Anything else is just a bonus. */
  667. #define GET_FEATURE(flag, feature) \
  668. do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
  669. #define GET_FEATURE2(flags, feature) \
  670. do { \
  671. if ((cpu->env.insns_flags2 & flags) == flags) { \
  672. features |= feature; \
  673. } \
  674. } while (0)
  675. GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
  676. GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
  677. GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
  678. GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
  679. GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
  680. GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
  681. GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
  682. GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
  683. GET_FEATURE2(PPC2_DFP, QEMU_PPC_FEATURE_HAS_DFP);
  684. GET_FEATURE2(PPC2_VSX, QEMU_PPC_FEATURE_HAS_VSX);
  685. GET_FEATURE2((PPC2_PERM_ISA206 | PPC2_DIVE_ISA206 | PPC2_ATOMIC_ISA206 |
  686. PPC2_FP_CVT_ISA206 | PPC2_FP_TST_ISA206),
  687. QEMU_PPC_FEATURE_ARCH_2_06);
  688. #undef GET_FEATURE
  689. #undef GET_FEATURE2
  690. return features;
  691. }
  692. #define ELF_HWCAP2 get_elf_hwcap2()
  693. static uint32_t get_elf_hwcap2(void)
  694. {
  695. PowerPCCPU *cpu = POWERPC_CPU(thread_cpu);
  696. uint32_t features = 0;
  697. #define GET_FEATURE(flag, feature) \
  698. do { if (cpu->env.insns_flags & flag) { features |= feature; } } while (0)
  699. #define GET_FEATURE2(flag, feature) \
  700. do { if (cpu->env.insns_flags2 & flag) { features |= feature; } } while (0)
  701. GET_FEATURE(PPC_ISEL, QEMU_PPC_FEATURE2_HAS_ISEL);
  702. GET_FEATURE2(PPC2_BCTAR_ISA207, QEMU_PPC_FEATURE2_HAS_TAR);
  703. GET_FEATURE2((PPC2_BCTAR_ISA207 | PPC2_LSQ_ISA207 | PPC2_ALTIVEC_207 |
  704. PPC2_ISA207S), QEMU_PPC_FEATURE2_ARCH_2_07 |
  705. QEMU_PPC_FEATURE2_VEC_CRYPTO);
  706. GET_FEATURE2(PPC2_ISA300, QEMU_PPC_FEATURE2_ARCH_3_00 |
  707. QEMU_PPC_FEATURE2_DARN);
  708. #undef GET_FEATURE
  709. #undef GET_FEATURE2
  710. return features;
  711. }
  712. /*
  713. * The requirements here are:
  714. * - keep the final alignment of sp (sp & 0xf)
  715. * - make sure the 32-bit value at the first 16 byte aligned position of
  716. * AUXV is greater than 16 for glibc compatibility.
  717. * AT_IGNOREPPC is used for that.
  718. * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
  719. * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
  720. */
  721. #define DLINFO_ARCH_ITEMS 5
  722. #define ARCH_DLINFO \
  723. do { \
  724. PowerPCCPU *cpu = POWERPC_CPU(thread_cpu); \
  725. /* \
  726. * Handle glibc compatibility: these magic entries must \
  727. * be at the lowest addresses in the final auxv. \
  728. */ \
  729. NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
  730. NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
  731. NEW_AUX_ENT(AT_DCACHEBSIZE, cpu->env.dcache_line_size); \
  732. NEW_AUX_ENT(AT_ICACHEBSIZE, cpu->env.icache_line_size); \
  733. NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
  734. } while (0)
  735. static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
  736. {
  737. _regs->gpr[1] = infop->start_stack;
  738. #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
  739. if (get_ppc64_abi(infop) < 2) {
  740. uint64_t val;
  741. get_user_u64(val, infop->entry + 8);
  742. _regs->gpr[2] = val + infop->load_bias;
  743. get_user_u64(val, infop->entry);
  744. infop->entry = val + infop->load_bias;
  745. } else {
  746. _regs->gpr[12] = infop->entry; /* r12 set to global entry address */
  747. }
  748. #endif
  749. _regs->nip = infop->entry;
  750. }
  751. /* See linux kernel: arch/powerpc/include/asm/elf.h. */
  752. #define ELF_NREG 48
  753. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  754. static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
  755. {
  756. int i;
  757. target_ulong ccr = 0;
  758. for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
  759. (*regs)[i] = tswapreg(env->gpr[i]);
  760. }
  761. (*regs)[32] = tswapreg(env->nip);
  762. (*regs)[33] = tswapreg(env->msr);
  763. (*regs)[35] = tswapreg(env->ctr);
  764. (*regs)[36] = tswapreg(env->lr);
  765. (*regs)[37] = tswapreg(env->xer);
  766. for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
  767. ccr |= env->crf[i] << (32 - ((i + 1) * 4));
  768. }
  769. (*regs)[38] = tswapreg(ccr);
  770. }
  771. #define USE_ELF_CORE_DUMP
  772. #define ELF_EXEC_PAGESIZE 4096
  773. #endif
  774. #ifdef TARGET_MIPS
  775. #define ELF_START_MMAP 0x80000000
  776. #ifdef TARGET_MIPS64
  777. #define ELF_CLASS ELFCLASS64
  778. #else
  779. #define ELF_CLASS ELFCLASS32
  780. #endif
  781. #define ELF_ARCH EM_MIPS
  782. #define elf_check_arch(x) ((x) == EM_MIPS || (x) == EM_NANOMIPS)
  783. static inline void init_thread(struct target_pt_regs *regs,
  784. struct image_info *infop)
  785. {
  786. regs->cp0_status = 2 << CP0St_KSU;
  787. regs->cp0_epc = infop->entry;
  788. regs->regs[29] = infop->start_stack;
  789. }
  790. /* See linux kernel: arch/mips/include/asm/elf.h. */
  791. #define ELF_NREG 45
  792. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  793. /* See linux kernel: arch/mips/include/asm/reg.h. */
  794. enum {
  795. #ifdef TARGET_MIPS64
  796. TARGET_EF_R0 = 0,
  797. #else
  798. TARGET_EF_R0 = 6,
  799. #endif
  800. TARGET_EF_R26 = TARGET_EF_R0 + 26,
  801. TARGET_EF_R27 = TARGET_EF_R0 + 27,
  802. TARGET_EF_LO = TARGET_EF_R0 + 32,
  803. TARGET_EF_HI = TARGET_EF_R0 + 33,
  804. TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
  805. TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
  806. TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
  807. TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
  808. };
  809. /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
  810. static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
  811. {
  812. int i;
  813. for (i = 0; i < TARGET_EF_R0; i++) {
  814. (*regs)[i] = 0;
  815. }
  816. (*regs)[TARGET_EF_R0] = 0;
  817. for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
  818. (*regs)[TARGET_EF_R0 + i] = tswapreg(env->active_tc.gpr[i]);
  819. }
  820. (*regs)[TARGET_EF_R26] = 0;
  821. (*regs)[TARGET_EF_R27] = 0;
  822. (*regs)[TARGET_EF_LO] = tswapreg(env->active_tc.LO[0]);
  823. (*regs)[TARGET_EF_HI] = tswapreg(env->active_tc.HI[0]);
  824. (*regs)[TARGET_EF_CP0_EPC] = tswapreg(env->active_tc.PC);
  825. (*regs)[TARGET_EF_CP0_BADVADDR] = tswapreg(env->CP0_BadVAddr);
  826. (*regs)[TARGET_EF_CP0_STATUS] = tswapreg(env->CP0_Status);
  827. (*regs)[TARGET_EF_CP0_CAUSE] = tswapreg(env->CP0_Cause);
  828. }
  829. #define USE_ELF_CORE_DUMP
  830. #define ELF_EXEC_PAGESIZE 4096
  831. /* See arch/mips/include/uapi/asm/hwcap.h. */
  832. enum {
  833. HWCAP_MIPS_R6 = (1 << 0),
  834. HWCAP_MIPS_MSA = (1 << 1),
  835. };
  836. #define ELF_HWCAP get_elf_hwcap()
  837. static uint32_t get_elf_hwcap(void)
  838. {
  839. MIPSCPU *cpu = MIPS_CPU(thread_cpu);
  840. uint32_t hwcaps = 0;
  841. #define GET_FEATURE(flag, hwcap) \
  842. do { if (cpu->env.insn_flags & (flag)) { hwcaps |= hwcap; } } while (0)
  843. GET_FEATURE(ISA_MIPS32R6 | ISA_MIPS64R6, HWCAP_MIPS_R6);
  844. GET_FEATURE(ASE_MSA, HWCAP_MIPS_MSA);
  845. #undef GET_FEATURE
  846. return hwcaps;
  847. }
  848. #endif /* TARGET_MIPS */
  849. #ifdef TARGET_MICROBLAZE
  850. #define ELF_START_MMAP 0x80000000
  851. #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
  852. #define ELF_CLASS ELFCLASS32
  853. #define ELF_ARCH EM_MICROBLAZE
  854. static inline void init_thread(struct target_pt_regs *regs,
  855. struct image_info *infop)
  856. {
  857. regs->pc = infop->entry;
  858. regs->r1 = infop->start_stack;
  859. }
  860. #define ELF_EXEC_PAGESIZE 4096
  861. #define USE_ELF_CORE_DUMP
  862. #define ELF_NREG 38
  863. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  864. /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
  865. static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
  866. {
  867. int i, pos = 0;
  868. for (i = 0; i < 32; i++) {
  869. (*regs)[pos++] = tswapreg(env->regs[i]);
  870. }
  871. for (i = 0; i < 6; i++) {
  872. (*regs)[pos++] = tswapreg(env->sregs[i]);
  873. }
  874. }
  875. #endif /* TARGET_MICROBLAZE */
  876. #ifdef TARGET_NIOS2
  877. #define ELF_START_MMAP 0x80000000
  878. #define elf_check_arch(x) ((x) == EM_ALTERA_NIOS2)
  879. #define ELF_CLASS ELFCLASS32
  880. #define ELF_ARCH EM_ALTERA_NIOS2
  881. static void init_thread(struct target_pt_regs *regs, struct image_info *infop)
  882. {
  883. regs->ea = infop->entry;
  884. regs->sp = infop->start_stack;
  885. regs->estatus = 0x3;
  886. }
  887. #define ELF_EXEC_PAGESIZE 4096
  888. #define USE_ELF_CORE_DUMP
  889. #define ELF_NREG 49
  890. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  891. /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
  892. static void elf_core_copy_regs(target_elf_gregset_t *regs,
  893. const CPUNios2State *env)
  894. {
  895. int i;
  896. (*regs)[0] = -1;
  897. for (i = 1; i < 8; i++) /* r0-r7 */
  898. (*regs)[i] = tswapreg(env->regs[i + 7]);
  899. for (i = 8; i < 16; i++) /* r8-r15 */
  900. (*regs)[i] = tswapreg(env->regs[i - 8]);
  901. for (i = 16; i < 24; i++) /* r16-r23 */
  902. (*regs)[i] = tswapreg(env->regs[i + 7]);
  903. (*regs)[24] = -1; /* R_ET */
  904. (*regs)[25] = -1; /* R_BT */
  905. (*regs)[26] = tswapreg(env->regs[R_GP]);
  906. (*regs)[27] = tswapreg(env->regs[R_SP]);
  907. (*regs)[28] = tswapreg(env->regs[R_FP]);
  908. (*regs)[29] = tswapreg(env->regs[R_EA]);
  909. (*regs)[30] = -1; /* R_SSTATUS */
  910. (*regs)[31] = tswapreg(env->regs[R_RA]);
  911. (*regs)[32] = tswapreg(env->regs[R_PC]);
  912. (*regs)[33] = -1; /* R_STATUS */
  913. (*regs)[34] = tswapreg(env->regs[CR_ESTATUS]);
  914. for (i = 35; i < 49; i++) /* ... */
  915. (*regs)[i] = -1;
  916. }
  917. #endif /* TARGET_NIOS2 */
  918. #ifdef TARGET_OPENRISC
  919. #define ELF_START_MMAP 0x08000000
  920. #define ELF_ARCH EM_OPENRISC
  921. #define ELF_CLASS ELFCLASS32
  922. #define ELF_DATA ELFDATA2MSB
  923. static inline void init_thread(struct target_pt_regs *regs,
  924. struct image_info *infop)
  925. {
  926. regs->pc = infop->entry;
  927. regs->gpr[1] = infop->start_stack;
  928. }
  929. #define USE_ELF_CORE_DUMP
  930. #define ELF_EXEC_PAGESIZE 8192
  931. /* See linux kernel arch/openrisc/include/asm/elf.h. */
  932. #define ELF_NREG 34 /* gprs and pc, sr */
  933. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  934. static void elf_core_copy_regs(target_elf_gregset_t *regs,
  935. const CPUOpenRISCState *env)
  936. {
  937. int i;
  938. for (i = 0; i < 32; i++) {
  939. (*regs)[i] = tswapreg(cpu_get_gpr(env, i));
  940. }
  941. (*regs)[32] = tswapreg(env->pc);
  942. (*regs)[33] = tswapreg(cpu_get_sr(env));
  943. }
  944. #define ELF_HWCAP 0
  945. #define ELF_PLATFORM NULL
  946. #endif /* TARGET_OPENRISC */
  947. #ifdef TARGET_SH4
  948. #define ELF_START_MMAP 0x80000000
  949. #define ELF_CLASS ELFCLASS32
  950. #define ELF_ARCH EM_SH
  951. static inline void init_thread(struct target_pt_regs *regs,
  952. struct image_info *infop)
  953. {
  954. /* Check other registers XXXXX */
  955. regs->pc = infop->entry;
  956. regs->regs[15] = infop->start_stack;
  957. }
  958. /* See linux kernel: arch/sh/include/asm/elf.h. */
  959. #define ELF_NREG 23
  960. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  961. /* See linux kernel: arch/sh/include/asm/ptrace.h. */
  962. enum {
  963. TARGET_REG_PC = 16,
  964. TARGET_REG_PR = 17,
  965. TARGET_REG_SR = 18,
  966. TARGET_REG_GBR = 19,
  967. TARGET_REG_MACH = 20,
  968. TARGET_REG_MACL = 21,
  969. TARGET_REG_SYSCALL = 22
  970. };
  971. static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
  972. const CPUSH4State *env)
  973. {
  974. int i;
  975. for (i = 0; i < 16; i++) {
  976. (*regs)[i] = tswapreg(env->gregs[i]);
  977. }
  978. (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
  979. (*regs)[TARGET_REG_PR] = tswapreg(env->pr);
  980. (*regs)[TARGET_REG_SR] = tswapreg(env->sr);
  981. (*regs)[TARGET_REG_GBR] = tswapreg(env->gbr);
  982. (*regs)[TARGET_REG_MACH] = tswapreg(env->mach);
  983. (*regs)[TARGET_REG_MACL] = tswapreg(env->macl);
  984. (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
  985. }
  986. #define USE_ELF_CORE_DUMP
  987. #define ELF_EXEC_PAGESIZE 4096
  988. enum {
  989. SH_CPU_HAS_FPU = 0x0001, /* Hardware FPU support */
  990. SH_CPU_HAS_P2_FLUSH_BUG = 0x0002, /* Need to flush the cache in P2 area */
  991. SH_CPU_HAS_MMU_PAGE_ASSOC = 0x0004, /* SH3: TLB way selection bit support */
  992. SH_CPU_HAS_DSP = 0x0008, /* SH-DSP: DSP support */
  993. SH_CPU_HAS_PERF_COUNTER = 0x0010, /* Hardware performance counters */
  994. SH_CPU_HAS_PTEA = 0x0020, /* PTEA register */
  995. SH_CPU_HAS_LLSC = 0x0040, /* movli.l/movco.l */
  996. SH_CPU_HAS_L2_CACHE = 0x0080, /* Secondary cache / URAM */
  997. SH_CPU_HAS_OP32 = 0x0100, /* 32-bit instruction support */
  998. SH_CPU_HAS_PTEAEX = 0x0200, /* PTE ASID Extension support */
  999. };
  1000. #define ELF_HWCAP get_elf_hwcap()
  1001. static uint32_t get_elf_hwcap(void)
  1002. {
  1003. SuperHCPU *cpu = SUPERH_CPU(thread_cpu);
  1004. uint32_t hwcap = 0;
  1005. hwcap |= SH_CPU_HAS_FPU;
  1006. if (cpu->env.features & SH_FEATURE_SH4A) {
  1007. hwcap |= SH_CPU_HAS_LLSC;
  1008. }
  1009. return hwcap;
  1010. }
  1011. #endif
  1012. #ifdef TARGET_CRIS
  1013. #define ELF_START_MMAP 0x80000000
  1014. #define ELF_CLASS ELFCLASS32
  1015. #define ELF_ARCH EM_CRIS
  1016. static inline void init_thread(struct target_pt_regs *regs,
  1017. struct image_info *infop)
  1018. {
  1019. regs->erp = infop->entry;
  1020. }
  1021. #define ELF_EXEC_PAGESIZE 8192
  1022. #endif
  1023. #ifdef TARGET_M68K
  1024. #define ELF_START_MMAP 0x80000000
  1025. #define ELF_CLASS ELFCLASS32
  1026. #define ELF_ARCH EM_68K
  1027. /* ??? Does this need to do anything?
  1028. #define ELF_PLAT_INIT(_r) */
  1029. static inline void init_thread(struct target_pt_regs *regs,
  1030. struct image_info *infop)
  1031. {
  1032. regs->usp = infop->start_stack;
  1033. regs->sr = 0;
  1034. regs->pc = infop->entry;
  1035. }
  1036. /* See linux kernel: arch/m68k/include/asm/elf.h. */
  1037. #define ELF_NREG 20
  1038. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  1039. static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
  1040. {
  1041. (*regs)[0] = tswapreg(env->dregs[1]);
  1042. (*regs)[1] = tswapreg(env->dregs[2]);
  1043. (*regs)[2] = tswapreg(env->dregs[3]);
  1044. (*regs)[3] = tswapreg(env->dregs[4]);
  1045. (*regs)[4] = tswapreg(env->dregs[5]);
  1046. (*regs)[5] = tswapreg(env->dregs[6]);
  1047. (*regs)[6] = tswapreg(env->dregs[7]);
  1048. (*regs)[7] = tswapreg(env->aregs[0]);
  1049. (*regs)[8] = tswapreg(env->aregs[1]);
  1050. (*regs)[9] = tswapreg(env->aregs[2]);
  1051. (*regs)[10] = tswapreg(env->aregs[3]);
  1052. (*regs)[11] = tswapreg(env->aregs[4]);
  1053. (*regs)[12] = tswapreg(env->aregs[5]);
  1054. (*regs)[13] = tswapreg(env->aregs[6]);
  1055. (*regs)[14] = tswapreg(env->dregs[0]);
  1056. (*regs)[15] = tswapreg(env->aregs[7]);
  1057. (*regs)[16] = tswapreg(env->dregs[0]); /* FIXME: orig_d0 */
  1058. (*regs)[17] = tswapreg(env->sr);
  1059. (*regs)[18] = tswapreg(env->pc);
  1060. (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
  1061. }
  1062. #define USE_ELF_CORE_DUMP
  1063. #define ELF_EXEC_PAGESIZE 8192
  1064. #endif
  1065. #ifdef TARGET_ALPHA
  1066. #define ELF_START_MMAP (0x30000000000ULL)
  1067. #define ELF_CLASS ELFCLASS64
  1068. #define ELF_ARCH EM_ALPHA
  1069. static inline void init_thread(struct target_pt_regs *regs,
  1070. struct image_info *infop)
  1071. {
  1072. regs->pc = infop->entry;
  1073. regs->ps = 8;
  1074. regs->usp = infop->start_stack;
  1075. }
  1076. #define ELF_EXEC_PAGESIZE 8192
  1077. #endif /* TARGET_ALPHA */
  1078. #ifdef TARGET_S390X
  1079. #define ELF_START_MMAP (0x20000000000ULL)
  1080. #define ELF_CLASS ELFCLASS64
  1081. #define ELF_DATA ELFDATA2MSB
  1082. #define ELF_ARCH EM_S390
  1083. #include "elf.h"
  1084. #define ELF_HWCAP get_elf_hwcap()
  1085. #define GET_FEATURE(_feat, _hwcap) \
  1086. do { if (s390_has_feat(_feat)) { hwcap |= _hwcap; } } while (0)
  1087. static uint32_t get_elf_hwcap(void)
  1088. {
  1089. /*
  1090. * Let's assume we always have esan3 and zarch.
  1091. * 31-bit processes can use 64-bit registers (high gprs).
  1092. */
  1093. uint32_t hwcap = HWCAP_S390_ESAN3 | HWCAP_S390_ZARCH | HWCAP_S390_HIGH_GPRS;
  1094. GET_FEATURE(S390_FEAT_STFLE, HWCAP_S390_STFLE);
  1095. GET_FEATURE(S390_FEAT_MSA, HWCAP_S390_MSA);
  1096. GET_FEATURE(S390_FEAT_LONG_DISPLACEMENT, HWCAP_S390_LDISP);
  1097. GET_FEATURE(S390_FEAT_EXTENDED_IMMEDIATE, HWCAP_S390_EIMM);
  1098. if (s390_has_feat(S390_FEAT_EXTENDED_TRANSLATION_3) &&
  1099. s390_has_feat(S390_FEAT_ETF3_ENH)) {
  1100. hwcap |= HWCAP_S390_ETF3EH;
  1101. }
  1102. GET_FEATURE(S390_FEAT_VECTOR, HWCAP_S390_VXRS);
  1103. return hwcap;
  1104. }
  1105. static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
  1106. {
  1107. regs->psw.addr = infop->entry;
  1108. regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
  1109. regs->gprs[15] = infop->start_stack;
  1110. }
  1111. #endif /* TARGET_S390X */
  1112. #ifdef TARGET_TILEGX
  1113. /* 42 bits real used address, a half for user mode */
  1114. #define ELF_START_MMAP (0x00000020000000000ULL)
  1115. #define elf_check_arch(x) ((x) == EM_TILEGX)
  1116. #define ELF_CLASS ELFCLASS64
  1117. #define ELF_DATA ELFDATA2LSB
  1118. #define ELF_ARCH EM_TILEGX
  1119. static inline void init_thread(struct target_pt_regs *regs,
  1120. struct image_info *infop)
  1121. {
  1122. regs->pc = infop->entry;
  1123. regs->sp = infop->start_stack;
  1124. }
  1125. #define ELF_EXEC_PAGESIZE 65536 /* TILE-Gx page size is 64KB */
  1126. #endif /* TARGET_TILEGX */
  1127. #ifdef TARGET_RISCV
  1128. #define ELF_START_MMAP 0x80000000
  1129. #define ELF_ARCH EM_RISCV
  1130. #ifdef TARGET_RISCV32
  1131. #define ELF_CLASS ELFCLASS32
  1132. #else
  1133. #define ELF_CLASS ELFCLASS64
  1134. #endif
  1135. static inline void init_thread(struct target_pt_regs *regs,
  1136. struct image_info *infop)
  1137. {
  1138. regs->sepc = infop->entry;
  1139. regs->sp = infop->start_stack;
  1140. }
  1141. #define ELF_EXEC_PAGESIZE 4096
  1142. #endif /* TARGET_RISCV */
  1143. #ifdef TARGET_HPPA
  1144. #define ELF_START_MMAP 0x80000000
  1145. #define ELF_CLASS ELFCLASS32
  1146. #define ELF_ARCH EM_PARISC
  1147. #define ELF_PLATFORM "PARISC"
  1148. #define STACK_GROWS_DOWN 0
  1149. #define STACK_ALIGNMENT 64
  1150. static inline void init_thread(struct target_pt_regs *regs,
  1151. struct image_info *infop)
  1152. {
  1153. regs->iaoq[0] = infop->entry;
  1154. regs->iaoq[1] = infop->entry + 4;
  1155. regs->gr[23] = 0;
  1156. regs->gr[24] = infop->arg_start;
  1157. regs->gr[25] = (infop->arg_end - infop->arg_start) / sizeof(abi_ulong);
  1158. /* The top-of-stack contains a linkage buffer. */
  1159. regs->gr[30] = infop->start_stack + 64;
  1160. regs->gr[31] = infop->entry;
  1161. }
  1162. #endif /* TARGET_HPPA */
  1163. #ifdef TARGET_XTENSA
  1164. #define ELF_START_MMAP 0x20000000
  1165. #define ELF_CLASS ELFCLASS32
  1166. #define ELF_ARCH EM_XTENSA
  1167. static inline void init_thread(struct target_pt_regs *regs,
  1168. struct image_info *infop)
  1169. {
  1170. regs->windowbase = 0;
  1171. regs->windowstart = 1;
  1172. regs->areg[1] = infop->start_stack;
  1173. regs->pc = infop->entry;
  1174. }
  1175. /* See linux kernel: arch/xtensa/include/asm/elf.h. */
  1176. #define ELF_NREG 128
  1177. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  1178. enum {
  1179. TARGET_REG_PC,
  1180. TARGET_REG_PS,
  1181. TARGET_REG_LBEG,
  1182. TARGET_REG_LEND,
  1183. TARGET_REG_LCOUNT,
  1184. TARGET_REG_SAR,
  1185. TARGET_REG_WINDOWSTART,
  1186. TARGET_REG_WINDOWBASE,
  1187. TARGET_REG_THREADPTR,
  1188. TARGET_REG_AR0 = 64,
  1189. };
  1190. static void elf_core_copy_regs(target_elf_gregset_t *regs,
  1191. const CPUXtensaState *env)
  1192. {
  1193. unsigned i;
  1194. (*regs)[TARGET_REG_PC] = tswapreg(env->pc);
  1195. (*regs)[TARGET_REG_PS] = tswapreg(env->sregs[PS] & ~PS_EXCM);
  1196. (*regs)[TARGET_REG_LBEG] = tswapreg(env->sregs[LBEG]);
  1197. (*regs)[TARGET_REG_LEND] = tswapreg(env->sregs[LEND]);
  1198. (*regs)[TARGET_REG_LCOUNT] = tswapreg(env->sregs[LCOUNT]);
  1199. (*regs)[TARGET_REG_SAR] = tswapreg(env->sregs[SAR]);
  1200. (*regs)[TARGET_REG_WINDOWSTART] = tswapreg(env->sregs[WINDOW_START]);
  1201. (*regs)[TARGET_REG_WINDOWBASE] = tswapreg(env->sregs[WINDOW_BASE]);
  1202. (*regs)[TARGET_REG_THREADPTR] = tswapreg(env->uregs[THREADPTR]);
  1203. xtensa_sync_phys_from_window((CPUXtensaState *)env);
  1204. for (i = 0; i < env->config->nareg; ++i) {
  1205. (*regs)[TARGET_REG_AR0 + i] = tswapreg(env->phys_regs[i]);
  1206. }
  1207. }
  1208. #define USE_ELF_CORE_DUMP
  1209. #define ELF_EXEC_PAGESIZE 4096
  1210. #endif /* TARGET_XTENSA */
  1211. #ifndef ELF_PLATFORM
  1212. #define ELF_PLATFORM (NULL)
  1213. #endif
  1214. #ifndef ELF_MACHINE
  1215. #define ELF_MACHINE ELF_ARCH
  1216. #endif
  1217. #ifndef elf_check_arch
  1218. #define elf_check_arch(x) ((x) == ELF_ARCH)
  1219. #endif
  1220. #ifndef ELF_HWCAP
  1221. #define ELF_HWCAP 0
  1222. #endif
  1223. #ifndef STACK_GROWS_DOWN
  1224. #define STACK_GROWS_DOWN 1
  1225. #endif
  1226. #ifndef STACK_ALIGNMENT
  1227. #define STACK_ALIGNMENT 16
  1228. #endif
  1229. #ifdef TARGET_ABI32
  1230. #undef ELF_CLASS
  1231. #define ELF_CLASS ELFCLASS32
  1232. #undef bswaptls
  1233. #define bswaptls(ptr) bswap32s(ptr)
  1234. #endif
  1235. #include "elf.h"
  1236. struct exec
  1237. {
  1238. unsigned int a_info; /* Use macros N_MAGIC, etc for access */
  1239. unsigned int a_text; /* length of text, in bytes */
  1240. unsigned int a_data; /* length of data, in bytes */
  1241. unsigned int a_bss; /* length of uninitialized data area, in bytes */
  1242. unsigned int a_syms; /* length of symbol table data in file, in bytes */
  1243. unsigned int a_entry; /* start address */
  1244. unsigned int a_trsize; /* length of relocation info for text, in bytes */
  1245. unsigned int a_drsize; /* length of relocation info for data, in bytes */
  1246. };
  1247. #define N_MAGIC(exec) ((exec).a_info & 0xffff)
  1248. #define OMAGIC 0407
  1249. #define NMAGIC 0410
  1250. #define ZMAGIC 0413
  1251. #define QMAGIC 0314
  1252. /* Necessary parameters */
  1253. #define TARGET_ELF_EXEC_PAGESIZE \
  1254. (((eppnt->p_align & ~qemu_host_page_mask) != 0) ? \
  1255. TARGET_PAGE_SIZE : MAX(qemu_host_page_size, TARGET_PAGE_SIZE))
  1256. #define TARGET_ELF_PAGELENGTH(_v) ROUND_UP((_v), TARGET_ELF_EXEC_PAGESIZE)
  1257. #define TARGET_ELF_PAGESTART(_v) ((_v) & \
  1258. ~(abi_ulong)(TARGET_ELF_EXEC_PAGESIZE-1))
  1259. #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
  1260. #define DLINFO_ITEMS 16
  1261. static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
  1262. {
  1263. memcpy(to, from, n);
  1264. }
  1265. #ifdef BSWAP_NEEDED
  1266. static void bswap_ehdr(struct elfhdr *ehdr)
  1267. {
  1268. bswap16s(&ehdr->e_type); /* Object file type */
  1269. bswap16s(&ehdr->e_machine); /* Architecture */
  1270. bswap32s(&ehdr->e_version); /* Object file version */
  1271. bswaptls(&ehdr->e_entry); /* Entry point virtual address */
  1272. bswaptls(&ehdr->e_phoff); /* Program header table file offset */
  1273. bswaptls(&ehdr->e_shoff); /* Section header table file offset */
  1274. bswap32s(&ehdr->e_flags); /* Processor-specific flags */
  1275. bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
  1276. bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
  1277. bswap16s(&ehdr->e_phnum); /* Program header table entry count */
  1278. bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
  1279. bswap16s(&ehdr->e_shnum); /* Section header table entry count */
  1280. bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
  1281. }
  1282. static void bswap_phdr(struct elf_phdr *phdr, int phnum)
  1283. {
  1284. int i;
  1285. for (i = 0; i < phnum; ++i, ++phdr) {
  1286. bswap32s(&phdr->p_type); /* Segment type */
  1287. bswap32s(&phdr->p_flags); /* Segment flags */
  1288. bswaptls(&phdr->p_offset); /* Segment file offset */
  1289. bswaptls(&phdr->p_vaddr); /* Segment virtual address */
  1290. bswaptls(&phdr->p_paddr); /* Segment physical address */
  1291. bswaptls(&phdr->p_filesz); /* Segment size in file */
  1292. bswaptls(&phdr->p_memsz); /* Segment size in memory */
  1293. bswaptls(&phdr->p_align); /* Segment alignment */
  1294. }
  1295. }
  1296. static void bswap_shdr(struct elf_shdr *shdr, int shnum)
  1297. {
  1298. int i;
  1299. for (i = 0; i < shnum; ++i, ++shdr) {
  1300. bswap32s(&shdr->sh_name);
  1301. bswap32s(&shdr->sh_type);
  1302. bswaptls(&shdr->sh_flags);
  1303. bswaptls(&shdr->sh_addr);
  1304. bswaptls(&shdr->sh_offset);
  1305. bswaptls(&shdr->sh_size);
  1306. bswap32s(&shdr->sh_link);
  1307. bswap32s(&shdr->sh_info);
  1308. bswaptls(&shdr->sh_addralign);
  1309. bswaptls(&shdr->sh_entsize);
  1310. }
  1311. }
  1312. static void bswap_sym(struct elf_sym *sym)
  1313. {
  1314. bswap32s(&sym->st_name);
  1315. bswaptls(&sym->st_value);
  1316. bswaptls(&sym->st_size);
  1317. bswap16s(&sym->st_shndx);
  1318. }
  1319. #ifdef TARGET_MIPS
  1320. static void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags)
  1321. {
  1322. bswap16s(&abiflags->version);
  1323. bswap32s(&abiflags->ases);
  1324. bswap32s(&abiflags->isa_ext);
  1325. bswap32s(&abiflags->flags1);
  1326. bswap32s(&abiflags->flags2);
  1327. }
  1328. #endif
  1329. #else
  1330. static inline void bswap_ehdr(struct elfhdr *ehdr) { }
  1331. static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
  1332. static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
  1333. static inline void bswap_sym(struct elf_sym *sym) { }
  1334. #ifdef TARGET_MIPS
  1335. static inline void bswap_mips_abiflags(Mips_elf_abiflags_v0 *abiflags) { }
  1336. #endif
  1337. #endif
  1338. #ifdef USE_ELF_CORE_DUMP
  1339. static int elf_core_dump(int, const CPUArchState *);
  1340. #endif /* USE_ELF_CORE_DUMP */
  1341. static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
  1342. /* Verify the portions of EHDR within E_IDENT for the target.
  1343. This can be performed before bswapping the entire header. */
  1344. static bool elf_check_ident(struct elfhdr *ehdr)
  1345. {
  1346. return (ehdr->e_ident[EI_MAG0] == ELFMAG0
  1347. && ehdr->e_ident[EI_MAG1] == ELFMAG1
  1348. && ehdr->e_ident[EI_MAG2] == ELFMAG2
  1349. && ehdr->e_ident[EI_MAG3] == ELFMAG3
  1350. && ehdr->e_ident[EI_CLASS] == ELF_CLASS
  1351. && ehdr->e_ident[EI_DATA] == ELF_DATA
  1352. && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
  1353. }
  1354. /* Verify the portions of EHDR outside of E_IDENT for the target.
  1355. This has to wait until after bswapping the header. */
  1356. static bool elf_check_ehdr(struct elfhdr *ehdr)
  1357. {
  1358. return (elf_check_arch(ehdr->e_machine)
  1359. && ehdr->e_ehsize == sizeof(struct elfhdr)
  1360. && ehdr->e_phentsize == sizeof(struct elf_phdr)
  1361. && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
  1362. }
  1363. /*
  1364. * 'copy_elf_strings()' copies argument/envelope strings from user
  1365. * memory to free pages in kernel mem. These are in a format ready
  1366. * to be put directly into the top of new user memory.
  1367. *
  1368. */
  1369. static abi_ulong copy_elf_strings(int argc, char **argv, char *scratch,
  1370. abi_ulong p, abi_ulong stack_limit)
  1371. {
  1372. char *tmp;
  1373. int len, i;
  1374. abi_ulong top = p;
  1375. if (!p) {
  1376. return 0; /* bullet-proofing */
  1377. }
  1378. if (STACK_GROWS_DOWN) {
  1379. int offset = ((p - 1) % TARGET_PAGE_SIZE) + 1;
  1380. for (i = argc - 1; i >= 0; --i) {
  1381. tmp = argv[i];
  1382. if (!tmp) {
  1383. fprintf(stderr, "VFS: argc is wrong");
  1384. exit(-1);
  1385. }
  1386. len = strlen(tmp) + 1;
  1387. tmp += len;
  1388. if (len > (p - stack_limit)) {
  1389. return 0;
  1390. }
  1391. while (len) {
  1392. int bytes_to_copy = (len > offset) ? offset : len;
  1393. tmp -= bytes_to_copy;
  1394. p -= bytes_to_copy;
  1395. offset -= bytes_to_copy;
  1396. len -= bytes_to_copy;
  1397. memcpy_fromfs(scratch + offset, tmp, bytes_to_copy);
  1398. if (offset == 0) {
  1399. memcpy_to_target(p, scratch, top - p);
  1400. top = p;
  1401. offset = TARGET_PAGE_SIZE;
  1402. }
  1403. }
  1404. }
  1405. if (p != top) {
  1406. memcpy_to_target(p, scratch + offset, top - p);
  1407. }
  1408. } else {
  1409. int remaining = TARGET_PAGE_SIZE - (p % TARGET_PAGE_SIZE);
  1410. for (i = 0; i < argc; ++i) {
  1411. tmp = argv[i];
  1412. if (!tmp) {
  1413. fprintf(stderr, "VFS: argc is wrong");
  1414. exit(-1);
  1415. }
  1416. len = strlen(tmp) + 1;
  1417. if (len > (stack_limit - p)) {
  1418. return 0;
  1419. }
  1420. while (len) {
  1421. int bytes_to_copy = (len > remaining) ? remaining : len;
  1422. memcpy_fromfs(scratch + (p - top), tmp, bytes_to_copy);
  1423. tmp += bytes_to_copy;
  1424. remaining -= bytes_to_copy;
  1425. p += bytes_to_copy;
  1426. len -= bytes_to_copy;
  1427. if (remaining == 0) {
  1428. memcpy_to_target(top, scratch, p - top);
  1429. top = p;
  1430. remaining = TARGET_PAGE_SIZE;
  1431. }
  1432. }
  1433. }
  1434. if (p != top) {
  1435. memcpy_to_target(top, scratch, p - top);
  1436. }
  1437. }
  1438. return p;
  1439. }
  1440. /* Older linux kernels provide up to MAX_ARG_PAGES (default: 32) of
  1441. * argument/environment space. Newer kernels (>2.6.33) allow more,
  1442. * dependent on stack size, but guarantee at least 32 pages for
  1443. * backwards compatibility.
  1444. */
  1445. #define STACK_LOWER_LIMIT (32 * TARGET_PAGE_SIZE)
  1446. static abi_ulong setup_arg_pages(struct linux_binprm *bprm,
  1447. struct image_info *info)
  1448. {
  1449. abi_ulong size, error, guard;
  1450. size = guest_stack_size;
  1451. if (size < STACK_LOWER_LIMIT) {
  1452. size = STACK_LOWER_LIMIT;
  1453. }
  1454. guard = TARGET_PAGE_SIZE;
  1455. if (guard < qemu_real_host_page_size) {
  1456. guard = qemu_real_host_page_size;
  1457. }
  1458. error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
  1459. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  1460. if (error == -1) {
  1461. perror("mmap stack");
  1462. exit(-1);
  1463. }
  1464. /* We reserve one extra page at the top of the stack as guard. */
  1465. if (STACK_GROWS_DOWN) {
  1466. target_mprotect(error, guard, PROT_NONE);
  1467. info->stack_limit = error + guard;
  1468. return info->stack_limit + size - sizeof(void *);
  1469. } else {
  1470. target_mprotect(error + size, guard, PROT_NONE);
  1471. info->stack_limit = error + size;
  1472. return error;
  1473. }
  1474. }
  1475. /* Map and zero the bss. We need to explicitly zero any fractional pages
  1476. after the data section (i.e. bss). */
  1477. static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
  1478. {
  1479. uintptr_t host_start, host_map_start, host_end;
  1480. last_bss = TARGET_PAGE_ALIGN(last_bss);
  1481. /* ??? There is confusion between qemu_real_host_page_size and
  1482. qemu_host_page_size here and elsewhere in target_mmap, which
  1483. may lead to the end of the data section mapping from the file
  1484. not being mapped. At least there was an explicit test and
  1485. comment for that here, suggesting that "the file size must
  1486. be known". The comment probably pre-dates the introduction
  1487. of the fstat system call in target_mmap which does in fact
  1488. find out the size. What isn't clear is if the workaround
  1489. here is still actually needed. For now, continue with it,
  1490. but merge it with the "normal" mmap that would allocate the bss. */
  1491. host_start = (uintptr_t) g2h(elf_bss);
  1492. host_end = (uintptr_t) g2h(last_bss);
  1493. host_map_start = REAL_HOST_PAGE_ALIGN(host_start);
  1494. if (host_map_start < host_end) {
  1495. void *p = mmap((void *)host_map_start, host_end - host_map_start,
  1496. prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  1497. if (p == MAP_FAILED) {
  1498. perror("cannot mmap brk");
  1499. exit(-1);
  1500. }
  1501. }
  1502. /* Ensure that the bss page(s) are valid */
  1503. if ((page_get_flags(last_bss-1) & prot) != prot) {
  1504. page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot | PAGE_VALID);
  1505. }
  1506. if (host_start < host_map_start) {
  1507. memset((void *)host_start, 0, host_map_start - host_start);
  1508. }
  1509. }
  1510. #ifdef TARGET_ARM
  1511. static int elf_is_fdpic(struct elfhdr *exec)
  1512. {
  1513. return exec->e_ident[EI_OSABI] == ELFOSABI_ARM_FDPIC;
  1514. }
  1515. #else
  1516. /* Default implementation, always false. */
  1517. static int elf_is_fdpic(struct elfhdr *exec)
  1518. {
  1519. return 0;
  1520. }
  1521. #endif
  1522. static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
  1523. {
  1524. uint16_t n;
  1525. struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
  1526. /* elf32_fdpic_loadseg */
  1527. n = info->nsegs;
  1528. while (n--) {
  1529. sp -= 12;
  1530. put_user_u32(loadsegs[n].addr, sp+0);
  1531. put_user_u32(loadsegs[n].p_vaddr, sp+4);
  1532. put_user_u32(loadsegs[n].p_memsz, sp+8);
  1533. }
  1534. /* elf32_fdpic_loadmap */
  1535. sp -= 4;
  1536. put_user_u16(0, sp+0); /* version */
  1537. put_user_u16(info->nsegs, sp+2); /* nsegs */
  1538. info->personality = PER_LINUX_FDPIC;
  1539. info->loadmap_addr = sp;
  1540. return sp;
  1541. }
  1542. static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
  1543. struct elfhdr *exec,
  1544. struct image_info *info,
  1545. struct image_info *interp_info)
  1546. {
  1547. abi_ulong sp;
  1548. abi_ulong u_argc, u_argv, u_envp, u_auxv;
  1549. int size;
  1550. int i;
  1551. abi_ulong u_rand_bytes;
  1552. uint8_t k_rand_bytes[16];
  1553. abi_ulong u_platform;
  1554. const char *k_platform;
  1555. const int n = sizeof(elf_addr_t);
  1556. sp = p;
  1557. /* Needs to be before we load the env/argc/... */
  1558. if (elf_is_fdpic(exec)) {
  1559. /* Need 4 byte alignment for these structs */
  1560. sp &= ~3;
  1561. sp = loader_build_fdpic_loadmap(info, sp);
  1562. info->other_info = interp_info;
  1563. if (interp_info) {
  1564. interp_info->other_info = info;
  1565. sp = loader_build_fdpic_loadmap(interp_info, sp);
  1566. info->interpreter_loadmap_addr = interp_info->loadmap_addr;
  1567. info->interpreter_pt_dynamic_addr = interp_info->pt_dynamic_addr;
  1568. } else {
  1569. info->interpreter_loadmap_addr = 0;
  1570. info->interpreter_pt_dynamic_addr = 0;
  1571. }
  1572. }
  1573. u_platform = 0;
  1574. k_platform = ELF_PLATFORM;
  1575. if (k_platform) {
  1576. size_t len = strlen(k_platform) + 1;
  1577. if (STACK_GROWS_DOWN) {
  1578. sp -= (len + n - 1) & ~(n - 1);
  1579. u_platform = sp;
  1580. /* FIXME - check return value of memcpy_to_target() for failure */
  1581. memcpy_to_target(sp, k_platform, len);
  1582. } else {
  1583. memcpy_to_target(sp, k_platform, len);
  1584. u_platform = sp;
  1585. sp += len + 1;
  1586. }
  1587. }
  1588. /* Provide 16 byte alignment for the PRNG, and basic alignment for
  1589. * the argv and envp pointers.
  1590. */
  1591. if (STACK_GROWS_DOWN) {
  1592. sp = QEMU_ALIGN_DOWN(sp, 16);
  1593. } else {
  1594. sp = QEMU_ALIGN_UP(sp, 16);
  1595. }
  1596. /*
  1597. * Generate 16 random bytes for userspace PRNG seeding.
  1598. */
  1599. qemu_guest_getrandom_nofail(k_rand_bytes, sizeof(k_rand_bytes));
  1600. if (STACK_GROWS_DOWN) {
  1601. sp -= 16;
  1602. u_rand_bytes = sp;
  1603. /* FIXME - check return value of memcpy_to_target() for failure */
  1604. memcpy_to_target(sp, k_rand_bytes, 16);
  1605. } else {
  1606. memcpy_to_target(sp, k_rand_bytes, 16);
  1607. u_rand_bytes = sp;
  1608. sp += 16;
  1609. }
  1610. size = (DLINFO_ITEMS + 1) * 2;
  1611. if (k_platform)
  1612. size += 2;
  1613. #ifdef DLINFO_ARCH_ITEMS
  1614. size += DLINFO_ARCH_ITEMS * 2;
  1615. #endif
  1616. #ifdef ELF_HWCAP2
  1617. size += 2;
  1618. #endif
  1619. info->auxv_len = size * n;
  1620. size += envc + argc + 2;
  1621. size += 1; /* argc itself */
  1622. size *= n;
  1623. /* Allocate space and finalize stack alignment for entry now. */
  1624. if (STACK_GROWS_DOWN) {
  1625. u_argc = QEMU_ALIGN_DOWN(sp - size, STACK_ALIGNMENT);
  1626. sp = u_argc;
  1627. } else {
  1628. u_argc = sp;
  1629. sp = QEMU_ALIGN_UP(sp + size, STACK_ALIGNMENT);
  1630. }
  1631. u_argv = u_argc + n;
  1632. u_envp = u_argv + (argc + 1) * n;
  1633. u_auxv = u_envp + (envc + 1) * n;
  1634. info->saved_auxv = u_auxv;
  1635. info->arg_start = u_argv;
  1636. info->arg_end = u_argv + argc * n;
  1637. /* This is correct because Linux defines
  1638. * elf_addr_t as Elf32_Off / Elf64_Off
  1639. */
  1640. #define NEW_AUX_ENT(id, val) do { \
  1641. put_user_ual(id, u_auxv); u_auxv += n; \
  1642. put_user_ual(val, u_auxv); u_auxv += n; \
  1643. } while(0)
  1644. #ifdef ARCH_DLINFO
  1645. /*
  1646. * ARCH_DLINFO must come first so platform specific code can enforce
  1647. * special alignment requirements on the AUXV if necessary (eg. PPC).
  1648. */
  1649. ARCH_DLINFO;
  1650. #endif
  1651. /* There must be exactly DLINFO_ITEMS entries here, or the assert
  1652. * on info->auxv_len will trigger.
  1653. */
  1654. NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
  1655. NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
  1656. NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
  1657. if ((info->alignment & ~qemu_host_page_mask) != 0) {
  1658. /* Target doesn't support host page size alignment */
  1659. NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
  1660. } else {
  1661. NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(MAX(TARGET_PAGE_SIZE,
  1662. qemu_host_page_size)));
  1663. }
  1664. NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
  1665. NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
  1666. NEW_AUX_ENT(AT_ENTRY, info->entry);
  1667. NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
  1668. NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
  1669. NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
  1670. NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
  1671. NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
  1672. NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
  1673. NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
  1674. NEW_AUX_ENT(AT_SECURE, (abi_ulong) qemu_getauxval(AT_SECURE));
  1675. NEW_AUX_ENT(AT_EXECFN, info->file_string);
  1676. #ifdef ELF_HWCAP2
  1677. NEW_AUX_ENT(AT_HWCAP2, (abi_ulong) ELF_HWCAP2);
  1678. #endif
  1679. if (u_platform) {
  1680. NEW_AUX_ENT(AT_PLATFORM, u_platform);
  1681. }
  1682. NEW_AUX_ENT (AT_NULL, 0);
  1683. #undef NEW_AUX_ENT
  1684. /* Check that our initial calculation of the auxv length matches how much
  1685. * we actually put into it.
  1686. */
  1687. assert(info->auxv_len == u_auxv - info->saved_auxv);
  1688. put_user_ual(argc, u_argc);
  1689. p = info->arg_strings;
  1690. for (i = 0; i < argc; ++i) {
  1691. put_user_ual(p, u_argv);
  1692. u_argv += n;
  1693. p += target_strlen(p) + 1;
  1694. }
  1695. put_user_ual(0, u_argv);
  1696. p = info->env_strings;
  1697. for (i = 0; i < envc; ++i) {
  1698. put_user_ual(p, u_envp);
  1699. u_envp += n;
  1700. p += target_strlen(p) + 1;
  1701. }
  1702. put_user_ual(0, u_envp);
  1703. return sp;
  1704. }
  1705. #ifndef ARM_COMMPAGE
  1706. #define ARM_COMMPAGE 0
  1707. #define init_guest_commpage() true
  1708. #endif
  1709. static void pgb_fail_in_use(const char *image_name)
  1710. {
  1711. error_report("%s: requires virtual address space that is in use "
  1712. "(omit the -B option or choose a different value)",
  1713. image_name);
  1714. exit(EXIT_FAILURE);
  1715. }
  1716. static void pgb_have_guest_base(const char *image_name, abi_ulong guest_loaddr,
  1717. abi_ulong guest_hiaddr, long align)
  1718. {
  1719. const int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
  1720. void *addr, *test;
  1721. if (!QEMU_IS_ALIGNED(guest_base, align)) {
  1722. fprintf(stderr, "Requested guest base 0x%lx does not satisfy "
  1723. "host minimum alignment (0x%lx)\n",
  1724. guest_base, align);
  1725. exit(EXIT_FAILURE);
  1726. }
  1727. /* Sanity check the guest binary. */
  1728. if (reserved_va) {
  1729. if (guest_hiaddr > reserved_va) {
  1730. error_report("%s: requires more than reserved virtual "
  1731. "address space (0x%" PRIx64 " > 0x%lx)",
  1732. image_name, (uint64_t)guest_hiaddr, reserved_va);
  1733. exit(EXIT_FAILURE);
  1734. }
  1735. } else {
  1736. #if HOST_LONG_BITS < TARGET_ABI_BITS
  1737. if ((guest_hiaddr - guest_base) > ~(uintptr_t)0) {
  1738. error_report("%s: requires more virtual address space "
  1739. "than the host can provide (0x%" PRIx64 ")",
  1740. image_name, (uint64_t)guest_hiaddr - guest_base);
  1741. exit(EXIT_FAILURE);
  1742. }
  1743. #endif
  1744. }
  1745. /*
  1746. * Expand the allocation to the entire reserved_va.
  1747. * Exclude the mmap_min_addr hole.
  1748. */
  1749. if (reserved_va) {
  1750. guest_loaddr = (guest_base >= mmap_min_addr ? 0
  1751. : mmap_min_addr - guest_base);
  1752. guest_hiaddr = reserved_va;
  1753. }
  1754. /* Reserve the address space for the binary, or reserved_va. */
  1755. test = g2h(guest_loaddr);
  1756. addr = mmap(test, guest_hiaddr - guest_loaddr, PROT_NONE, flags, -1, 0);
  1757. if (test != addr) {
  1758. pgb_fail_in_use(image_name);
  1759. }
  1760. }
  1761. /**
  1762. * pgd_find_hole_fallback: potential mmap address
  1763. * @guest_size: size of available space
  1764. * @brk: location of break
  1765. * @align: memory alignment
  1766. *
  1767. * This is a fallback method for finding a hole in the host address
  1768. * space if we don't have the benefit of being able to access
  1769. * /proc/self/map. It can potentially take a very long time as we can
  1770. * only dumbly iterate up the host address space seeing if the
  1771. * allocation would work.
  1772. */
  1773. static uintptr_t pgd_find_hole_fallback(uintptr_t guest_size, uintptr_t brk,
  1774. long align, uintptr_t offset)
  1775. {
  1776. uintptr_t base;
  1777. /* Start (aligned) at the bottom and work our way up */
  1778. base = ROUND_UP(mmap_min_addr, align);
  1779. while (true) {
  1780. uintptr_t align_start, end;
  1781. align_start = ROUND_UP(base, align);
  1782. end = align_start + guest_size + offset;
  1783. /* if brk is anywhere in the range give ourselves some room to grow. */
  1784. if (align_start <= brk && brk < end) {
  1785. base = brk + (16 * MiB);
  1786. continue;
  1787. } else if (align_start + guest_size < align_start) {
  1788. /* we have run out of space */
  1789. return -1;
  1790. } else {
  1791. int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE |
  1792. MAP_FIXED_NOREPLACE;
  1793. void * mmap_start = mmap((void *) align_start, guest_size,
  1794. PROT_NONE, flags, -1, 0);
  1795. if (mmap_start != MAP_FAILED) {
  1796. munmap((void *) align_start, guest_size);
  1797. if (MAP_FIXED_NOREPLACE || mmap_start == (void *) align_start) {
  1798. return (uintptr_t) mmap_start + offset;
  1799. }
  1800. }
  1801. base += qemu_host_page_size;
  1802. }
  1803. }
  1804. }
  1805. /* Return value for guest_base, or -1 if no hole found. */
  1806. static uintptr_t pgb_find_hole(uintptr_t guest_loaddr, uintptr_t guest_size,
  1807. long align, uintptr_t offset)
  1808. {
  1809. GSList *maps, *iter;
  1810. uintptr_t this_start, this_end, next_start, brk;
  1811. intptr_t ret = -1;
  1812. assert(QEMU_IS_ALIGNED(guest_loaddr, align));
  1813. maps = read_self_maps();
  1814. /* Read brk after we've read the maps, which will malloc. */
  1815. brk = (uintptr_t)sbrk(0);
  1816. if (!maps) {
  1817. return pgd_find_hole_fallback(guest_size, brk, align, offset);
  1818. }
  1819. /* The first hole is before the first map entry. */
  1820. this_start = mmap_min_addr;
  1821. for (iter = maps; iter;
  1822. this_start = next_start, iter = g_slist_next(iter)) {
  1823. uintptr_t align_start, hole_size;
  1824. this_end = ((MapInfo *)iter->data)->start;
  1825. next_start = ((MapInfo *)iter->data)->end;
  1826. align_start = ROUND_UP(this_start + offset, align);
  1827. /* Skip holes that are too small. */
  1828. if (align_start >= this_end) {
  1829. continue;
  1830. }
  1831. hole_size = this_end - align_start;
  1832. if (hole_size < guest_size) {
  1833. continue;
  1834. }
  1835. /* If this hole contains brk, give ourselves some room to grow. */
  1836. if (this_start <= brk && brk < this_end) {
  1837. hole_size -= guest_size;
  1838. if (sizeof(uintptr_t) == 8 && hole_size >= 1 * GiB) {
  1839. align_start += 1 * GiB;
  1840. } else if (hole_size >= 16 * MiB) {
  1841. align_start += 16 * MiB;
  1842. } else {
  1843. align_start = (this_end - guest_size) & -align;
  1844. if (align_start < this_start) {
  1845. continue;
  1846. }
  1847. }
  1848. }
  1849. /* Record the lowest successful match. */
  1850. if (ret < 0) {
  1851. ret = align_start - guest_loaddr;
  1852. }
  1853. /* If this hole contains the identity map, select it. */
  1854. if (align_start <= guest_loaddr &&
  1855. guest_loaddr + guest_size <= this_end) {
  1856. ret = 0;
  1857. }
  1858. /* If this hole ends above the identity map, stop looking. */
  1859. if (this_end >= guest_loaddr) {
  1860. break;
  1861. }
  1862. }
  1863. free_self_maps(maps);
  1864. return ret;
  1865. }
  1866. static void pgb_static(const char *image_name, abi_ulong orig_loaddr,
  1867. abi_ulong orig_hiaddr, long align)
  1868. {
  1869. uintptr_t loaddr = orig_loaddr;
  1870. uintptr_t hiaddr = orig_hiaddr;
  1871. uintptr_t offset = 0;
  1872. uintptr_t addr;
  1873. if (hiaddr != orig_hiaddr) {
  1874. error_report("%s: requires virtual address space that the "
  1875. "host cannot provide (0x%" PRIx64 ")",
  1876. image_name, (uint64_t)orig_hiaddr);
  1877. exit(EXIT_FAILURE);
  1878. }
  1879. loaddr &= -align;
  1880. if (ARM_COMMPAGE) {
  1881. /*
  1882. * Extend the allocation to include the commpage.
  1883. * For a 64-bit host, this is just 4GiB; for a 32-bit host we
  1884. * need to ensure there is space bellow the guest_base so we
  1885. * can map the commpage in the place needed when the address
  1886. * arithmetic wraps around.
  1887. */
  1888. if (sizeof(uintptr_t) == 8 || loaddr >= 0x80000000u) {
  1889. hiaddr = (uintptr_t) 4 << 30;
  1890. } else {
  1891. offset = -(ARM_COMMPAGE & -align);
  1892. }
  1893. }
  1894. addr = pgb_find_hole(loaddr, hiaddr - loaddr, align, offset);
  1895. if (addr == -1) {
  1896. /*
  1897. * If ARM_COMMPAGE, there *might* be a non-consecutive allocation
  1898. * that can satisfy both. But as the normal arm32 link base address
  1899. * is ~32k, and we extend down to include the commpage, making the
  1900. * overhead only ~96k, this is unlikely.
  1901. */
  1902. error_report("%s: Unable to allocate %#zx bytes of "
  1903. "virtual address space", image_name,
  1904. (size_t)(hiaddr - loaddr));
  1905. exit(EXIT_FAILURE);
  1906. }
  1907. guest_base = addr;
  1908. }
  1909. static void pgb_dynamic(const char *image_name, long align)
  1910. {
  1911. /*
  1912. * The executable is dynamic and does not require a fixed address.
  1913. * All we need is a commpage that satisfies align.
  1914. * If we do not need a commpage, leave guest_base == 0.
  1915. */
  1916. if (ARM_COMMPAGE) {
  1917. uintptr_t addr, commpage;
  1918. /* 64-bit hosts should have used reserved_va. */
  1919. assert(sizeof(uintptr_t) == 4);
  1920. /*
  1921. * By putting the commpage at the first hole, that puts guest_base
  1922. * just above that, and maximises the positive guest addresses.
  1923. */
  1924. commpage = ARM_COMMPAGE & -align;
  1925. addr = pgb_find_hole(commpage, -commpage, align, 0);
  1926. assert(addr != -1);
  1927. guest_base = addr;
  1928. }
  1929. }
  1930. static void pgb_reserved_va(const char *image_name, abi_ulong guest_loaddr,
  1931. abi_ulong guest_hiaddr, long align)
  1932. {
  1933. int flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
  1934. void *addr, *test;
  1935. if (guest_hiaddr > reserved_va) {
  1936. error_report("%s: requires more than reserved virtual "
  1937. "address space (0x%" PRIx64 " > 0x%lx)",
  1938. image_name, (uint64_t)guest_hiaddr, reserved_va);
  1939. exit(EXIT_FAILURE);
  1940. }
  1941. /* Widen the "image" to the entire reserved address space. */
  1942. pgb_static(image_name, 0, reserved_va, align);
  1943. /* osdep.h defines this as 0 if it's missing */
  1944. flags |= MAP_FIXED_NOREPLACE;
  1945. /* Reserve the memory on the host. */
  1946. assert(guest_base != 0);
  1947. test = g2h(0);
  1948. addr = mmap(test, reserved_va, PROT_NONE, flags, -1, 0);
  1949. if (addr == MAP_FAILED) {
  1950. error_report("Unable to reserve 0x%lx bytes of virtual address "
  1951. "space (%s) for use as guest address space (check your "
  1952. "virtual memory ulimit setting or reserve less "
  1953. "using -R option)", reserved_va, strerror(errno));
  1954. exit(EXIT_FAILURE);
  1955. }
  1956. assert(addr == test);
  1957. }
  1958. void probe_guest_base(const char *image_name, abi_ulong guest_loaddr,
  1959. abi_ulong guest_hiaddr)
  1960. {
  1961. /* In order to use host shmat, we must be able to honor SHMLBA. */
  1962. uintptr_t align = MAX(SHMLBA, qemu_host_page_size);
  1963. if (have_guest_base) {
  1964. pgb_have_guest_base(image_name, guest_loaddr, guest_hiaddr, align);
  1965. } else if (reserved_va) {
  1966. pgb_reserved_va(image_name, guest_loaddr, guest_hiaddr, align);
  1967. } else if (guest_loaddr) {
  1968. pgb_static(image_name, guest_loaddr, guest_hiaddr, align);
  1969. } else {
  1970. pgb_dynamic(image_name, align);
  1971. }
  1972. /* Reserve and initialize the commpage. */
  1973. if (!init_guest_commpage()) {
  1974. /*
  1975. * With have_guest_base, the user has selected the address and
  1976. * we are trying to work with that. Otherwise, we have selected
  1977. * free space and init_guest_commpage must succeeded.
  1978. */
  1979. assert(have_guest_base);
  1980. pgb_fail_in_use(image_name);
  1981. }
  1982. assert(QEMU_IS_ALIGNED(guest_base, align));
  1983. qemu_log_mask(CPU_LOG_PAGE, "Locating guest address space "
  1984. "@ 0x%" PRIx64 "\n", (uint64_t)guest_base);
  1985. }
  1986. /* Load an ELF image into the address space.
  1987. IMAGE_NAME is the filename of the image, to use in error messages.
  1988. IMAGE_FD is the open file descriptor for the image.
  1989. BPRM_BUF is a copy of the beginning of the file; this of course
  1990. contains the elf file header at offset 0. It is assumed that this
  1991. buffer is sufficiently aligned to present no problems to the host
  1992. in accessing data at aligned offsets within the buffer.
  1993. On return: INFO values will be filled in, as necessary or available. */
  1994. static void load_elf_image(const char *image_name, int image_fd,
  1995. struct image_info *info, char **pinterp_name,
  1996. char bprm_buf[BPRM_BUF_SIZE])
  1997. {
  1998. struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
  1999. struct elf_phdr *phdr;
  2000. abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
  2001. int i, retval;
  2002. const char *errmsg;
  2003. /* First of all, some simple consistency checks */
  2004. errmsg = "Invalid ELF image for this architecture";
  2005. if (!elf_check_ident(ehdr)) {
  2006. goto exit_errmsg;
  2007. }
  2008. bswap_ehdr(ehdr);
  2009. if (!elf_check_ehdr(ehdr)) {
  2010. goto exit_errmsg;
  2011. }
  2012. i = ehdr->e_phnum * sizeof(struct elf_phdr);
  2013. if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
  2014. phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
  2015. } else {
  2016. phdr = (struct elf_phdr *) alloca(i);
  2017. retval = pread(image_fd, phdr, i, ehdr->e_phoff);
  2018. if (retval != i) {
  2019. goto exit_read;
  2020. }
  2021. }
  2022. bswap_phdr(phdr, ehdr->e_phnum);
  2023. info->nsegs = 0;
  2024. info->pt_dynamic_addr = 0;
  2025. mmap_lock();
  2026. /* Find the maximum size of the image and allocate an appropriate
  2027. amount of memory to handle that. */
  2028. loaddr = -1, hiaddr = 0;
  2029. info->alignment = 0;
  2030. for (i = 0; i < ehdr->e_phnum; ++i) {
  2031. if (phdr[i].p_type == PT_LOAD) {
  2032. abi_ulong a = phdr[i].p_vaddr - phdr[i].p_offset;
  2033. if (a < loaddr) {
  2034. loaddr = a;
  2035. }
  2036. a = phdr[i].p_vaddr + phdr[i].p_memsz;
  2037. if (a > hiaddr) {
  2038. hiaddr = a;
  2039. }
  2040. ++info->nsegs;
  2041. info->alignment |= phdr[i].p_align;
  2042. }
  2043. }
  2044. if (pinterp_name != NULL) {
  2045. /*
  2046. * This is the main executable.
  2047. *
  2048. * Reserve extra space for brk.
  2049. * We hold on to this space while placing the interpreter
  2050. * and the stack, lest they be placed immediately after
  2051. * the data segment and block allocation from the brk.
  2052. *
  2053. * 16MB is chosen as "large enough" without being so large
  2054. * as to allow the result to not fit with a 32-bit guest on
  2055. * a 32-bit host.
  2056. */
  2057. info->reserve_brk = 16 * MiB;
  2058. hiaddr += info->reserve_brk;
  2059. if (ehdr->e_type == ET_EXEC) {
  2060. /*
  2061. * Make sure that the low address does not conflict with
  2062. * MMAP_MIN_ADDR or the QEMU application itself.
  2063. */
  2064. probe_guest_base(image_name, loaddr, hiaddr);
  2065. } else {
  2066. /*
  2067. * The binary is dynamic, but we still need to
  2068. * select guest_base. In this case we pass a size.
  2069. */
  2070. probe_guest_base(image_name, 0, hiaddr - loaddr);
  2071. }
  2072. }
  2073. /*
  2074. * Reserve address space for all of this.
  2075. *
  2076. * In the case of ET_EXEC, we supply MAP_FIXED so that we get
  2077. * exactly the address range that is required.
  2078. *
  2079. * Otherwise this is ET_DYN, and we are searching for a location
  2080. * that can hold the memory space required. If the image is
  2081. * pre-linked, LOADDR will be non-zero, and the kernel should
  2082. * honor that address if it happens to be free.
  2083. *
  2084. * In both cases, we will overwrite pages in this range with mappings
  2085. * from the executable.
  2086. */
  2087. load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
  2088. MAP_PRIVATE | MAP_ANON | MAP_NORESERVE |
  2089. (ehdr->e_type == ET_EXEC ? MAP_FIXED : 0),
  2090. -1, 0);
  2091. if (load_addr == -1) {
  2092. goto exit_perror;
  2093. }
  2094. load_bias = load_addr - loaddr;
  2095. if (elf_is_fdpic(ehdr)) {
  2096. struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
  2097. g_malloc(sizeof(*loadsegs) * info->nsegs);
  2098. for (i = 0; i < ehdr->e_phnum; ++i) {
  2099. switch (phdr[i].p_type) {
  2100. case PT_DYNAMIC:
  2101. info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
  2102. break;
  2103. case PT_LOAD:
  2104. loadsegs->addr = phdr[i].p_vaddr + load_bias;
  2105. loadsegs->p_vaddr = phdr[i].p_vaddr;
  2106. loadsegs->p_memsz = phdr[i].p_memsz;
  2107. ++loadsegs;
  2108. break;
  2109. }
  2110. }
  2111. }
  2112. info->load_bias = load_bias;
  2113. info->code_offset = load_bias;
  2114. info->data_offset = load_bias;
  2115. info->load_addr = load_addr;
  2116. info->entry = ehdr->e_entry + load_bias;
  2117. info->start_code = -1;
  2118. info->end_code = 0;
  2119. info->start_data = -1;
  2120. info->end_data = 0;
  2121. info->brk = 0;
  2122. info->elf_flags = ehdr->e_flags;
  2123. for (i = 0; i < ehdr->e_phnum; i++) {
  2124. struct elf_phdr *eppnt = phdr + i;
  2125. if (eppnt->p_type == PT_LOAD) {
  2126. abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em, vaddr_len;
  2127. int elf_prot = 0;
  2128. if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
  2129. if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
  2130. if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
  2131. vaddr = load_bias + eppnt->p_vaddr;
  2132. vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
  2133. vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
  2134. vaddr_len = TARGET_ELF_PAGELENGTH(eppnt->p_filesz + vaddr_po);
  2135. /*
  2136. * Some segments may be completely empty without any backing file
  2137. * segment, in that case just let zero_bss allocate an empty buffer
  2138. * for it.
  2139. */
  2140. if (eppnt->p_filesz != 0) {
  2141. error = target_mmap(vaddr_ps, vaddr_len, elf_prot,
  2142. MAP_PRIVATE | MAP_FIXED,
  2143. image_fd, eppnt->p_offset - vaddr_po);
  2144. if (error == -1) {
  2145. goto exit_perror;
  2146. }
  2147. }
  2148. vaddr_ef = vaddr + eppnt->p_filesz;
  2149. vaddr_em = vaddr + eppnt->p_memsz;
  2150. /* If the load segment requests extra zeros (e.g. bss), map it. */
  2151. if (vaddr_ef < vaddr_em) {
  2152. zero_bss(vaddr_ef, vaddr_em, elf_prot);
  2153. }
  2154. /* Find the full program boundaries. */
  2155. if (elf_prot & PROT_EXEC) {
  2156. if (vaddr < info->start_code) {
  2157. info->start_code = vaddr;
  2158. }
  2159. if (vaddr_ef > info->end_code) {
  2160. info->end_code = vaddr_ef;
  2161. }
  2162. }
  2163. if (elf_prot & PROT_WRITE) {
  2164. if (vaddr < info->start_data) {
  2165. info->start_data = vaddr;
  2166. }
  2167. if (vaddr_ef > info->end_data) {
  2168. info->end_data = vaddr_ef;
  2169. }
  2170. if (vaddr_em > info->brk) {
  2171. info->brk = vaddr_em;
  2172. }
  2173. }
  2174. } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
  2175. char *interp_name;
  2176. if (*pinterp_name) {
  2177. errmsg = "Multiple PT_INTERP entries";
  2178. goto exit_errmsg;
  2179. }
  2180. interp_name = malloc(eppnt->p_filesz);
  2181. if (!interp_name) {
  2182. goto exit_perror;
  2183. }
  2184. if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
  2185. memcpy(interp_name, bprm_buf + eppnt->p_offset,
  2186. eppnt->p_filesz);
  2187. } else {
  2188. retval = pread(image_fd, interp_name, eppnt->p_filesz,
  2189. eppnt->p_offset);
  2190. if (retval != eppnt->p_filesz) {
  2191. goto exit_perror;
  2192. }
  2193. }
  2194. if (interp_name[eppnt->p_filesz - 1] != 0) {
  2195. errmsg = "Invalid PT_INTERP entry";
  2196. goto exit_errmsg;
  2197. }
  2198. *pinterp_name = interp_name;
  2199. #ifdef TARGET_MIPS
  2200. } else if (eppnt->p_type == PT_MIPS_ABIFLAGS) {
  2201. Mips_elf_abiflags_v0 abiflags;
  2202. if (eppnt->p_filesz < sizeof(Mips_elf_abiflags_v0)) {
  2203. errmsg = "Invalid PT_MIPS_ABIFLAGS entry";
  2204. goto exit_errmsg;
  2205. }
  2206. if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
  2207. memcpy(&abiflags, bprm_buf + eppnt->p_offset,
  2208. sizeof(Mips_elf_abiflags_v0));
  2209. } else {
  2210. retval = pread(image_fd, &abiflags, sizeof(Mips_elf_abiflags_v0),
  2211. eppnt->p_offset);
  2212. if (retval != sizeof(Mips_elf_abiflags_v0)) {
  2213. goto exit_perror;
  2214. }
  2215. }
  2216. bswap_mips_abiflags(&abiflags);
  2217. info->fp_abi = abiflags.fp_abi;
  2218. #endif
  2219. }
  2220. }
  2221. if (info->end_data == 0) {
  2222. info->start_data = info->end_code;
  2223. info->end_data = info->end_code;
  2224. info->brk = info->end_code;
  2225. }
  2226. if (qemu_log_enabled()) {
  2227. load_symbols(ehdr, image_fd, load_bias);
  2228. }
  2229. mmap_unlock();
  2230. close(image_fd);
  2231. return;
  2232. exit_read:
  2233. if (retval >= 0) {
  2234. errmsg = "Incomplete read of file header";
  2235. goto exit_errmsg;
  2236. }
  2237. exit_perror:
  2238. errmsg = strerror(errno);
  2239. exit_errmsg:
  2240. fprintf(stderr, "%s: %s\n", image_name, errmsg);
  2241. exit(-1);
  2242. }
  2243. static void load_elf_interp(const char *filename, struct image_info *info,
  2244. char bprm_buf[BPRM_BUF_SIZE])
  2245. {
  2246. int fd, retval;
  2247. fd = open(path(filename), O_RDONLY);
  2248. if (fd < 0) {
  2249. goto exit_perror;
  2250. }
  2251. retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
  2252. if (retval < 0) {
  2253. goto exit_perror;
  2254. }
  2255. if (retval < BPRM_BUF_SIZE) {
  2256. memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
  2257. }
  2258. load_elf_image(filename, fd, info, NULL, bprm_buf);
  2259. return;
  2260. exit_perror:
  2261. fprintf(stderr, "%s: %s\n", filename, strerror(errno));
  2262. exit(-1);
  2263. }
  2264. static int symfind(const void *s0, const void *s1)
  2265. {
  2266. target_ulong addr = *(target_ulong *)s0;
  2267. struct elf_sym *sym = (struct elf_sym *)s1;
  2268. int result = 0;
  2269. if (addr < sym->st_value) {
  2270. result = -1;
  2271. } else if (addr >= sym->st_value + sym->st_size) {
  2272. result = 1;
  2273. }
  2274. return result;
  2275. }
  2276. static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
  2277. {
  2278. #if ELF_CLASS == ELFCLASS32
  2279. struct elf_sym *syms = s->disas_symtab.elf32;
  2280. #else
  2281. struct elf_sym *syms = s->disas_symtab.elf64;
  2282. #endif
  2283. // binary search
  2284. struct elf_sym *sym;
  2285. sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
  2286. if (sym != NULL) {
  2287. return s->disas_strtab + sym->st_name;
  2288. }
  2289. return "";
  2290. }
  2291. /* FIXME: This should use elf_ops.h */
  2292. static int symcmp(const void *s0, const void *s1)
  2293. {
  2294. struct elf_sym *sym0 = (struct elf_sym *)s0;
  2295. struct elf_sym *sym1 = (struct elf_sym *)s1;
  2296. return (sym0->st_value < sym1->st_value)
  2297. ? -1
  2298. : ((sym0->st_value > sym1->st_value) ? 1 : 0);
  2299. }
  2300. /* Best attempt to load symbols from this ELF object. */
  2301. static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
  2302. {
  2303. int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
  2304. uint64_t segsz;
  2305. struct elf_shdr *shdr;
  2306. char *strings = NULL;
  2307. struct syminfo *s = NULL;
  2308. struct elf_sym *new_syms, *syms = NULL;
  2309. shnum = hdr->e_shnum;
  2310. i = shnum * sizeof(struct elf_shdr);
  2311. shdr = (struct elf_shdr *)alloca(i);
  2312. if (pread(fd, shdr, i, hdr->e_shoff) != i) {
  2313. return;
  2314. }
  2315. bswap_shdr(shdr, shnum);
  2316. for (i = 0; i < shnum; ++i) {
  2317. if (shdr[i].sh_type == SHT_SYMTAB) {
  2318. sym_idx = i;
  2319. str_idx = shdr[i].sh_link;
  2320. goto found;
  2321. }
  2322. }
  2323. /* There will be no symbol table if the file was stripped. */
  2324. return;
  2325. found:
  2326. /* Now know where the strtab and symtab are. Snarf them. */
  2327. s = g_try_new(struct syminfo, 1);
  2328. if (!s) {
  2329. goto give_up;
  2330. }
  2331. segsz = shdr[str_idx].sh_size;
  2332. s->disas_strtab = strings = g_try_malloc(segsz);
  2333. if (!strings ||
  2334. pread(fd, strings, segsz, shdr[str_idx].sh_offset) != segsz) {
  2335. goto give_up;
  2336. }
  2337. segsz = shdr[sym_idx].sh_size;
  2338. syms = g_try_malloc(segsz);
  2339. if (!syms || pread(fd, syms, segsz, shdr[sym_idx].sh_offset) != segsz) {
  2340. goto give_up;
  2341. }
  2342. if (segsz / sizeof(struct elf_sym) > INT_MAX) {
  2343. /* Implausibly large symbol table: give up rather than ploughing
  2344. * on with the number of symbols calculation overflowing
  2345. */
  2346. goto give_up;
  2347. }
  2348. nsyms = segsz / sizeof(struct elf_sym);
  2349. for (i = 0; i < nsyms; ) {
  2350. bswap_sym(syms + i);
  2351. /* Throw away entries which we do not need. */
  2352. if (syms[i].st_shndx == SHN_UNDEF
  2353. || syms[i].st_shndx >= SHN_LORESERVE
  2354. || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
  2355. if (i < --nsyms) {
  2356. syms[i] = syms[nsyms];
  2357. }
  2358. } else {
  2359. #if defined(TARGET_ARM) || defined (TARGET_MIPS)
  2360. /* The bottom address bit marks a Thumb or MIPS16 symbol. */
  2361. syms[i].st_value &= ~(target_ulong)1;
  2362. #endif
  2363. syms[i].st_value += load_bias;
  2364. i++;
  2365. }
  2366. }
  2367. /* No "useful" symbol. */
  2368. if (nsyms == 0) {
  2369. goto give_up;
  2370. }
  2371. /* Attempt to free the storage associated with the local symbols
  2372. that we threw away. Whether or not this has any effect on the
  2373. memory allocation depends on the malloc implementation and how
  2374. many symbols we managed to discard. */
  2375. new_syms = g_try_renew(struct elf_sym, syms, nsyms);
  2376. if (new_syms == NULL) {
  2377. goto give_up;
  2378. }
  2379. syms = new_syms;
  2380. qsort(syms, nsyms, sizeof(*syms), symcmp);
  2381. s->disas_num_syms = nsyms;
  2382. #if ELF_CLASS == ELFCLASS32
  2383. s->disas_symtab.elf32 = syms;
  2384. #else
  2385. s->disas_symtab.elf64 = syms;
  2386. #endif
  2387. s->lookup_symbol = lookup_symbolxx;
  2388. s->next = syminfos;
  2389. syminfos = s;
  2390. return;
  2391. give_up:
  2392. g_free(s);
  2393. g_free(strings);
  2394. g_free(syms);
  2395. }
  2396. uint32_t get_elf_eflags(int fd)
  2397. {
  2398. struct elfhdr ehdr;
  2399. off_t offset;
  2400. int ret;
  2401. /* Read ELF header */
  2402. offset = lseek(fd, 0, SEEK_SET);
  2403. if (offset == (off_t) -1) {
  2404. return 0;
  2405. }
  2406. ret = read(fd, &ehdr, sizeof(ehdr));
  2407. if (ret < sizeof(ehdr)) {
  2408. return 0;
  2409. }
  2410. offset = lseek(fd, offset, SEEK_SET);
  2411. if (offset == (off_t) -1) {
  2412. return 0;
  2413. }
  2414. /* Check ELF signature */
  2415. if (!elf_check_ident(&ehdr)) {
  2416. return 0;
  2417. }
  2418. /* check header */
  2419. bswap_ehdr(&ehdr);
  2420. if (!elf_check_ehdr(&ehdr)) {
  2421. return 0;
  2422. }
  2423. /* return architecture id */
  2424. return ehdr.e_flags;
  2425. }
  2426. int load_elf_binary(struct linux_binprm *bprm, struct image_info *info)
  2427. {
  2428. struct image_info interp_info;
  2429. struct elfhdr elf_ex;
  2430. char *elf_interpreter = NULL;
  2431. char *scratch;
  2432. memset(&interp_info, 0, sizeof(interp_info));
  2433. #ifdef TARGET_MIPS
  2434. interp_info.fp_abi = MIPS_ABI_FP_UNKNOWN;
  2435. #endif
  2436. info->start_mmap = (abi_ulong)ELF_START_MMAP;
  2437. load_elf_image(bprm->filename, bprm->fd, info,
  2438. &elf_interpreter, bprm->buf);
  2439. /* ??? We need a copy of the elf header for passing to create_elf_tables.
  2440. If we do nothing, we'll have overwritten this when we re-use bprm->buf
  2441. when we load the interpreter. */
  2442. elf_ex = *(struct elfhdr *)bprm->buf;
  2443. /* Do this so that we can load the interpreter, if need be. We will
  2444. change some of these later */
  2445. bprm->p = setup_arg_pages(bprm, info);
  2446. scratch = g_new0(char, TARGET_PAGE_SIZE);
  2447. if (STACK_GROWS_DOWN) {
  2448. bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
  2449. bprm->p, info->stack_limit);
  2450. info->file_string = bprm->p;
  2451. bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
  2452. bprm->p, info->stack_limit);
  2453. info->env_strings = bprm->p;
  2454. bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
  2455. bprm->p, info->stack_limit);
  2456. info->arg_strings = bprm->p;
  2457. } else {
  2458. info->arg_strings = bprm->p;
  2459. bprm->p = copy_elf_strings(bprm->argc, bprm->argv, scratch,
  2460. bprm->p, info->stack_limit);
  2461. info->env_strings = bprm->p;
  2462. bprm->p = copy_elf_strings(bprm->envc, bprm->envp, scratch,
  2463. bprm->p, info->stack_limit);
  2464. info->file_string = bprm->p;
  2465. bprm->p = copy_elf_strings(1, &bprm->filename, scratch,
  2466. bprm->p, info->stack_limit);
  2467. }
  2468. g_free(scratch);
  2469. if (!bprm->p) {
  2470. fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
  2471. exit(-1);
  2472. }
  2473. if (elf_interpreter) {
  2474. load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
  2475. /* If the program interpreter is one of these two, then assume
  2476. an iBCS2 image. Otherwise assume a native linux image. */
  2477. if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
  2478. || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
  2479. info->personality = PER_SVR4;
  2480. /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
  2481. and some applications "depend" upon this behavior. Since
  2482. we do not have the power to recompile these, we emulate
  2483. the SVr4 behavior. Sigh. */
  2484. target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
  2485. MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  2486. }
  2487. #ifdef TARGET_MIPS
  2488. info->interp_fp_abi = interp_info.fp_abi;
  2489. #endif
  2490. }
  2491. bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
  2492. info, (elf_interpreter ? &interp_info : NULL));
  2493. info->start_stack = bprm->p;
  2494. /* If we have an interpreter, set that as the program's entry point.
  2495. Copy the load_bias as well, to help PPC64 interpret the entry
  2496. point as a function descriptor. Do this after creating elf tables
  2497. so that we copy the original program entry point into the AUXV. */
  2498. if (elf_interpreter) {
  2499. info->load_bias = interp_info.load_bias;
  2500. info->entry = interp_info.entry;
  2501. free(elf_interpreter);
  2502. }
  2503. #ifdef USE_ELF_CORE_DUMP
  2504. bprm->core_dump = &elf_core_dump;
  2505. #endif
  2506. /*
  2507. * If we reserved extra space for brk, release it now.
  2508. * The implementation of do_brk in syscalls.c expects to be able
  2509. * to mmap pages in this space.
  2510. */
  2511. if (info->reserve_brk) {
  2512. abi_ulong start_brk = HOST_PAGE_ALIGN(info->brk);
  2513. abi_ulong end_brk = HOST_PAGE_ALIGN(info->brk + info->reserve_brk);
  2514. target_munmap(start_brk, end_brk - start_brk);
  2515. }
  2516. return 0;
  2517. }
  2518. #ifdef USE_ELF_CORE_DUMP
  2519. /*
  2520. * Definitions to generate Intel SVR4-like core files.
  2521. * These mostly have the same names as the SVR4 types with "target_elf_"
  2522. * tacked on the front to prevent clashes with linux definitions,
  2523. * and the typedef forms have been avoided. This is mostly like
  2524. * the SVR4 structure, but more Linuxy, with things that Linux does
  2525. * not support and which gdb doesn't really use excluded.
  2526. *
  2527. * Fields we don't dump (their contents is zero) in linux-user qemu
  2528. * are marked with XXX.
  2529. *
  2530. * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
  2531. *
  2532. * Porting ELF coredump for target is (quite) simple process. First you
  2533. * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
  2534. * the target resides):
  2535. *
  2536. * #define USE_ELF_CORE_DUMP
  2537. *
  2538. * Next you define type of register set used for dumping. ELF specification
  2539. * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
  2540. *
  2541. * typedef <target_regtype> target_elf_greg_t;
  2542. * #define ELF_NREG <number of registers>
  2543. * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
  2544. *
  2545. * Last step is to implement target specific function that copies registers
  2546. * from given cpu into just specified register set. Prototype is:
  2547. *
  2548. * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
  2549. * const CPUArchState *env);
  2550. *
  2551. * Parameters:
  2552. * regs - copy register values into here (allocated and zeroed by caller)
  2553. * env - copy registers from here
  2554. *
  2555. * Example for ARM target is provided in this file.
  2556. */
  2557. /* An ELF note in memory */
  2558. struct memelfnote {
  2559. const char *name;
  2560. size_t namesz;
  2561. size_t namesz_rounded;
  2562. int type;
  2563. size_t datasz;
  2564. size_t datasz_rounded;
  2565. void *data;
  2566. size_t notesz;
  2567. };
  2568. struct target_elf_siginfo {
  2569. abi_int si_signo; /* signal number */
  2570. abi_int si_code; /* extra code */
  2571. abi_int si_errno; /* errno */
  2572. };
  2573. struct target_elf_prstatus {
  2574. struct target_elf_siginfo pr_info; /* Info associated with signal */
  2575. abi_short pr_cursig; /* Current signal */
  2576. abi_ulong pr_sigpend; /* XXX */
  2577. abi_ulong pr_sighold; /* XXX */
  2578. target_pid_t pr_pid;
  2579. target_pid_t pr_ppid;
  2580. target_pid_t pr_pgrp;
  2581. target_pid_t pr_sid;
  2582. struct target_timeval pr_utime; /* XXX User time */
  2583. struct target_timeval pr_stime; /* XXX System time */
  2584. struct target_timeval pr_cutime; /* XXX Cumulative user time */
  2585. struct target_timeval pr_cstime; /* XXX Cumulative system time */
  2586. target_elf_gregset_t pr_reg; /* GP registers */
  2587. abi_int pr_fpvalid; /* XXX */
  2588. };
  2589. #define ELF_PRARGSZ (80) /* Number of chars for args */
  2590. struct target_elf_prpsinfo {
  2591. char pr_state; /* numeric process state */
  2592. char pr_sname; /* char for pr_state */
  2593. char pr_zomb; /* zombie */
  2594. char pr_nice; /* nice val */
  2595. abi_ulong pr_flag; /* flags */
  2596. target_uid_t pr_uid;
  2597. target_gid_t pr_gid;
  2598. target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
  2599. /* Lots missing */
  2600. char pr_fname[16] QEMU_NONSTRING; /* filename of executable */
  2601. char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
  2602. };
  2603. /* Here is the structure in which status of each thread is captured. */
  2604. struct elf_thread_status {
  2605. QTAILQ_ENTRY(elf_thread_status) ets_link;
  2606. struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
  2607. #if 0
  2608. elf_fpregset_t fpu; /* NT_PRFPREG */
  2609. struct task_struct *thread;
  2610. elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
  2611. #endif
  2612. struct memelfnote notes[1];
  2613. int num_notes;
  2614. };
  2615. struct elf_note_info {
  2616. struct memelfnote *notes;
  2617. struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
  2618. struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
  2619. QTAILQ_HEAD(, elf_thread_status) thread_list;
  2620. #if 0
  2621. /*
  2622. * Current version of ELF coredump doesn't support
  2623. * dumping fp regs etc.
  2624. */
  2625. elf_fpregset_t *fpu;
  2626. elf_fpxregset_t *xfpu;
  2627. int thread_status_size;
  2628. #endif
  2629. int notes_size;
  2630. int numnote;
  2631. };
  2632. struct vm_area_struct {
  2633. target_ulong vma_start; /* start vaddr of memory region */
  2634. target_ulong vma_end; /* end vaddr of memory region */
  2635. abi_ulong vma_flags; /* protection etc. flags for the region */
  2636. QTAILQ_ENTRY(vm_area_struct) vma_link;
  2637. };
  2638. struct mm_struct {
  2639. QTAILQ_HEAD(, vm_area_struct) mm_mmap;
  2640. int mm_count; /* number of mappings */
  2641. };
  2642. static struct mm_struct *vma_init(void);
  2643. static void vma_delete(struct mm_struct *);
  2644. static int vma_add_mapping(struct mm_struct *, target_ulong,
  2645. target_ulong, abi_ulong);
  2646. static int vma_get_mapping_count(const struct mm_struct *);
  2647. static struct vm_area_struct *vma_first(const struct mm_struct *);
  2648. static struct vm_area_struct *vma_next(struct vm_area_struct *);
  2649. static abi_ulong vma_dump_size(const struct vm_area_struct *);
  2650. static int vma_walker(void *priv, target_ulong start, target_ulong end,
  2651. unsigned long flags);
  2652. static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
  2653. static void fill_note(struct memelfnote *, const char *, int,
  2654. unsigned int, void *);
  2655. static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
  2656. static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
  2657. static void fill_auxv_note(struct memelfnote *, const TaskState *);
  2658. static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
  2659. static size_t note_size(const struct memelfnote *);
  2660. static void free_note_info(struct elf_note_info *);
  2661. static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
  2662. static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
  2663. static int core_dump_filename(const TaskState *, char *, size_t);
  2664. static int dump_write(int, const void *, size_t);
  2665. static int write_note(struct memelfnote *, int);
  2666. static int write_note_info(struct elf_note_info *, int);
  2667. #ifdef BSWAP_NEEDED
  2668. static void bswap_prstatus(struct target_elf_prstatus *prstatus)
  2669. {
  2670. prstatus->pr_info.si_signo = tswap32(prstatus->pr_info.si_signo);
  2671. prstatus->pr_info.si_code = tswap32(prstatus->pr_info.si_code);
  2672. prstatus->pr_info.si_errno = tswap32(prstatus->pr_info.si_errno);
  2673. prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
  2674. prstatus->pr_sigpend = tswapal(prstatus->pr_sigpend);
  2675. prstatus->pr_sighold = tswapal(prstatus->pr_sighold);
  2676. prstatus->pr_pid = tswap32(prstatus->pr_pid);
  2677. prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
  2678. prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
  2679. prstatus->pr_sid = tswap32(prstatus->pr_sid);
  2680. /* cpu times are not filled, so we skip them */
  2681. /* regs should be in correct format already */
  2682. prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
  2683. }
  2684. static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
  2685. {
  2686. psinfo->pr_flag = tswapal(psinfo->pr_flag);
  2687. psinfo->pr_uid = tswap16(psinfo->pr_uid);
  2688. psinfo->pr_gid = tswap16(psinfo->pr_gid);
  2689. psinfo->pr_pid = tswap32(psinfo->pr_pid);
  2690. psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
  2691. psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
  2692. psinfo->pr_sid = tswap32(psinfo->pr_sid);
  2693. }
  2694. static void bswap_note(struct elf_note *en)
  2695. {
  2696. bswap32s(&en->n_namesz);
  2697. bswap32s(&en->n_descsz);
  2698. bswap32s(&en->n_type);
  2699. }
  2700. #else
  2701. static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
  2702. static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
  2703. static inline void bswap_note(struct elf_note *en) { }
  2704. #endif /* BSWAP_NEEDED */
  2705. /*
  2706. * Minimal support for linux memory regions. These are needed
  2707. * when we are finding out what memory exactly belongs to
  2708. * emulated process. No locks needed here, as long as
  2709. * thread that received the signal is stopped.
  2710. */
  2711. static struct mm_struct *vma_init(void)
  2712. {
  2713. struct mm_struct *mm;
  2714. if ((mm = g_malloc(sizeof (*mm))) == NULL)
  2715. return (NULL);
  2716. mm->mm_count = 0;
  2717. QTAILQ_INIT(&mm->mm_mmap);
  2718. return (mm);
  2719. }
  2720. static void vma_delete(struct mm_struct *mm)
  2721. {
  2722. struct vm_area_struct *vma;
  2723. while ((vma = vma_first(mm)) != NULL) {
  2724. QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
  2725. g_free(vma);
  2726. }
  2727. g_free(mm);
  2728. }
  2729. static int vma_add_mapping(struct mm_struct *mm, target_ulong start,
  2730. target_ulong end, abi_ulong flags)
  2731. {
  2732. struct vm_area_struct *vma;
  2733. if ((vma = g_malloc0(sizeof (*vma))) == NULL)
  2734. return (-1);
  2735. vma->vma_start = start;
  2736. vma->vma_end = end;
  2737. vma->vma_flags = flags;
  2738. QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
  2739. mm->mm_count++;
  2740. return (0);
  2741. }
  2742. static struct vm_area_struct *vma_first(const struct mm_struct *mm)
  2743. {
  2744. return (QTAILQ_FIRST(&mm->mm_mmap));
  2745. }
  2746. static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
  2747. {
  2748. return (QTAILQ_NEXT(vma, vma_link));
  2749. }
  2750. static int vma_get_mapping_count(const struct mm_struct *mm)
  2751. {
  2752. return (mm->mm_count);
  2753. }
  2754. /*
  2755. * Calculate file (dump) size of given memory region.
  2756. */
  2757. static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
  2758. {
  2759. /* if we cannot even read the first page, skip it */
  2760. if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
  2761. return (0);
  2762. /*
  2763. * Usually we don't dump executable pages as they contain
  2764. * non-writable code that debugger can read directly from
  2765. * target library etc. However, thread stacks are marked
  2766. * also executable so we read in first page of given region
  2767. * and check whether it contains elf header. If there is
  2768. * no elf header, we dump it.
  2769. */
  2770. if (vma->vma_flags & PROT_EXEC) {
  2771. char page[TARGET_PAGE_SIZE];
  2772. copy_from_user(page, vma->vma_start, sizeof (page));
  2773. if ((page[EI_MAG0] == ELFMAG0) &&
  2774. (page[EI_MAG1] == ELFMAG1) &&
  2775. (page[EI_MAG2] == ELFMAG2) &&
  2776. (page[EI_MAG3] == ELFMAG3)) {
  2777. /*
  2778. * Mappings are possibly from ELF binary. Don't dump
  2779. * them.
  2780. */
  2781. return (0);
  2782. }
  2783. }
  2784. return (vma->vma_end - vma->vma_start);
  2785. }
  2786. static int vma_walker(void *priv, target_ulong start, target_ulong end,
  2787. unsigned long flags)
  2788. {
  2789. struct mm_struct *mm = (struct mm_struct *)priv;
  2790. vma_add_mapping(mm, start, end, flags);
  2791. return (0);
  2792. }
  2793. static void fill_note(struct memelfnote *note, const char *name, int type,
  2794. unsigned int sz, void *data)
  2795. {
  2796. unsigned int namesz;
  2797. namesz = strlen(name) + 1;
  2798. note->name = name;
  2799. note->namesz = namesz;
  2800. note->namesz_rounded = roundup(namesz, sizeof (int32_t));
  2801. note->type = type;
  2802. note->datasz = sz;
  2803. note->datasz_rounded = roundup(sz, sizeof (int32_t));
  2804. note->data = data;
  2805. /*
  2806. * We calculate rounded up note size here as specified by
  2807. * ELF document.
  2808. */
  2809. note->notesz = sizeof (struct elf_note) +
  2810. note->namesz_rounded + note->datasz_rounded;
  2811. }
  2812. static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
  2813. uint32_t flags)
  2814. {
  2815. (void) memset(elf, 0, sizeof(*elf));
  2816. (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
  2817. elf->e_ident[EI_CLASS] = ELF_CLASS;
  2818. elf->e_ident[EI_DATA] = ELF_DATA;
  2819. elf->e_ident[EI_VERSION] = EV_CURRENT;
  2820. elf->e_ident[EI_OSABI] = ELF_OSABI;
  2821. elf->e_type = ET_CORE;
  2822. elf->e_machine = machine;
  2823. elf->e_version = EV_CURRENT;
  2824. elf->e_phoff = sizeof(struct elfhdr);
  2825. elf->e_flags = flags;
  2826. elf->e_ehsize = sizeof(struct elfhdr);
  2827. elf->e_phentsize = sizeof(struct elf_phdr);
  2828. elf->e_phnum = segs;
  2829. bswap_ehdr(elf);
  2830. }
  2831. static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
  2832. {
  2833. phdr->p_type = PT_NOTE;
  2834. phdr->p_offset = offset;
  2835. phdr->p_vaddr = 0;
  2836. phdr->p_paddr = 0;
  2837. phdr->p_filesz = sz;
  2838. phdr->p_memsz = 0;
  2839. phdr->p_flags = 0;
  2840. phdr->p_align = 0;
  2841. bswap_phdr(phdr, 1);
  2842. }
  2843. static size_t note_size(const struct memelfnote *note)
  2844. {
  2845. return (note->notesz);
  2846. }
  2847. static void fill_prstatus(struct target_elf_prstatus *prstatus,
  2848. const TaskState *ts, int signr)
  2849. {
  2850. (void) memset(prstatus, 0, sizeof (*prstatus));
  2851. prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
  2852. prstatus->pr_pid = ts->ts_tid;
  2853. prstatus->pr_ppid = getppid();
  2854. prstatus->pr_pgrp = getpgrp();
  2855. prstatus->pr_sid = getsid(0);
  2856. bswap_prstatus(prstatus);
  2857. }
  2858. static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
  2859. {
  2860. char *base_filename;
  2861. unsigned int i, len;
  2862. (void) memset(psinfo, 0, sizeof (*psinfo));
  2863. len = ts->info->arg_end - ts->info->arg_start;
  2864. if (len >= ELF_PRARGSZ)
  2865. len = ELF_PRARGSZ - 1;
  2866. if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
  2867. return -EFAULT;
  2868. for (i = 0; i < len; i++)
  2869. if (psinfo->pr_psargs[i] == 0)
  2870. psinfo->pr_psargs[i] = ' ';
  2871. psinfo->pr_psargs[len] = 0;
  2872. psinfo->pr_pid = getpid();
  2873. psinfo->pr_ppid = getppid();
  2874. psinfo->pr_pgrp = getpgrp();
  2875. psinfo->pr_sid = getsid(0);
  2876. psinfo->pr_uid = getuid();
  2877. psinfo->pr_gid = getgid();
  2878. base_filename = g_path_get_basename(ts->bprm->filename);
  2879. /*
  2880. * Using strncpy here is fine: at max-length,
  2881. * this field is not NUL-terminated.
  2882. */
  2883. (void) strncpy(psinfo->pr_fname, base_filename,
  2884. sizeof(psinfo->pr_fname));
  2885. g_free(base_filename);
  2886. bswap_psinfo(psinfo);
  2887. return (0);
  2888. }
  2889. static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
  2890. {
  2891. elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
  2892. elf_addr_t orig_auxv = auxv;
  2893. void *ptr;
  2894. int len = ts->info->auxv_len;
  2895. /*
  2896. * Auxiliary vector is stored in target process stack. It contains
  2897. * {type, value} pairs that we need to dump into note. This is not
  2898. * strictly necessary but we do it here for sake of completeness.
  2899. */
  2900. /* read in whole auxv vector and copy it to memelfnote */
  2901. ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
  2902. if (ptr != NULL) {
  2903. fill_note(note, "CORE", NT_AUXV, len, ptr);
  2904. unlock_user(ptr, auxv, len);
  2905. }
  2906. }
  2907. /*
  2908. * Constructs name of coredump file. We have following convention
  2909. * for the name:
  2910. * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
  2911. *
  2912. * Returns 0 in case of success, -1 otherwise (errno is set).
  2913. */
  2914. static int core_dump_filename(const TaskState *ts, char *buf,
  2915. size_t bufsize)
  2916. {
  2917. char timestamp[64];
  2918. char *base_filename = NULL;
  2919. struct timeval tv;
  2920. struct tm tm;
  2921. assert(bufsize >= PATH_MAX);
  2922. if (gettimeofday(&tv, NULL) < 0) {
  2923. (void) fprintf(stderr, "unable to get current timestamp: %s",
  2924. strerror(errno));
  2925. return (-1);
  2926. }
  2927. base_filename = g_path_get_basename(ts->bprm->filename);
  2928. (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
  2929. localtime_r(&tv.tv_sec, &tm));
  2930. (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
  2931. base_filename, timestamp, (int)getpid());
  2932. g_free(base_filename);
  2933. return (0);
  2934. }
  2935. static int dump_write(int fd, const void *ptr, size_t size)
  2936. {
  2937. const char *bufp = (const char *)ptr;
  2938. ssize_t bytes_written, bytes_left;
  2939. struct rlimit dumpsize;
  2940. off_t pos;
  2941. bytes_written = 0;
  2942. getrlimit(RLIMIT_CORE, &dumpsize);
  2943. if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
  2944. if (errno == ESPIPE) { /* not a seekable stream */
  2945. bytes_left = size;
  2946. } else {
  2947. return pos;
  2948. }
  2949. } else {
  2950. if (dumpsize.rlim_cur <= pos) {
  2951. return -1;
  2952. } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
  2953. bytes_left = size;
  2954. } else {
  2955. size_t limit_left=dumpsize.rlim_cur - pos;
  2956. bytes_left = limit_left >= size ? size : limit_left ;
  2957. }
  2958. }
  2959. /*
  2960. * In normal conditions, single write(2) should do but
  2961. * in case of socket etc. this mechanism is more portable.
  2962. */
  2963. do {
  2964. bytes_written = write(fd, bufp, bytes_left);
  2965. if (bytes_written < 0) {
  2966. if (errno == EINTR)
  2967. continue;
  2968. return (-1);
  2969. } else if (bytes_written == 0) { /* eof */
  2970. return (-1);
  2971. }
  2972. bufp += bytes_written;
  2973. bytes_left -= bytes_written;
  2974. } while (bytes_left > 0);
  2975. return (0);
  2976. }
  2977. static int write_note(struct memelfnote *men, int fd)
  2978. {
  2979. struct elf_note en;
  2980. en.n_namesz = men->namesz;
  2981. en.n_type = men->type;
  2982. en.n_descsz = men->datasz;
  2983. bswap_note(&en);
  2984. if (dump_write(fd, &en, sizeof(en)) != 0)
  2985. return (-1);
  2986. if (dump_write(fd, men->name, men->namesz_rounded) != 0)
  2987. return (-1);
  2988. if (dump_write(fd, men->data, men->datasz_rounded) != 0)
  2989. return (-1);
  2990. return (0);
  2991. }
  2992. static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
  2993. {
  2994. CPUState *cpu = env_cpu((CPUArchState *)env);
  2995. TaskState *ts = (TaskState *)cpu->opaque;
  2996. struct elf_thread_status *ets;
  2997. ets = g_malloc0(sizeof (*ets));
  2998. ets->num_notes = 1; /* only prstatus is dumped */
  2999. fill_prstatus(&ets->prstatus, ts, 0);
  3000. elf_core_copy_regs(&ets->prstatus.pr_reg, env);
  3001. fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
  3002. &ets->prstatus);
  3003. QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
  3004. info->notes_size += note_size(&ets->notes[0]);
  3005. }
  3006. static void init_note_info(struct elf_note_info *info)
  3007. {
  3008. /* Initialize the elf_note_info structure so that it is at
  3009. * least safe to call free_note_info() on it. Must be
  3010. * called before calling fill_note_info().
  3011. */
  3012. memset(info, 0, sizeof (*info));
  3013. QTAILQ_INIT(&info->thread_list);
  3014. }
  3015. static int fill_note_info(struct elf_note_info *info,
  3016. long signr, const CPUArchState *env)
  3017. {
  3018. #define NUMNOTES 3
  3019. CPUState *cpu = env_cpu((CPUArchState *)env);
  3020. TaskState *ts = (TaskState *)cpu->opaque;
  3021. int i;
  3022. info->notes = g_new0(struct memelfnote, NUMNOTES);
  3023. if (info->notes == NULL)
  3024. return (-ENOMEM);
  3025. info->prstatus = g_malloc0(sizeof (*info->prstatus));
  3026. if (info->prstatus == NULL)
  3027. return (-ENOMEM);
  3028. info->psinfo = g_malloc0(sizeof (*info->psinfo));
  3029. if (info->prstatus == NULL)
  3030. return (-ENOMEM);
  3031. /*
  3032. * First fill in status (and registers) of current thread
  3033. * including process info & aux vector.
  3034. */
  3035. fill_prstatus(info->prstatus, ts, signr);
  3036. elf_core_copy_regs(&info->prstatus->pr_reg, env);
  3037. fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
  3038. sizeof (*info->prstatus), info->prstatus);
  3039. fill_psinfo(info->psinfo, ts);
  3040. fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
  3041. sizeof (*info->psinfo), info->psinfo);
  3042. fill_auxv_note(&info->notes[2], ts);
  3043. info->numnote = 3;
  3044. info->notes_size = 0;
  3045. for (i = 0; i < info->numnote; i++)
  3046. info->notes_size += note_size(&info->notes[i]);
  3047. /* read and fill status of all threads */
  3048. cpu_list_lock();
  3049. CPU_FOREACH(cpu) {
  3050. if (cpu == thread_cpu) {
  3051. continue;
  3052. }
  3053. fill_thread_info(info, (CPUArchState *)cpu->env_ptr);
  3054. }
  3055. cpu_list_unlock();
  3056. return (0);
  3057. }
  3058. static void free_note_info(struct elf_note_info *info)
  3059. {
  3060. struct elf_thread_status *ets;
  3061. while (!QTAILQ_EMPTY(&info->thread_list)) {
  3062. ets = QTAILQ_FIRST(&info->thread_list);
  3063. QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
  3064. g_free(ets);
  3065. }
  3066. g_free(info->prstatus);
  3067. g_free(info->psinfo);
  3068. g_free(info->notes);
  3069. }
  3070. static int write_note_info(struct elf_note_info *info, int fd)
  3071. {
  3072. struct elf_thread_status *ets;
  3073. int i, error = 0;
  3074. /* write prstatus, psinfo and auxv for current thread */
  3075. for (i = 0; i < info->numnote; i++)
  3076. if ((error = write_note(&info->notes[i], fd)) != 0)
  3077. return (error);
  3078. /* write prstatus for each thread */
  3079. QTAILQ_FOREACH(ets, &info->thread_list, ets_link) {
  3080. if ((error = write_note(&ets->notes[0], fd)) != 0)
  3081. return (error);
  3082. }
  3083. return (0);
  3084. }
  3085. /*
  3086. * Write out ELF coredump.
  3087. *
  3088. * See documentation of ELF object file format in:
  3089. * http://www.caldera.com/developers/devspecs/gabi41.pdf
  3090. *
  3091. * Coredump format in linux is following:
  3092. *
  3093. * 0 +----------------------+ \
  3094. * | ELF header | ET_CORE |
  3095. * +----------------------+ |
  3096. * | ELF program headers | |--- headers
  3097. * | - NOTE section | |
  3098. * | - PT_LOAD sections | |
  3099. * +----------------------+ /
  3100. * | NOTEs: |
  3101. * | - NT_PRSTATUS |
  3102. * | - NT_PRSINFO |
  3103. * | - NT_AUXV |
  3104. * +----------------------+ <-- aligned to target page
  3105. * | Process memory dump |
  3106. * : :
  3107. * . .
  3108. * : :
  3109. * | |
  3110. * +----------------------+
  3111. *
  3112. * NT_PRSTATUS -> struct elf_prstatus (per thread)
  3113. * NT_PRSINFO -> struct elf_prpsinfo
  3114. * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
  3115. *
  3116. * Format follows System V format as close as possible. Current
  3117. * version limitations are as follows:
  3118. * - no floating point registers are dumped
  3119. *
  3120. * Function returns 0 in case of success, negative errno otherwise.
  3121. *
  3122. * TODO: make this work also during runtime: it should be
  3123. * possible to force coredump from running process and then
  3124. * continue processing. For example qemu could set up SIGUSR2
  3125. * handler (provided that target process haven't registered
  3126. * handler for that) that does the dump when signal is received.
  3127. */
  3128. static int elf_core_dump(int signr, const CPUArchState *env)
  3129. {
  3130. const CPUState *cpu = env_cpu((CPUArchState *)env);
  3131. const TaskState *ts = (const TaskState *)cpu->opaque;
  3132. struct vm_area_struct *vma = NULL;
  3133. char corefile[PATH_MAX];
  3134. struct elf_note_info info;
  3135. struct elfhdr elf;
  3136. struct elf_phdr phdr;
  3137. struct rlimit dumpsize;
  3138. struct mm_struct *mm = NULL;
  3139. off_t offset = 0, data_offset = 0;
  3140. int segs = 0;
  3141. int fd = -1;
  3142. init_note_info(&info);
  3143. errno = 0;
  3144. getrlimit(RLIMIT_CORE, &dumpsize);
  3145. if (dumpsize.rlim_cur == 0)
  3146. return 0;
  3147. if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
  3148. return (-errno);
  3149. if ((fd = open(corefile, O_WRONLY | O_CREAT,
  3150. S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
  3151. return (-errno);
  3152. /*
  3153. * Walk through target process memory mappings and
  3154. * set up structure containing this information. After
  3155. * this point vma_xxx functions can be used.
  3156. */
  3157. if ((mm = vma_init()) == NULL)
  3158. goto out;
  3159. walk_memory_regions(mm, vma_walker);
  3160. segs = vma_get_mapping_count(mm);
  3161. /*
  3162. * Construct valid coredump ELF header. We also
  3163. * add one more segment for notes.
  3164. */
  3165. fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
  3166. if (dump_write(fd, &elf, sizeof (elf)) != 0)
  3167. goto out;
  3168. /* fill in the in-memory version of notes */
  3169. if (fill_note_info(&info, signr, env) < 0)
  3170. goto out;
  3171. offset += sizeof (elf); /* elf header */
  3172. offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
  3173. /* write out notes program header */
  3174. fill_elf_note_phdr(&phdr, info.notes_size, offset);
  3175. offset += info.notes_size;
  3176. if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
  3177. goto out;
  3178. /*
  3179. * ELF specification wants data to start at page boundary so
  3180. * we align it here.
  3181. */
  3182. data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
  3183. /*
  3184. * Write program headers for memory regions mapped in
  3185. * the target process.
  3186. */
  3187. for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
  3188. (void) memset(&phdr, 0, sizeof (phdr));
  3189. phdr.p_type = PT_LOAD;
  3190. phdr.p_offset = offset;
  3191. phdr.p_vaddr = vma->vma_start;
  3192. phdr.p_paddr = 0;
  3193. phdr.p_filesz = vma_dump_size(vma);
  3194. offset += phdr.p_filesz;
  3195. phdr.p_memsz = vma->vma_end - vma->vma_start;
  3196. phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
  3197. if (vma->vma_flags & PROT_WRITE)
  3198. phdr.p_flags |= PF_W;
  3199. if (vma->vma_flags & PROT_EXEC)
  3200. phdr.p_flags |= PF_X;
  3201. phdr.p_align = ELF_EXEC_PAGESIZE;
  3202. bswap_phdr(&phdr, 1);
  3203. if (dump_write(fd, &phdr, sizeof(phdr)) != 0) {
  3204. goto out;
  3205. }
  3206. }
  3207. /*
  3208. * Next we write notes just after program headers. No
  3209. * alignment needed here.
  3210. */
  3211. if (write_note_info(&info, fd) < 0)
  3212. goto out;
  3213. /* align data to page boundary */
  3214. if (lseek(fd, data_offset, SEEK_SET) != data_offset)
  3215. goto out;
  3216. /*
  3217. * Finally we can dump process memory into corefile as well.
  3218. */
  3219. for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
  3220. abi_ulong addr;
  3221. abi_ulong end;
  3222. end = vma->vma_start + vma_dump_size(vma);
  3223. for (addr = vma->vma_start; addr < end;
  3224. addr += TARGET_PAGE_SIZE) {
  3225. char page[TARGET_PAGE_SIZE];
  3226. int error;
  3227. /*
  3228. * Read in page from target process memory and
  3229. * write it to coredump file.
  3230. */
  3231. error = copy_from_user(page, addr, sizeof (page));
  3232. if (error != 0) {
  3233. (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
  3234. addr);
  3235. errno = -error;
  3236. goto out;
  3237. }
  3238. if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
  3239. goto out;
  3240. }
  3241. }
  3242. out:
  3243. free_note_info(&info);
  3244. if (mm != NULL)
  3245. vma_delete(mm);
  3246. (void) close(fd);
  3247. if (errno != 0)
  3248. return (-errno);
  3249. return (0);
  3250. }
  3251. #endif /* USE_ELF_CORE_DUMP */
  3252. void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
  3253. {
  3254. init_thread(regs, infop);
  3255. }