2
0

elfload.c 87 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896
  1. /* This is the Linux kernel elf-loading code, ported into user space */
  2. #include <sys/time.h>
  3. #include <sys/param.h>
  4. #include <stdio.h>
  5. #include <sys/types.h>
  6. #include <fcntl.h>
  7. #include <errno.h>
  8. #include <unistd.h>
  9. #include <sys/mman.h>
  10. #include <sys/resource.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include <time.h>
  14. #include "qemu.h"
  15. #include "disas/disas.h"
  16. #ifdef _ARCH_PPC64
  17. #undef ARCH_DLINFO
  18. #undef ELF_PLATFORM
  19. #undef ELF_HWCAP
  20. #undef ELF_CLASS
  21. #undef ELF_DATA
  22. #undef ELF_ARCH
  23. #endif
  24. #define ELF_OSABI ELFOSABI_SYSV
  25. /* from personality.h */
  26. /*
  27. * Flags for bug emulation.
  28. *
  29. * These occupy the top three bytes.
  30. */
  31. enum {
  32. ADDR_NO_RANDOMIZE = 0x0040000, /* disable randomization of VA space */
  33. FDPIC_FUNCPTRS = 0x0080000, /* userspace function ptrs point to
  34. descriptors (signal handling) */
  35. MMAP_PAGE_ZERO = 0x0100000,
  36. ADDR_COMPAT_LAYOUT = 0x0200000,
  37. READ_IMPLIES_EXEC = 0x0400000,
  38. ADDR_LIMIT_32BIT = 0x0800000,
  39. SHORT_INODE = 0x1000000,
  40. WHOLE_SECONDS = 0x2000000,
  41. STICKY_TIMEOUTS = 0x4000000,
  42. ADDR_LIMIT_3GB = 0x8000000,
  43. };
  44. /*
  45. * Personality types.
  46. *
  47. * These go in the low byte. Avoid using the top bit, it will
  48. * conflict with error returns.
  49. */
  50. enum {
  51. PER_LINUX = 0x0000,
  52. PER_LINUX_32BIT = 0x0000 | ADDR_LIMIT_32BIT,
  53. PER_LINUX_FDPIC = 0x0000 | FDPIC_FUNCPTRS,
  54. PER_SVR4 = 0x0001 | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
  55. PER_SVR3 = 0x0002 | STICKY_TIMEOUTS | SHORT_INODE,
  56. PER_SCOSVR3 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS | SHORT_INODE,
  57. PER_OSR5 = 0x0003 | STICKY_TIMEOUTS | WHOLE_SECONDS,
  58. PER_WYSEV386 = 0x0004 | STICKY_TIMEOUTS | SHORT_INODE,
  59. PER_ISCR4 = 0x0005 | STICKY_TIMEOUTS,
  60. PER_BSD = 0x0006,
  61. PER_SUNOS = 0x0006 | STICKY_TIMEOUTS,
  62. PER_XENIX = 0x0007 | STICKY_TIMEOUTS | SHORT_INODE,
  63. PER_LINUX32 = 0x0008,
  64. PER_LINUX32_3GB = 0x0008 | ADDR_LIMIT_3GB,
  65. PER_IRIX32 = 0x0009 | STICKY_TIMEOUTS,/* IRIX5 32-bit */
  66. PER_IRIXN32 = 0x000a | STICKY_TIMEOUTS,/* IRIX6 new 32-bit */
  67. PER_IRIX64 = 0x000b | STICKY_TIMEOUTS,/* IRIX6 64-bit */
  68. PER_RISCOS = 0x000c,
  69. PER_SOLARIS = 0x000d | STICKY_TIMEOUTS,
  70. PER_UW7 = 0x000e | STICKY_TIMEOUTS | MMAP_PAGE_ZERO,
  71. PER_OSF4 = 0x000f, /* OSF/1 v4 */
  72. PER_HPUX = 0x0010,
  73. PER_MASK = 0x00ff,
  74. };
  75. /*
  76. * Return the base personality without flags.
  77. */
  78. #define personality(pers) (pers & PER_MASK)
  79. /* this flag is uneffective under linux too, should be deleted */
  80. #ifndef MAP_DENYWRITE
  81. #define MAP_DENYWRITE 0
  82. #endif
  83. /* should probably go in elf.h */
  84. #ifndef ELIBBAD
  85. #define ELIBBAD 80
  86. #endif
  87. #ifdef TARGET_WORDS_BIGENDIAN
  88. #define ELF_DATA ELFDATA2MSB
  89. #else
  90. #define ELF_DATA ELFDATA2LSB
  91. #endif
  92. typedef target_ulong target_elf_greg_t;
  93. #ifdef USE_UID16
  94. typedef target_ushort target_uid_t;
  95. typedef target_ushort target_gid_t;
  96. #else
  97. typedef target_uint target_uid_t;
  98. typedef target_uint target_gid_t;
  99. #endif
  100. typedef target_int target_pid_t;
  101. #ifdef TARGET_I386
  102. #define ELF_PLATFORM get_elf_platform()
  103. static const char *get_elf_platform(void)
  104. {
  105. static char elf_platform[] = "i386";
  106. int family = (thread_env->cpuid_version >> 8) & 0xff;
  107. if (family > 6)
  108. family = 6;
  109. if (family >= 3)
  110. elf_platform[1] = '0' + family;
  111. return elf_platform;
  112. }
  113. #define ELF_HWCAP get_elf_hwcap()
  114. static uint32_t get_elf_hwcap(void)
  115. {
  116. return thread_env->cpuid_features;
  117. }
  118. #ifdef TARGET_X86_64
  119. #define ELF_START_MMAP 0x2aaaaab000ULL
  120. #define elf_check_arch(x) ( ((x) == ELF_ARCH) )
  121. #define ELF_CLASS ELFCLASS64
  122. #define ELF_ARCH EM_X86_64
  123. static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
  124. {
  125. regs->rax = 0;
  126. regs->rsp = infop->start_stack;
  127. regs->rip = infop->entry;
  128. }
  129. #define ELF_NREG 27
  130. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  131. /*
  132. * Note that ELF_NREG should be 29 as there should be place for
  133. * TRAPNO and ERR "registers" as well but linux doesn't dump
  134. * those.
  135. *
  136. * See linux kernel: arch/x86/include/asm/elf.h
  137. */
  138. static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
  139. {
  140. (*regs)[0] = env->regs[15];
  141. (*regs)[1] = env->regs[14];
  142. (*regs)[2] = env->regs[13];
  143. (*regs)[3] = env->regs[12];
  144. (*regs)[4] = env->regs[R_EBP];
  145. (*regs)[5] = env->regs[R_EBX];
  146. (*regs)[6] = env->regs[11];
  147. (*regs)[7] = env->regs[10];
  148. (*regs)[8] = env->regs[9];
  149. (*regs)[9] = env->regs[8];
  150. (*regs)[10] = env->regs[R_EAX];
  151. (*regs)[11] = env->regs[R_ECX];
  152. (*regs)[12] = env->regs[R_EDX];
  153. (*regs)[13] = env->regs[R_ESI];
  154. (*regs)[14] = env->regs[R_EDI];
  155. (*regs)[15] = env->regs[R_EAX]; /* XXX */
  156. (*regs)[16] = env->eip;
  157. (*regs)[17] = env->segs[R_CS].selector & 0xffff;
  158. (*regs)[18] = env->eflags;
  159. (*regs)[19] = env->regs[R_ESP];
  160. (*regs)[20] = env->segs[R_SS].selector & 0xffff;
  161. (*regs)[21] = env->segs[R_FS].selector & 0xffff;
  162. (*regs)[22] = env->segs[R_GS].selector & 0xffff;
  163. (*regs)[23] = env->segs[R_DS].selector & 0xffff;
  164. (*regs)[24] = env->segs[R_ES].selector & 0xffff;
  165. (*regs)[25] = env->segs[R_FS].selector & 0xffff;
  166. (*regs)[26] = env->segs[R_GS].selector & 0xffff;
  167. }
  168. #else
  169. #define ELF_START_MMAP 0x80000000
  170. /*
  171. * This is used to ensure we don't load something for the wrong architecture.
  172. */
  173. #define elf_check_arch(x) ( ((x) == EM_386) || ((x) == EM_486) )
  174. /*
  175. * These are used to set parameters in the core dumps.
  176. */
  177. #define ELF_CLASS ELFCLASS32
  178. #define ELF_ARCH EM_386
  179. static inline void init_thread(struct target_pt_regs *regs,
  180. struct image_info *infop)
  181. {
  182. regs->esp = infop->start_stack;
  183. regs->eip = infop->entry;
  184. /* SVR4/i386 ABI (pages 3-31, 3-32) says that when the program
  185. starts %edx contains a pointer to a function which might be
  186. registered using `atexit'. This provides a mean for the
  187. dynamic linker to call DT_FINI functions for shared libraries
  188. that have been loaded before the code runs.
  189. A value of 0 tells we have no such handler. */
  190. regs->edx = 0;
  191. }
  192. #define ELF_NREG 17
  193. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  194. /*
  195. * Note that ELF_NREG should be 19 as there should be place for
  196. * TRAPNO and ERR "registers" as well but linux doesn't dump
  197. * those.
  198. *
  199. * See linux kernel: arch/x86/include/asm/elf.h
  200. */
  201. static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUX86State *env)
  202. {
  203. (*regs)[0] = env->regs[R_EBX];
  204. (*regs)[1] = env->regs[R_ECX];
  205. (*regs)[2] = env->regs[R_EDX];
  206. (*regs)[3] = env->regs[R_ESI];
  207. (*regs)[4] = env->regs[R_EDI];
  208. (*regs)[5] = env->regs[R_EBP];
  209. (*regs)[6] = env->regs[R_EAX];
  210. (*regs)[7] = env->segs[R_DS].selector & 0xffff;
  211. (*regs)[8] = env->segs[R_ES].selector & 0xffff;
  212. (*regs)[9] = env->segs[R_FS].selector & 0xffff;
  213. (*regs)[10] = env->segs[R_GS].selector & 0xffff;
  214. (*regs)[11] = env->regs[R_EAX]; /* XXX */
  215. (*regs)[12] = env->eip;
  216. (*regs)[13] = env->segs[R_CS].selector & 0xffff;
  217. (*regs)[14] = env->eflags;
  218. (*regs)[15] = env->regs[R_ESP];
  219. (*regs)[16] = env->segs[R_SS].selector & 0xffff;
  220. }
  221. #endif
  222. #define USE_ELF_CORE_DUMP
  223. #define ELF_EXEC_PAGESIZE 4096
  224. #endif
  225. #ifdef TARGET_ARM
  226. #define ELF_START_MMAP 0x80000000
  227. #define elf_check_arch(x) ( (x) == EM_ARM )
  228. #define ELF_CLASS ELFCLASS32
  229. #define ELF_ARCH EM_ARM
  230. static inline void init_thread(struct target_pt_regs *regs,
  231. struct image_info *infop)
  232. {
  233. abi_long stack = infop->start_stack;
  234. memset(regs, 0, sizeof(*regs));
  235. regs->ARM_cpsr = 0x10;
  236. if (infop->entry & 1)
  237. regs->ARM_cpsr |= CPSR_T;
  238. regs->ARM_pc = infop->entry & 0xfffffffe;
  239. regs->ARM_sp = infop->start_stack;
  240. /* FIXME - what to for failure of get_user()? */
  241. get_user_ual(regs->ARM_r2, stack + 8); /* envp */
  242. get_user_ual(regs->ARM_r1, stack + 4); /* envp */
  243. /* XXX: it seems that r0 is zeroed after ! */
  244. regs->ARM_r0 = 0;
  245. /* For uClinux PIC binaries. */
  246. /* XXX: Linux does this only on ARM with no MMU (do we care ?) */
  247. regs->ARM_r10 = infop->start_data;
  248. }
  249. #define ELF_NREG 18
  250. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  251. static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUARMState *env)
  252. {
  253. (*regs)[0] = tswapl(env->regs[0]);
  254. (*regs)[1] = tswapl(env->regs[1]);
  255. (*regs)[2] = tswapl(env->regs[2]);
  256. (*regs)[3] = tswapl(env->regs[3]);
  257. (*regs)[4] = tswapl(env->regs[4]);
  258. (*regs)[5] = tswapl(env->regs[5]);
  259. (*regs)[6] = tswapl(env->regs[6]);
  260. (*regs)[7] = tswapl(env->regs[7]);
  261. (*regs)[8] = tswapl(env->regs[8]);
  262. (*regs)[9] = tswapl(env->regs[9]);
  263. (*regs)[10] = tswapl(env->regs[10]);
  264. (*regs)[11] = tswapl(env->regs[11]);
  265. (*regs)[12] = tswapl(env->regs[12]);
  266. (*regs)[13] = tswapl(env->regs[13]);
  267. (*regs)[14] = tswapl(env->regs[14]);
  268. (*regs)[15] = tswapl(env->regs[15]);
  269. (*regs)[16] = tswapl(cpsr_read((CPUARMState *)env));
  270. (*regs)[17] = tswapl(env->regs[0]); /* XXX */
  271. }
  272. #define USE_ELF_CORE_DUMP
  273. #define ELF_EXEC_PAGESIZE 4096
  274. enum
  275. {
  276. ARM_HWCAP_ARM_SWP = 1 << 0,
  277. ARM_HWCAP_ARM_HALF = 1 << 1,
  278. ARM_HWCAP_ARM_THUMB = 1 << 2,
  279. ARM_HWCAP_ARM_26BIT = 1 << 3,
  280. ARM_HWCAP_ARM_FAST_MULT = 1 << 4,
  281. ARM_HWCAP_ARM_FPA = 1 << 5,
  282. ARM_HWCAP_ARM_VFP = 1 << 6,
  283. ARM_HWCAP_ARM_EDSP = 1 << 7,
  284. ARM_HWCAP_ARM_JAVA = 1 << 8,
  285. ARM_HWCAP_ARM_IWMMXT = 1 << 9,
  286. ARM_HWCAP_ARM_THUMBEE = 1 << 10,
  287. ARM_HWCAP_ARM_NEON = 1 << 11,
  288. ARM_HWCAP_ARM_VFPv3 = 1 << 12,
  289. ARM_HWCAP_ARM_VFPv3D16 = 1 << 13,
  290. };
  291. #define TARGET_HAS_VALIDATE_GUEST_SPACE
  292. /* Return 1 if the proposed guest space is suitable for the guest.
  293. * Return 0 if the proposed guest space isn't suitable, but another
  294. * address space should be tried.
  295. * Return -1 if there is no way the proposed guest space can be
  296. * valid regardless of the base.
  297. * The guest code may leave a page mapped and populate it if the
  298. * address is suitable.
  299. */
  300. static int validate_guest_space(unsigned long guest_base,
  301. unsigned long guest_size)
  302. {
  303. unsigned long real_start, test_page_addr;
  304. /* We need to check that we can force a fault on access to the
  305. * commpage at 0xffff0fxx
  306. */
  307. test_page_addr = guest_base + (0xffff0f00 & qemu_host_page_mask);
  308. /* If the commpage lies within the already allocated guest space,
  309. * then there is no way we can allocate it.
  310. */
  311. if (test_page_addr >= guest_base
  312. && test_page_addr <= (guest_base + guest_size)) {
  313. return -1;
  314. }
  315. /* Note it needs to be writeable to let us initialise it */
  316. real_start = (unsigned long)
  317. mmap((void *)test_page_addr, qemu_host_page_size,
  318. PROT_READ | PROT_WRITE,
  319. MAP_ANONYMOUS | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  320. /* If we can't map it then try another address */
  321. if (real_start == -1ul) {
  322. return 0;
  323. }
  324. if (real_start != test_page_addr) {
  325. /* OS didn't put the page where we asked - unmap and reject */
  326. munmap((void *)real_start, qemu_host_page_size);
  327. return 0;
  328. }
  329. /* Leave the page mapped
  330. * Populate it (mmap should have left it all 0'd)
  331. */
  332. /* Kernel helper versions */
  333. __put_user(5, (uint32_t *)g2h(0xffff0ffcul));
  334. /* Now it's populated make it RO */
  335. if (mprotect((void *)test_page_addr, qemu_host_page_size, PROT_READ)) {
  336. perror("Protecting guest commpage");
  337. exit(-1);
  338. }
  339. return 1; /* All good */
  340. }
  341. #define ELF_HWCAP get_elf_hwcap()
  342. static uint32_t get_elf_hwcap(void)
  343. {
  344. CPUARMState *e = thread_env;
  345. uint32_t hwcaps = 0;
  346. hwcaps |= ARM_HWCAP_ARM_SWP;
  347. hwcaps |= ARM_HWCAP_ARM_HALF;
  348. hwcaps |= ARM_HWCAP_ARM_THUMB;
  349. hwcaps |= ARM_HWCAP_ARM_FAST_MULT;
  350. hwcaps |= ARM_HWCAP_ARM_FPA;
  351. /* probe for the extra features */
  352. #define GET_FEATURE(feat, hwcap) \
  353. do {if (arm_feature(e, feat)) { hwcaps |= hwcap; } } while (0)
  354. GET_FEATURE(ARM_FEATURE_VFP, ARM_HWCAP_ARM_VFP);
  355. GET_FEATURE(ARM_FEATURE_IWMMXT, ARM_HWCAP_ARM_IWMMXT);
  356. GET_FEATURE(ARM_FEATURE_THUMB2EE, ARM_HWCAP_ARM_THUMBEE);
  357. GET_FEATURE(ARM_FEATURE_NEON, ARM_HWCAP_ARM_NEON);
  358. GET_FEATURE(ARM_FEATURE_VFP3, ARM_HWCAP_ARM_VFPv3);
  359. GET_FEATURE(ARM_FEATURE_VFP_FP16, ARM_HWCAP_ARM_VFPv3D16);
  360. #undef GET_FEATURE
  361. return hwcaps;
  362. }
  363. #endif
  364. #ifdef TARGET_UNICORE32
  365. #define ELF_START_MMAP 0x80000000
  366. #define elf_check_arch(x) ((x) == EM_UNICORE32)
  367. #define ELF_CLASS ELFCLASS32
  368. #define ELF_DATA ELFDATA2LSB
  369. #define ELF_ARCH EM_UNICORE32
  370. static inline void init_thread(struct target_pt_regs *regs,
  371. struct image_info *infop)
  372. {
  373. abi_long stack = infop->start_stack;
  374. memset(regs, 0, sizeof(*regs));
  375. regs->UC32_REG_asr = 0x10;
  376. regs->UC32_REG_pc = infop->entry & 0xfffffffe;
  377. regs->UC32_REG_sp = infop->start_stack;
  378. /* FIXME - what to for failure of get_user()? */
  379. get_user_ual(regs->UC32_REG_02, stack + 8); /* envp */
  380. get_user_ual(regs->UC32_REG_01, stack + 4); /* envp */
  381. /* XXX: it seems that r0 is zeroed after ! */
  382. regs->UC32_REG_00 = 0;
  383. }
  384. #define ELF_NREG 34
  385. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  386. static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUUniCore32State *env)
  387. {
  388. (*regs)[0] = env->regs[0];
  389. (*regs)[1] = env->regs[1];
  390. (*regs)[2] = env->regs[2];
  391. (*regs)[3] = env->regs[3];
  392. (*regs)[4] = env->regs[4];
  393. (*regs)[5] = env->regs[5];
  394. (*regs)[6] = env->regs[6];
  395. (*regs)[7] = env->regs[7];
  396. (*regs)[8] = env->regs[8];
  397. (*regs)[9] = env->regs[9];
  398. (*regs)[10] = env->regs[10];
  399. (*regs)[11] = env->regs[11];
  400. (*regs)[12] = env->regs[12];
  401. (*regs)[13] = env->regs[13];
  402. (*regs)[14] = env->regs[14];
  403. (*regs)[15] = env->regs[15];
  404. (*regs)[16] = env->regs[16];
  405. (*regs)[17] = env->regs[17];
  406. (*regs)[18] = env->regs[18];
  407. (*regs)[19] = env->regs[19];
  408. (*regs)[20] = env->regs[20];
  409. (*regs)[21] = env->regs[21];
  410. (*regs)[22] = env->regs[22];
  411. (*regs)[23] = env->regs[23];
  412. (*regs)[24] = env->regs[24];
  413. (*regs)[25] = env->regs[25];
  414. (*regs)[26] = env->regs[26];
  415. (*regs)[27] = env->regs[27];
  416. (*regs)[28] = env->regs[28];
  417. (*regs)[29] = env->regs[29];
  418. (*regs)[30] = env->regs[30];
  419. (*regs)[31] = env->regs[31];
  420. (*regs)[32] = cpu_asr_read((CPUUniCore32State *)env);
  421. (*regs)[33] = env->regs[0]; /* XXX */
  422. }
  423. #define USE_ELF_CORE_DUMP
  424. #define ELF_EXEC_PAGESIZE 4096
  425. #define ELF_HWCAP (UC32_HWCAP_CMOV | UC32_HWCAP_UCF64)
  426. #endif
  427. #ifdef TARGET_SPARC
  428. #ifdef TARGET_SPARC64
  429. #define ELF_START_MMAP 0x80000000
  430. #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
  431. | HWCAP_SPARC_MULDIV | HWCAP_SPARC_V9)
  432. #ifndef TARGET_ABI32
  433. #define elf_check_arch(x) ( (x) == EM_SPARCV9 || (x) == EM_SPARC32PLUS )
  434. #else
  435. #define elf_check_arch(x) ( (x) == EM_SPARC32PLUS || (x) == EM_SPARC )
  436. #endif
  437. #define ELF_CLASS ELFCLASS64
  438. #define ELF_ARCH EM_SPARCV9
  439. #define STACK_BIAS 2047
  440. static inline void init_thread(struct target_pt_regs *regs,
  441. struct image_info *infop)
  442. {
  443. #ifndef TARGET_ABI32
  444. regs->tstate = 0;
  445. #endif
  446. regs->pc = infop->entry;
  447. regs->npc = regs->pc + 4;
  448. regs->y = 0;
  449. #ifdef TARGET_ABI32
  450. regs->u_regs[14] = infop->start_stack - 16 * 4;
  451. #else
  452. if (personality(infop->personality) == PER_LINUX32)
  453. regs->u_regs[14] = infop->start_stack - 16 * 4;
  454. else
  455. regs->u_regs[14] = infop->start_stack - 16 * 8 - STACK_BIAS;
  456. #endif
  457. }
  458. #else
  459. #define ELF_START_MMAP 0x80000000
  460. #define ELF_HWCAP (HWCAP_SPARC_FLUSH | HWCAP_SPARC_STBAR | HWCAP_SPARC_SWAP \
  461. | HWCAP_SPARC_MULDIV)
  462. #define elf_check_arch(x) ( (x) == EM_SPARC )
  463. #define ELF_CLASS ELFCLASS32
  464. #define ELF_ARCH EM_SPARC
  465. static inline void init_thread(struct target_pt_regs *regs,
  466. struct image_info *infop)
  467. {
  468. regs->psr = 0;
  469. regs->pc = infop->entry;
  470. regs->npc = regs->pc + 4;
  471. regs->y = 0;
  472. regs->u_regs[14] = infop->start_stack - 16 * 4;
  473. }
  474. #endif
  475. #endif
  476. #ifdef TARGET_PPC
  477. #define ELF_START_MMAP 0x80000000
  478. #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
  479. #define elf_check_arch(x) ( (x) == EM_PPC64 )
  480. #define ELF_CLASS ELFCLASS64
  481. #else
  482. #define elf_check_arch(x) ( (x) == EM_PPC )
  483. #define ELF_CLASS ELFCLASS32
  484. #endif
  485. #define ELF_ARCH EM_PPC
  486. /* Feature masks for the Aux Vector Hardware Capabilities (AT_HWCAP).
  487. See arch/powerpc/include/asm/cputable.h. */
  488. enum {
  489. QEMU_PPC_FEATURE_32 = 0x80000000,
  490. QEMU_PPC_FEATURE_64 = 0x40000000,
  491. QEMU_PPC_FEATURE_601_INSTR = 0x20000000,
  492. QEMU_PPC_FEATURE_HAS_ALTIVEC = 0x10000000,
  493. QEMU_PPC_FEATURE_HAS_FPU = 0x08000000,
  494. QEMU_PPC_FEATURE_HAS_MMU = 0x04000000,
  495. QEMU_PPC_FEATURE_HAS_4xxMAC = 0x02000000,
  496. QEMU_PPC_FEATURE_UNIFIED_CACHE = 0x01000000,
  497. QEMU_PPC_FEATURE_HAS_SPE = 0x00800000,
  498. QEMU_PPC_FEATURE_HAS_EFP_SINGLE = 0x00400000,
  499. QEMU_PPC_FEATURE_HAS_EFP_DOUBLE = 0x00200000,
  500. QEMU_PPC_FEATURE_NO_TB = 0x00100000,
  501. QEMU_PPC_FEATURE_POWER4 = 0x00080000,
  502. QEMU_PPC_FEATURE_POWER5 = 0x00040000,
  503. QEMU_PPC_FEATURE_POWER5_PLUS = 0x00020000,
  504. QEMU_PPC_FEATURE_CELL = 0x00010000,
  505. QEMU_PPC_FEATURE_BOOKE = 0x00008000,
  506. QEMU_PPC_FEATURE_SMT = 0x00004000,
  507. QEMU_PPC_FEATURE_ICACHE_SNOOP = 0x00002000,
  508. QEMU_PPC_FEATURE_ARCH_2_05 = 0x00001000,
  509. QEMU_PPC_FEATURE_PA6T = 0x00000800,
  510. QEMU_PPC_FEATURE_HAS_DFP = 0x00000400,
  511. QEMU_PPC_FEATURE_POWER6_EXT = 0x00000200,
  512. QEMU_PPC_FEATURE_ARCH_2_06 = 0x00000100,
  513. QEMU_PPC_FEATURE_HAS_VSX = 0x00000080,
  514. QEMU_PPC_FEATURE_PSERIES_PERFMON_COMPAT = 0x00000040,
  515. QEMU_PPC_FEATURE_TRUE_LE = 0x00000002,
  516. QEMU_PPC_FEATURE_PPC_LE = 0x00000001,
  517. };
  518. #define ELF_HWCAP get_elf_hwcap()
  519. static uint32_t get_elf_hwcap(void)
  520. {
  521. CPUPPCState *e = thread_env;
  522. uint32_t features = 0;
  523. /* We don't have to be terribly complete here; the high points are
  524. Altivec/FP/SPE support. Anything else is just a bonus. */
  525. #define GET_FEATURE(flag, feature) \
  526. do {if (e->insns_flags & flag) features |= feature; } while(0)
  527. GET_FEATURE(PPC_64B, QEMU_PPC_FEATURE_64);
  528. GET_FEATURE(PPC_FLOAT, QEMU_PPC_FEATURE_HAS_FPU);
  529. GET_FEATURE(PPC_ALTIVEC, QEMU_PPC_FEATURE_HAS_ALTIVEC);
  530. GET_FEATURE(PPC_SPE, QEMU_PPC_FEATURE_HAS_SPE);
  531. GET_FEATURE(PPC_SPE_SINGLE, QEMU_PPC_FEATURE_HAS_EFP_SINGLE);
  532. GET_FEATURE(PPC_SPE_DOUBLE, QEMU_PPC_FEATURE_HAS_EFP_DOUBLE);
  533. GET_FEATURE(PPC_BOOKE, QEMU_PPC_FEATURE_BOOKE);
  534. GET_FEATURE(PPC_405_MAC, QEMU_PPC_FEATURE_HAS_4xxMAC);
  535. #undef GET_FEATURE
  536. return features;
  537. }
  538. /*
  539. * The requirements here are:
  540. * - keep the final alignment of sp (sp & 0xf)
  541. * - make sure the 32-bit value at the first 16 byte aligned position of
  542. * AUXV is greater than 16 for glibc compatibility.
  543. * AT_IGNOREPPC is used for that.
  544. * - for compatibility with glibc ARCH_DLINFO must always be defined on PPC,
  545. * even if DLINFO_ARCH_ITEMS goes to zero or is undefined.
  546. */
  547. #define DLINFO_ARCH_ITEMS 5
  548. #define ARCH_DLINFO \
  549. do { \
  550. NEW_AUX_ENT(AT_DCACHEBSIZE, 0x20); \
  551. NEW_AUX_ENT(AT_ICACHEBSIZE, 0x20); \
  552. NEW_AUX_ENT(AT_UCACHEBSIZE, 0); \
  553. /* \
  554. * Now handle glibc compatibility. \
  555. */ \
  556. NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
  557. NEW_AUX_ENT(AT_IGNOREPPC, AT_IGNOREPPC); \
  558. } while (0)
  559. static inline void init_thread(struct target_pt_regs *_regs, struct image_info *infop)
  560. {
  561. _regs->gpr[1] = infop->start_stack;
  562. #if defined(TARGET_PPC64) && !defined(TARGET_ABI32)
  563. _regs->gpr[2] = ldq_raw(infop->entry + 8) + infop->load_bias;
  564. infop->entry = ldq_raw(infop->entry) + infop->load_bias;
  565. #endif
  566. _regs->nip = infop->entry;
  567. }
  568. /* See linux kernel: arch/powerpc/include/asm/elf.h. */
  569. #define ELF_NREG 48
  570. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  571. static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUPPCState *env)
  572. {
  573. int i;
  574. target_ulong ccr = 0;
  575. for (i = 0; i < ARRAY_SIZE(env->gpr); i++) {
  576. (*regs)[i] = tswapl(env->gpr[i]);
  577. }
  578. (*regs)[32] = tswapl(env->nip);
  579. (*regs)[33] = tswapl(env->msr);
  580. (*regs)[35] = tswapl(env->ctr);
  581. (*regs)[36] = tswapl(env->lr);
  582. (*regs)[37] = tswapl(env->xer);
  583. for (i = 0; i < ARRAY_SIZE(env->crf); i++) {
  584. ccr |= env->crf[i] << (32 - ((i + 1) * 4));
  585. }
  586. (*regs)[38] = tswapl(ccr);
  587. }
  588. #define USE_ELF_CORE_DUMP
  589. #define ELF_EXEC_PAGESIZE 4096
  590. #endif
  591. #ifdef TARGET_MIPS
  592. #define ELF_START_MMAP 0x80000000
  593. #define elf_check_arch(x) ( (x) == EM_MIPS )
  594. #ifdef TARGET_MIPS64
  595. #define ELF_CLASS ELFCLASS64
  596. #else
  597. #define ELF_CLASS ELFCLASS32
  598. #endif
  599. #define ELF_ARCH EM_MIPS
  600. static inline void init_thread(struct target_pt_regs *regs,
  601. struct image_info *infop)
  602. {
  603. regs->cp0_status = 2 << CP0St_KSU;
  604. regs->cp0_epc = infop->entry;
  605. regs->regs[29] = infop->start_stack;
  606. }
  607. /* See linux kernel: arch/mips/include/asm/elf.h. */
  608. #define ELF_NREG 45
  609. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  610. /* See linux kernel: arch/mips/include/asm/reg.h. */
  611. enum {
  612. #ifdef TARGET_MIPS64
  613. TARGET_EF_R0 = 0,
  614. #else
  615. TARGET_EF_R0 = 6,
  616. #endif
  617. TARGET_EF_R26 = TARGET_EF_R0 + 26,
  618. TARGET_EF_R27 = TARGET_EF_R0 + 27,
  619. TARGET_EF_LO = TARGET_EF_R0 + 32,
  620. TARGET_EF_HI = TARGET_EF_R0 + 33,
  621. TARGET_EF_CP0_EPC = TARGET_EF_R0 + 34,
  622. TARGET_EF_CP0_BADVADDR = TARGET_EF_R0 + 35,
  623. TARGET_EF_CP0_STATUS = TARGET_EF_R0 + 36,
  624. TARGET_EF_CP0_CAUSE = TARGET_EF_R0 + 37
  625. };
  626. /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
  627. static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMIPSState *env)
  628. {
  629. int i;
  630. for (i = 0; i < TARGET_EF_R0; i++) {
  631. (*regs)[i] = 0;
  632. }
  633. (*regs)[TARGET_EF_R0] = 0;
  634. for (i = 1; i < ARRAY_SIZE(env->active_tc.gpr); i++) {
  635. (*regs)[TARGET_EF_R0 + i] = tswapl(env->active_tc.gpr[i]);
  636. }
  637. (*regs)[TARGET_EF_R26] = 0;
  638. (*regs)[TARGET_EF_R27] = 0;
  639. (*regs)[TARGET_EF_LO] = tswapl(env->active_tc.LO[0]);
  640. (*regs)[TARGET_EF_HI] = tswapl(env->active_tc.HI[0]);
  641. (*regs)[TARGET_EF_CP0_EPC] = tswapl(env->active_tc.PC);
  642. (*regs)[TARGET_EF_CP0_BADVADDR] = tswapl(env->CP0_BadVAddr);
  643. (*regs)[TARGET_EF_CP0_STATUS] = tswapl(env->CP0_Status);
  644. (*regs)[TARGET_EF_CP0_CAUSE] = tswapl(env->CP0_Cause);
  645. }
  646. #define USE_ELF_CORE_DUMP
  647. #define ELF_EXEC_PAGESIZE 4096
  648. #endif /* TARGET_MIPS */
  649. #ifdef TARGET_MICROBLAZE
  650. #define ELF_START_MMAP 0x80000000
  651. #define elf_check_arch(x) ( (x) == EM_MICROBLAZE || (x) == EM_MICROBLAZE_OLD)
  652. #define ELF_CLASS ELFCLASS32
  653. #define ELF_ARCH EM_MICROBLAZE
  654. static inline void init_thread(struct target_pt_regs *regs,
  655. struct image_info *infop)
  656. {
  657. regs->pc = infop->entry;
  658. regs->r1 = infop->start_stack;
  659. }
  660. #define ELF_EXEC_PAGESIZE 4096
  661. #define USE_ELF_CORE_DUMP
  662. #define ELF_NREG 38
  663. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  664. /* See linux kernel: arch/mips/kernel/process.c:elf_dump_regs. */
  665. static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUMBState *env)
  666. {
  667. int i, pos = 0;
  668. for (i = 0; i < 32; i++) {
  669. (*regs)[pos++] = tswapl(env->regs[i]);
  670. }
  671. for (i = 0; i < 6; i++) {
  672. (*regs)[pos++] = tswapl(env->sregs[i]);
  673. }
  674. }
  675. #endif /* TARGET_MICROBLAZE */
  676. #ifdef TARGET_OPENRISC
  677. #define ELF_START_MMAP 0x08000000
  678. #define elf_check_arch(x) ((x) == EM_OPENRISC)
  679. #define ELF_ARCH EM_OPENRISC
  680. #define ELF_CLASS ELFCLASS32
  681. #define ELF_DATA ELFDATA2MSB
  682. static inline void init_thread(struct target_pt_regs *regs,
  683. struct image_info *infop)
  684. {
  685. regs->pc = infop->entry;
  686. regs->gpr[1] = infop->start_stack;
  687. }
  688. #define USE_ELF_CORE_DUMP
  689. #define ELF_EXEC_PAGESIZE 8192
  690. /* See linux kernel arch/openrisc/include/asm/elf.h. */
  691. #define ELF_NREG 34 /* gprs and pc, sr */
  692. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  693. static void elf_core_copy_regs(target_elf_gregset_t *regs,
  694. const CPUOpenRISCState *env)
  695. {
  696. int i;
  697. for (i = 0; i < 32; i++) {
  698. (*regs)[i] = tswapl(env->gpr[i]);
  699. }
  700. (*regs)[32] = tswapl(env->pc);
  701. (*regs)[33] = tswapl(env->sr);
  702. }
  703. #define ELF_HWCAP 0
  704. #define ELF_PLATFORM NULL
  705. #endif /* TARGET_OPENRISC */
  706. #ifdef TARGET_SH4
  707. #define ELF_START_MMAP 0x80000000
  708. #define elf_check_arch(x) ( (x) == EM_SH )
  709. #define ELF_CLASS ELFCLASS32
  710. #define ELF_ARCH EM_SH
  711. static inline void init_thread(struct target_pt_regs *regs,
  712. struct image_info *infop)
  713. {
  714. /* Check other registers XXXXX */
  715. regs->pc = infop->entry;
  716. regs->regs[15] = infop->start_stack;
  717. }
  718. /* See linux kernel: arch/sh/include/asm/elf.h. */
  719. #define ELF_NREG 23
  720. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  721. /* See linux kernel: arch/sh/include/asm/ptrace.h. */
  722. enum {
  723. TARGET_REG_PC = 16,
  724. TARGET_REG_PR = 17,
  725. TARGET_REG_SR = 18,
  726. TARGET_REG_GBR = 19,
  727. TARGET_REG_MACH = 20,
  728. TARGET_REG_MACL = 21,
  729. TARGET_REG_SYSCALL = 22
  730. };
  731. static inline void elf_core_copy_regs(target_elf_gregset_t *regs,
  732. const CPUSH4State *env)
  733. {
  734. int i;
  735. for (i = 0; i < 16; i++) {
  736. (*regs[i]) = tswapl(env->gregs[i]);
  737. }
  738. (*regs)[TARGET_REG_PC] = tswapl(env->pc);
  739. (*regs)[TARGET_REG_PR] = tswapl(env->pr);
  740. (*regs)[TARGET_REG_SR] = tswapl(env->sr);
  741. (*regs)[TARGET_REG_GBR] = tswapl(env->gbr);
  742. (*regs)[TARGET_REG_MACH] = tswapl(env->mach);
  743. (*regs)[TARGET_REG_MACL] = tswapl(env->macl);
  744. (*regs)[TARGET_REG_SYSCALL] = 0; /* FIXME */
  745. }
  746. #define USE_ELF_CORE_DUMP
  747. #define ELF_EXEC_PAGESIZE 4096
  748. #endif
  749. #ifdef TARGET_CRIS
  750. #define ELF_START_MMAP 0x80000000
  751. #define elf_check_arch(x) ( (x) == EM_CRIS )
  752. #define ELF_CLASS ELFCLASS32
  753. #define ELF_ARCH EM_CRIS
  754. static inline void init_thread(struct target_pt_regs *regs,
  755. struct image_info *infop)
  756. {
  757. regs->erp = infop->entry;
  758. }
  759. #define ELF_EXEC_PAGESIZE 8192
  760. #endif
  761. #ifdef TARGET_M68K
  762. #define ELF_START_MMAP 0x80000000
  763. #define elf_check_arch(x) ( (x) == EM_68K )
  764. #define ELF_CLASS ELFCLASS32
  765. #define ELF_ARCH EM_68K
  766. /* ??? Does this need to do anything?
  767. #define ELF_PLAT_INIT(_r) */
  768. static inline void init_thread(struct target_pt_regs *regs,
  769. struct image_info *infop)
  770. {
  771. regs->usp = infop->start_stack;
  772. regs->sr = 0;
  773. regs->pc = infop->entry;
  774. }
  775. /* See linux kernel: arch/m68k/include/asm/elf.h. */
  776. #define ELF_NREG 20
  777. typedef target_elf_greg_t target_elf_gregset_t[ELF_NREG];
  778. static void elf_core_copy_regs(target_elf_gregset_t *regs, const CPUM68KState *env)
  779. {
  780. (*regs)[0] = tswapl(env->dregs[1]);
  781. (*regs)[1] = tswapl(env->dregs[2]);
  782. (*regs)[2] = tswapl(env->dregs[3]);
  783. (*regs)[3] = tswapl(env->dregs[4]);
  784. (*regs)[4] = tswapl(env->dregs[5]);
  785. (*regs)[5] = tswapl(env->dregs[6]);
  786. (*regs)[6] = tswapl(env->dregs[7]);
  787. (*regs)[7] = tswapl(env->aregs[0]);
  788. (*regs)[8] = tswapl(env->aregs[1]);
  789. (*regs)[9] = tswapl(env->aregs[2]);
  790. (*regs)[10] = tswapl(env->aregs[3]);
  791. (*regs)[11] = tswapl(env->aregs[4]);
  792. (*regs)[12] = tswapl(env->aregs[5]);
  793. (*regs)[13] = tswapl(env->aregs[6]);
  794. (*regs)[14] = tswapl(env->dregs[0]);
  795. (*regs)[15] = tswapl(env->aregs[7]);
  796. (*regs)[16] = tswapl(env->dregs[0]); /* FIXME: orig_d0 */
  797. (*regs)[17] = tswapl(env->sr);
  798. (*regs)[18] = tswapl(env->pc);
  799. (*regs)[19] = 0; /* FIXME: regs->format | regs->vector */
  800. }
  801. #define USE_ELF_CORE_DUMP
  802. #define ELF_EXEC_PAGESIZE 8192
  803. #endif
  804. #ifdef TARGET_ALPHA
  805. #define ELF_START_MMAP (0x30000000000ULL)
  806. #define elf_check_arch(x) ( (x) == ELF_ARCH )
  807. #define ELF_CLASS ELFCLASS64
  808. #define ELF_ARCH EM_ALPHA
  809. static inline void init_thread(struct target_pt_regs *regs,
  810. struct image_info *infop)
  811. {
  812. regs->pc = infop->entry;
  813. regs->ps = 8;
  814. regs->usp = infop->start_stack;
  815. }
  816. #define ELF_EXEC_PAGESIZE 8192
  817. #endif /* TARGET_ALPHA */
  818. #ifdef TARGET_S390X
  819. #define ELF_START_MMAP (0x20000000000ULL)
  820. #define elf_check_arch(x) ( (x) == ELF_ARCH )
  821. #define ELF_CLASS ELFCLASS64
  822. #define ELF_DATA ELFDATA2MSB
  823. #define ELF_ARCH EM_S390
  824. static inline void init_thread(struct target_pt_regs *regs, struct image_info *infop)
  825. {
  826. regs->psw.addr = infop->entry;
  827. regs->psw.mask = PSW_MASK_64 | PSW_MASK_32;
  828. regs->gprs[15] = infop->start_stack;
  829. }
  830. #endif /* TARGET_S390X */
  831. #ifndef ELF_PLATFORM
  832. #define ELF_PLATFORM (NULL)
  833. #endif
  834. #ifndef ELF_HWCAP
  835. #define ELF_HWCAP 0
  836. #endif
  837. #ifdef TARGET_ABI32
  838. #undef ELF_CLASS
  839. #define ELF_CLASS ELFCLASS32
  840. #undef bswaptls
  841. #define bswaptls(ptr) bswap32s(ptr)
  842. #endif
  843. #include "elf.h"
  844. struct exec
  845. {
  846. unsigned int a_info; /* Use macros N_MAGIC, etc for access */
  847. unsigned int a_text; /* length of text, in bytes */
  848. unsigned int a_data; /* length of data, in bytes */
  849. unsigned int a_bss; /* length of uninitialized data area, in bytes */
  850. unsigned int a_syms; /* length of symbol table data in file, in bytes */
  851. unsigned int a_entry; /* start address */
  852. unsigned int a_trsize; /* length of relocation info for text, in bytes */
  853. unsigned int a_drsize; /* length of relocation info for data, in bytes */
  854. };
  855. #define N_MAGIC(exec) ((exec).a_info & 0xffff)
  856. #define OMAGIC 0407
  857. #define NMAGIC 0410
  858. #define ZMAGIC 0413
  859. #define QMAGIC 0314
  860. /* Necessary parameters */
  861. #define TARGET_ELF_EXEC_PAGESIZE TARGET_PAGE_SIZE
  862. #define TARGET_ELF_PAGESTART(_v) ((_v) & ~(unsigned long)(TARGET_ELF_EXEC_PAGESIZE-1))
  863. #define TARGET_ELF_PAGEOFFSET(_v) ((_v) & (TARGET_ELF_EXEC_PAGESIZE-1))
  864. #define DLINFO_ITEMS 13
  865. static inline void memcpy_fromfs(void * to, const void * from, unsigned long n)
  866. {
  867. memcpy(to, from, n);
  868. }
  869. #ifdef BSWAP_NEEDED
  870. static void bswap_ehdr(struct elfhdr *ehdr)
  871. {
  872. bswap16s(&ehdr->e_type); /* Object file type */
  873. bswap16s(&ehdr->e_machine); /* Architecture */
  874. bswap32s(&ehdr->e_version); /* Object file version */
  875. bswaptls(&ehdr->e_entry); /* Entry point virtual address */
  876. bswaptls(&ehdr->e_phoff); /* Program header table file offset */
  877. bswaptls(&ehdr->e_shoff); /* Section header table file offset */
  878. bswap32s(&ehdr->e_flags); /* Processor-specific flags */
  879. bswap16s(&ehdr->e_ehsize); /* ELF header size in bytes */
  880. bswap16s(&ehdr->e_phentsize); /* Program header table entry size */
  881. bswap16s(&ehdr->e_phnum); /* Program header table entry count */
  882. bswap16s(&ehdr->e_shentsize); /* Section header table entry size */
  883. bswap16s(&ehdr->e_shnum); /* Section header table entry count */
  884. bswap16s(&ehdr->e_shstrndx); /* Section header string table index */
  885. }
  886. static void bswap_phdr(struct elf_phdr *phdr, int phnum)
  887. {
  888. int i;
  889. for (i = 0; i < phnum; ++i, ++phdr) {
  890. bswap32s(&phdr->p_type); /* Segment type */
  891. bswap32s(&phdr->p_flags); /* Segment flags */
  892. bswaptls(&phdr->p_offset); /* Segment file offset */
  893. bswaptls(&phdr->p_vaddr); /* Segment virtual address */
  894. bswaptls(&phdr->p_paddr); /* Segment physical address */
  895. bswaptls(&phdr->p_filesz); /* Segment size in file */
  896. bswaptls(&phdr->p_memsz); /* Segment size in memory */
  897. bswaptls(&phdr->p_align); /* Segment alignment */
  898. }
  899. }
  900. static void bswap_shdr(struct elf_shdr *shdr, int shnum)
  901. {
  902. int i;
  903. for (i = 0; i < shnum; ++i, ++shdr) {
  904. bswap32s(&shdr->sh_name);
  905. bswap32s(&shdr->sh_type);
  906. bswaptls(&shdr->sh_flags);
  907. bswaptls(&shdr->sh_addr);
  908. bswaptls(&shdr->sh_offset);
  909. bswaptls(&shdr->sh_size);
  910. bswap32s(&shdr->sh_link);
  911. bswap32s(&shdr->sh_info);
  912. bswaptls(&shdr->sh_addralign);
  913. bswaptls(&shdr->sh_entsize);
  914. }
  915. }
  916. static void bswap_sym(struct elf_sym *sym)
  917. {
  918. bswap32s(&sym->st_name);
  919. bswaptls(&sym->st_value);
  920. bswaptls(&sym->st_size);
  921. bswap16s(&sym->st_shndx);
  922. }
  923. #else
  924. static inline void bswap_ehdr(struct elfhdr *ehdr) { }
  925. static inline void bswap_phdr(struct elf_phdr *phdr, int phnum) { }
  926. static inline void bswap_shdr(struct elf_shdr *shdr, int shnum) { }
  927. static inline void bswap_sym(struct elf_sym *sym) { }
  928. #endif
  929. #ifdef USE_ELF_CORE_DUMP
  930. static int elf_core_dump(int, const CPUArchState *);
  931. #endif /* USE_ELF_CORE_DUMP */
  932. static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias);
  933. /* Verify the portions of EHDR within E_IDENT for the target.
  934. This can be performed before bswapping the entire header. */
  935. static bool elf_check_ident(struct elfhdr *ehdr)
  936. {
  937. return (ehdr->e_ident[EI_MAG0] == ELFMAG0
  938. && ehdr->e_ident[EI_MAG1] == ELFMAG1
  939. && ehdr->e_ident[EI_MAG2] == ELFMAG2
  940. && ehdr->e_ident[EI_MAG3] == ELFMAG3
  941. && ehdr->e_ident[EI_CLASS] == ELF_CLASS
  942. && ehdr->e_ident[EI_DATA] == ELF_DATA
  943. && ehdr->e_ident[EI_VERSION] == EV_CURRENT);
  944. }
  945. /* Verify the portions of EHDR outside of E_IDENT for the target.
  946. This has to wait until after bswapping the header. */
  947. static bool elf_check_ehdr(struct elfhdr *ehdr)
  948. {
  949. return (elf_check_arch(ehdr->e_machine)
  950. && ehdr->e_ehsize == sizeof(struct elfhdr)
  951. && ehdr->e_phentsize == sizeof(struct elf_phdr)
  952. && ehdr->e_shentsize == sizeof(struct elf_shdr)
  953. && (ehdr->e_type == ET_EXEC || ehdr->e_type == ET_DYN));
  954. }
  955. /*
  956. * 'copy_elf_strings()' copies argument/envelope strings from user
  957. * memory to free pages in kernel mem. These are in a format ready
  958. * to be put directly into the top of new user memory.
  959. *
  960. */
  961. static abi_ulong copy_elf_strings(int argc,char ** argv, void **page,
  962. abi_ulong p)
  963. {
  964. char *tmp, *tmp1, *pag = NULL;
  965. int len, offset = 0;
  966. if (!p) {
  967. return 0; /* bullet-proofing */
  968. }
  969. while (argc-- > 0) {
  970. tmp = argv[argc];
  971. if (!tmp) {
  972. fprintf(stderr, "VFS: argc is wrong");
  973. exit(-1);
  974. }
  975. tmp1 = tmp;
  976. while (*tmp++);
  977. len = tmp - tmp1;
  978. if (p < len) { /* this shouldn't happen - 128kB */
  979. return 0;
  980. }
  981. while (len) {
  982. --p; --tmp; --len;
  983. if (--offset < 0) {
  984. offset = p % TARGET_PAGE_SIZE;
  985. pag = (char *)page[p/TARGET_PAGE_SIZE];
  986. if (!pag) {
  987. pag = g_try_malloc0(TARGET_PAGE_SIZE);
  988. page[p/TARGET_PAGE_SIZE] = pag;
  989. if (!pag)
  990. return 0;
  991. }
  992. }
  993. if (len == 0 || offset == 0) {
  994. *(pag + offset) = *tmp;
  995. }
  996. else {
  997. int bytes_to_copy = (len > offset) ? offset : len;
  998. tmp -= bytes_to_copy;
  999. p -= bytes_to_copy;
  1000. offset -= bytes_to_copy;
  1001. len -= bytes_to_copy;
  1002. memcpy_fromfs(pag + offset, tmp, bytes_to_copy + 1);
  1003. }
  1004. }
  1005. }
  1006. return p;
  1007. }
  1008. static abi_ulong setup_arg_pages(abi_ulong p, struct linux_binprm *bprm,
  1009. struct image_info *info)
  1010. {
  1011. abi_ulong stack_base, size, error, guard;
  1012. int i;
  1013. /* Create enough stack to hold everything. If we don't use
  1014. it for args, we'll use it for something else. */
  1015. size = guest_stack_size;
  1016. if (size < MAX_ARG_PAGES*TARGET_PAGE_SIZE) {
  1017. size = MAX_ARG_PAGES*TARGET_PAGE_SIZE;
  1018. }
  1019. guard = TARGET_PAGE_SIZE;
  1020. if (guard < qemu_real_host_page_size) {
  1021. guard = qemu_real_host_page_size;
  1022. }
  1023. error = target_mmap(0, size + guard, PROT_READ | PROT_WRITE,
  1024. MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  1025. if (error == -1) {
  1026. perror("mmap stack");
  1027. exit(-1);
  1028. }
  1029. /* We reserve one extra page at the top of the stack as guard. */
  1030. target_mprotect(error, guard, PROT_NONE);
  1031. info->stack_limit = error + guard;
  1032. stack_base = info->stack_limit + size - MAX_ARG_PAGES*TARGET_PAGE_SIZE;
  1033. p += stack_base;
  1034. for (i = 0 ; i < MAX_ARG_PAGES ; i++) {
  1035. if (bprm->page[i]) {
  1036. info->rss++;
  1037. /* FIXME - check return value of memcpy_to_target() for failure */
  1038. memcpy_to_target(stack_base, bprm->page[i], TARGET_PAGE_SIZE);
  1039. g_free(bprm->page[i]);
  1040. }
  1041. stack_base += TARGET_PAGE_SIZE;
  1042. }
  1043. return p;
  1044. }
  1045. /* Map and zero the bss. We need to explicitly zero any fractional pages
  1046. after the data section (i.e. bss). */
  1047. static void zero_bss(abi_ulong elf_bss, abi_ulong last_bss, int prot)
  1048. {
  1049. uintptr_t host_start, host_map_start, host_end;
  1050. last_bss = TARGET_PAGE_ALIGN(last_bss);
  1051. /* ??? There is confusion between qemu_real_host_page_size and
  1052. qemu_host_page_size here and elsewhere in target_mmap, which
  1053. may lead to the end of the data section mapping from the file
  1054. not being mapped. At least there was an explicit test and
  1055. comment for that here, suggesting that "the file size must
  1056. be known". The comment probably pre-dates the introduction
  1057. of the fstat system call in target_mmap which does in fact
  1058. find out the size. What isn't clear is if the workaround
  1059. here is still actually needed. For now, continue with it,
  1060. but merge it with the "normal" mmap that would allocate the bss. */
  1061. host_start = (uintptr_t) g2h(elf_bss);
  1062. host_end = (uintptr_t) g2h(last_bss);
  1063. host_map_start = (host_start + qemu_real_host_page_size - 1);
  1064. host_map_start &= -qemu_real_host_page_size;
  1065. if (host_map_start < host_end) {
  1066. void *p = mmap((void *)host_map_start, host_end - host_map_start,
  1067. prot, MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0);
  1068. if (p == MAP_FAILED) {
  1069. perror("cannot mmap brk");
  1070. exit(-1);
  1071. }
  1072. /* Since we didn't use target_mmap, make sure to record
  1073. the validity of the pages with qemu. */
  1074. page_set_flags(elf_bss & TARGET_PAGE_MASK, last_bss, prot|PAGE_VALID);
  1075. }
  1076. if (host_start < host_map_start) {
  1077. memset((void *)host_start, 0, host_map_start - host_start);
  1078. }
  1079. }
  1080. #ifdef CONFIG_USE_FDPIC
  1081. static abi_ulong loader_build_fdpic_loadmap(struct image_info *info, abi_ulong sp)
  1082. {
  1083. uint16_t n;
  1084. struct elf32_fdpic_loadseg *loadsegs = info->loadsegs;
  1085. /* elf32_fdpic_loadseg */
  1086. n = info->nsegs;
  1087. while (n--) {
  1088. sp -= 12;
  1089. put_user_u32(loadsegs[n].addr, sp+0);
  1090. put_user_u32(loadsegs[n].p_vaddr, sp+4);
  1091. put_user_u32(loadsegs[n].p_memsz, sp+8);
  1092. }
  1093. /* elf32_fdpic_loadmap */
  1094. sp -= 4;
  1095. put_user_u16(0, sp+0); /* version */
  1096. put_user_u16(info->nsegs, sp+2); /* nsegs */
  1097. info->personality = PER_LINUX_FDPIC;
  1098. info->loadmap_addr = sp;
  1099. return sp;
  1100. }
  1101. #endif
  1102. static abi_ulong create_elf_tables(abi_ulong p, int argc, int envc,
  1103. struct elfhdr *exec,
  1104. struct image_info *info,
  1105. struct image_info *interp_info)
  1106. {
  1107. abi_ulong sp;
  1108. abi_ulong sp_auxv;
  1109. int size;
  1110. int i;
  1111. abi_ulong u_rand_bytes;
  1112. uint8_t k_rand_bytes[16];
  1113. abi_ulong u_platform;
  1114. const char *k_platform;
  1115. const int n = sizeof(elf_addr_t);
  1116. sp = p;
  1117. #ifdef CONFIG_USE_FDPIC
  1118. /* Needs to be before we load the env/argc/... */
  1119. if (elf_is_fdpic(exec)) {
  1120. /* Need 4 byte alignment for these structs */
  1121. sp &= ~3;
  1122. sp = loader_build_fdpic_loadmap(info, sp);
  1123. info->other_info = interp_info;
  1124. if (interp_info) {
  1125. interp_info->other_info = info;
  1126. sp = loader_build_fdpic_loadmap(interp_info, sp);
  1127. }
  1128. }
  1129. #endif
  1130. u_platform = 0;
  1131. k_platform = ELF_PLATFORM;
  1132. if (k_platform) {
  1133. size_t len = strlen(k_platform) + 1;
  1134. sp -= (len + n - 1) & ~(n - 1);
  1135. u_platform = sp;
  1136. /* FIXME - check return value of memcpy_to_target() for failure */
  1137. memcpy_to_target(sp, k_platform, len);
  1138. }
  1139. /*
  1140. * Generate 16 random bytes for userspace PRNG seeding (not
  1141. * cryptically secure but it's not the aim of QEMU).
  1142. */
  1143. srand((unsigned int) time(NULL));
  1144. for (i = 0; i < 16; i++) {
  1145. k_rand_bytes[i] = rand();
  1146. }
  1147. sp -= 16;
  1148. u_rand_bytes = sp;
  1149. /* FIXME - check return value of memcpy_to_target() for failure */
  1150. memcpy_to_target(sp, k_rand_bytes, 16);
  1151. /*
  1152. * Force 16 byte _final_ alignment here for generality.
  1153. */
  1154. sp = sp &~ (abi_ulong)15;
  1155. size = (DLINFO_ITEMS + 1) * 2;
  1156. if (k_platform)
  1157. size += 2;
  1158. #ifdef DLINFO_ARCH_ITEMS
  1159. size += DLINFO_ARCH_ITEMS * 2;
  1160. #endif
  1161. size += envc + argc + 2;
  1162. size += 1; /* argc itself */
  1163. size *= n;
  1164. if (size & 15)
  1165. sp -= 16 - (size & 15);
  1166. /* This is correct because Linux defines
  1167. * elf_addr_t as Elf32_Off / Elf64_Off
  1168. */
  1169. #define NEW_AUX_ENT(id, val) do { \
  1170. sp -= n; put_user_ual(val, sp); \
  1171. sp -= n; put_user_ual(id, sp); \
  1172. } while(0)
  1173. sp_auxv = sp;
  1174. NEW_AUX_ENT (AT_NULL, 0);
  1175. /* There must be exactly DLINFO_ITEMS entries here. */
  1176. NEW_AUX_ENT(AT_PHDR, (abi_ulong)(info->load_addr + exec->e_phoff));
  1177. NEW_AUX_ENT(AT_PHENT, (abi_ulong)(sizeof (struct elf_phdr)));
  1178. NEW_AUX_ENT(AT_PHNUM, (abi_ulong)(exec->e_phnum));
  1179. NEW_AUX_ENT(AT_PAGESZ, (abi_ulong)(TARGET_PAGE_SIZE));
  1180. NEW_AUX_ENT(AT_BASE, (abi_ulong)(interp_info ? interp_info->load_addr : 0));
  1181. NEW_AUX_ENT(AT_FLAGS, (abi_ulong)0);
  1182. NEW_AUX_ENT(AT_ENTRY, info->entry);
  1183. NEW_AUX_ENT(AT_UID, (abi_ulong) getuid());
  1184. NEW_AUX_ENT(AT_EUID, (abi_ulong) geteuid());
  1185. NEW_AUX_ENT(AT_GID, (abi_ulong) getgid());
  1186. NEW_AUX_ENT(AT_EGID, (abi_ulong) getegid());
  1187. NEW_AUX_ENT(AT_HWCAP, (abi_ulong) ELF_HWCAP);
  1188. NEW_AUX_ENT(AT_CLKTCK, (abi_ulong) sysconf(_SC_CLK_TCK));
  1189. NEW_AUX_ENT(AT_RANDOM, (abi_ulong) u_rand_bytes);
  1190. if (k_platform)
  1191. NEW_AUX_ENT(AT_PLATFORM, u_platform);
  1192. #ifdef ARCH_DLINFO
  1193. /*
  1194. * ARCH_DLINFO must come last so platform specific code can enforce
  1195. * special alignment requirements on the AUXV if necessary (eg. PPC).
  1196. */
  1197. ARCH_DLINFO;
  1198. #endif
  1199. #undef NEW_AUX_ENT
  1200. info->saved_auxv = sp;
  1201. info->auxv_len = sp_auxv - sp;
  1202. sp = loader_build_argptr(envc, argc, sp, p, 0);
  1203. return sp;
  1204. }
  1205. #ifndef TARGET_HAS_VALIDATE_GUEST_SPACE
  1206. /* If the guest doesn't have a validation function just agree */
  1207. static int validate_guest_space(unsigned long guest_base,
  1208. unsigned long guest_size)
  1209. {
  1210. return 1;
  1211. }
  1212. #endif
  1213. unsigned long init_guest_space(unsigned long host_start,
  1214. unsigned long host_size,
  1215. unsigned long guest_start,
  1216. bool fixed)
  1217. {
  1218. unsigned long current_start, real_start;
  1219. int flags;
  1220. assert(host_start || host_size);
  1221. /* If just a starting address is given, then just verify that
  1222. * address. */
  1223. if (host_start && !host_size) {
  1224. if (validate_guest_space(host_start, host_size) == 1) {
  1225. return host_start;
  1226. } else {
  1227. return (unsigned long)-1;
  1228. }
  1229. }
  1230. /* Setup the initial flags and start address. */
  1231. current_start = host_start & qemu_host_page_mask;
  1232. flags = MAP_ANONYMOUS | MAP_PRIVATE | MAP_NORESERVE;
  1233. if (fixed) {
  1234. flags |= MAP_FIXED;
  1235. }
  1236. /* Otherwise, a non-zero size region of memory needs to be mapped
  1237. * and validated. */
  1238. while (1) {
  1239. unsigned long real_size = host_size;
  1240. /* Do not use mmap_find_vma here because that is limited to the
  1241. * guest address space. We are going to make the
  1242. * guest address space fit whatever we're given.
  1243. */
  1244. real_start = (unsigned long)
  1245. mmap((void *)current_start, host_size, PROT_NONE, flags, -1, 0);
  1246. if (real_start == (unsigned long)-1) {
  1247. return (unsigned long)-1;
  1248. }
  1249. /* Ensure the address is properly aligned. */
  1250. if (real_start & ~qemu_host_page_mask) {
  1251. munmap((void *)real_start, host_size);
  1252. real_size = host_size + qemu_host_page_size;
  1253. real_start = (unsigned long)
  1254. mmap((void *)real_start, real_size, PROT_NONE, flags, -1, 0);
  1255. if (real_start == (unsigned long)-1) {
  1256. return (unsigned long)-1;
  1257. }
  1258. real_start = HOST_PAGE_ALIGN(real_start);
  1259. }
  1260. /* Check to see if the address is valid. */
  1261. if (!host_start || real_start == current_start) {
  1262. int valid = validate_guest_space(real_start - guest_start,
  1263. real_size);
  1264. if (valid == 1) {
  1265. break;
  1266. } else if (valid == -1) {
  1267. return (unsigned long)-1;
  1268. }
  1269. /* valid == 0, so try again. */
  1270. }
  1271. /* That address didn't work. Unmap and try a different one.
  1272. * The address the host picked because is typically right at
  1273. * the top of the host address space and leaves the guest with
  1274. * no usable address space. Resort to a linear search. We
  1275. * already compensated for mmap_min_addr, so this should not
  1276. * happen often. Probably means we got unlucky and host
  1277. * address space randomization put a shared library somewhere
  1278. * inconvenient.
  1279. */
  1280. munmap((void *)real_start, host_size);
  1281. current_start += qemu_host_page_size;
  1282. if (host_start == current_start) {
  1283. /* Theoretically possible if host doesn't have any suitably
  1284. * aligned areas. Normally the first mmap will fail.
  1285. */
  1286. return (unsigned long)-1;
  1287. }
  1288. }
  1289. qemu_log("Reserved 0x%lx bytes of guest address space\n", host_size);
  1290. return real_start;
  1291. }
  1292. static void probe_guest_base(const char *image_name,
  1293. abi_ulong loaddr, abi_ulong hiaddr)
  1294. {
  1295. /* Probe for a suitable guest base address, if the user has not set
  1296. * it explicitly, and set guest_base appropriately.
  1297. * In case of error we will print a suitable message and exit.
  1298. */
  1299. #if defined(CONFIG_USE_GUEST_BASE)
  1300. const char *errmsg;
  1301. if (!have_guest_base && !reserved_va) {
  1302. unsigned long host_start, real_start, host_size;
  1303. /* Round addresses to page boundaries. */
  1304. loaddr &= qemu_host_page_mask;
  1305. hiaddr = HOST_PAGE_ALIGN(hiaddr);
  1306. if (loaddr < mmap_min_addr) {
  1307. host_start = HOST_PAGE_ALIGN(mmap_min_addr);
  1308. } else {
  1309. host_start = loaddr;
  1310. if (host_start != loaddr) {
  1311. errmsg = "Address overflow loading ELF binary";
  1312. goto exit_errmsg;
  1313. }
  1314. }
  1315. host_size = hiaddr - loaddr;
  1316. /* Setup the initial guest memory space with ranges gleaned from
  1317. * the ELF image that is being loaded.
  1318. */
  1319. real_start = init_guest_space(host_start, host_size, loaddr, false);
  1320. if (real_start == (unsigned long)-1) {
  1321. errmsg = "Unable to find space for application";
  1322. goto exit_errmsg;
  1323. }
  1324. guest_base = real_start - loaddr;
  1325. qemu_log("Relocating guest address space from 0x"
  1326. TARGET_ABI_FMT_lx " to 0x%lx\n",
  1327. loaddr, real_start);
  1328. }
  1329. return;
  1330. exit_errmsg:
  1331. fprintf(stderr, "%s: %s\n", image_name, errmsg);
  1332. exit(-1);
  1333. #endif
  1334. }
  1335. /* Load an ELF image into the address space.
  1336. IMAGE_NAME is the filename of the image, to use in error messages.
  1337. IMAGE_FD is the open file descriptor for the image.
  1338. BPRM_BUF is a copy of the beginning of the file; this of course
  1339. contains the elf file header at offset 0. It is assumed that this
  1340. buffer is sufficiently aligned to present no problems to the host
  1341. in accessing data at aligned offsets within the buffer.
  1342. On return: INFO values will be filled in, as necessary or available. */
  1343. static void load_elf_image(const char *image_name, int image_fd,
  1344. struct image_info *info, char **pinterp_name,
  1345. char bprm_buf[BPRM_BUF_SIZE])
  1346. {
  1347. struct elfhdr *ehdr = (struct elfhdr *)bprm_buf;
  1348. struct elf_phdr *phdr;
  1349. abi_ulong load_addr, load_bias, loaddr, hiaddr, error;
  1350. int i, retval;
  1351. const char *errmsg;
  1352. /* First of all, some simple consistency checks */
  1353. errmsg = "Invalid ELF image for this architecture";
  1354. if (!elf_check_ident(ehdr)) {
  1355. goto exit_errmsg;
  1356. }
  1357. bswap_ehdr(ehdr);
  1358. if (!elf_check_ehdr(ehdr)) {
  1359. goto exit_errmsg;
  1360. }
  1361. i = ehdr->e_phnum * sizeof(struct elf_phdr);
  1362. if (ehdr->e_phoff + i <= BPRM_BUF_SIZE) {
  1363. phdr = (struct elf_phdr *)(bprm_buf + ehdr->e_phoff);
  1364. } else {
  1365. phdr = (struct elf_phdr *) alloca(i);
  1366. retval = pread(image_fd, phdr, i, ehdr->e_phoff);
  1367. if (retval != i) {
  1368. goto exit_read;
  1369. }
  1370. }
  1371. bswap_phdr(phdr, ehdr->e_phnum);
  1372. #ifdef CONFIG_USE_FDPIC
  1373. info->nsegs = 0;
  1374. info->pt_dynamic_addr = 0;
  1375. #endif
  1376. /* Find the maximum size of the image and allocate an appropriate
  1377. amount of memory to handle that. */
  1378. loaddr = -1, hiaddr = 0;
  1379. for (i = 0; i < ehdr->e_phnum; ++i) {
  1380. if (phdr[i].p_type == PT_LOAD) {
  1381. abi_ulong a = phdr[i].p_vaddr;
  1382. if (a < loaddr) {
  1383. loaddr = a;
  1384. }
  1385. a += phdr[i].p_memsz;
  1386. if (a > hiaddr) {
  1387. hiaddr = a;
  1388. }
  1389. #ifdef CONFIG_USE_FDPIC
  1390. ++info->nsegs;
  1391. #endif
  1392. }
  1393. }
  1394. load_addr = loaddr;
  1395. if (ehdr->e_type == ET_DYN) {
  1396. /* The image indicates that it can be loaded anywhere. Find a
  1397. location that can hold the memory space required. If the
  1398. image is pre-linked, LOADDR will be non-zero. Since we do
  1399. not supply MAP_FIXED here we'll use that address if and
  1400. only if it remains available. */
  1401. load_addr = target_mmap(loaddr, hiaddr - loaddr, PROT_NONE,
  1402. MAP_PRIVATE | MAP_ANON | MAP_NORESERVE,
  1403. -1, 0);
  1404. if (load_addr == -1) {
  1405. goto exit_perror;
  1406. }
  1407. } else if (pinterp_name != NULL) {
  1408. /* This is the main executable. Make sure that the low
  1409. address does not conflict with MMAP_MIN_ADDR or the
  1410. QEMU application itself. */
  1411. probe_guest_base(image_name, loaddr, hiaddr);
  1412. }
  1413. load_bias = load_addr - loaddr;
  1414. #ifdef CONFIG_USE_FDPIC
  1415. {
  1416. struct elf32_fdpic_loadseg *loadsegs = info->loadsegs =
  1417. g_malloc(sizeof(*loadsegs) * info->nsegs);
  1418. for (i = 0; i < ehdr->e_phnum; ++i) {
  1419. switch (phdr[i].p_type) {
  1420. case PT_DYNAMIC:
  1421. info->pt_dynamic_addr = phdr[i].p_vaddr + load_bias;
  1422. break;
  1423. case PT_LOAD:
  1424. loadsegs->addr = phdr[i].p_vaddr + load_bias;
  1425. loadsegs->p_vaddr = phdr[i].p_vaddr;
  1426. loadsegs->p_memsz = phdr[i].p_memsz;
  1427. ++loadsegs;
  1428. break;
  1429. }
  1430. }
  1431. }
  1432. #endif
  1433. info->load_bias = load_bias;
  1434. info->load_addr = load_addr;
  1435. info->entry = ehdr->e_entry + load_bias;
  1436. info->start_code = -1;
  1437. info->end_code = 0;
  1438. info->start_data = -1;
  1439. info->end_data = 0;
  1440. info->brk = 0;
  1441. info->elf_flags = ehdr->e_flags;
  1442. for (i = 0; i < ehdr->e_phnum; i++) {
  1443. struct elf_phdr *eppnt = phdr + i;
  1444. if (eppnt->p_type == PT_LOAD) {
  1445. abi_ulong vaddr, vaddr_po, vaddr_ps, vaddr_ef, vaddr_em;
  1446. int elf_prot = 0;
  1447. if (eppnt->p_flags & PF_R) elf_prot = PROT_READ;
  1448. if (eppnt->p_flags & PF_W) elf_prot |= PROT_WRITE;
  1449. if (eppnt->p_flags & PF_X) elf_prot |= PROT_EXEC;
  1450. vaddr = load_bias + eppnt->p_vaddr;
  1451. vaddr_po = TARGET_ELF_PAGEOFFSET(vaddr);
  1452. vaddr_ps = TARGET_ELF_PAGESTART(vaddr);
  1453. error = target_mmap(vaddr_ps, eppnt->p_filesz + vaddr_po,
  1454. elf_prot, MAP_PRIVATE | MAP_FIXED,
  1455. image_fd, eppnt->p_offset - vaddr_po);
  1456. if (error == -1) {
  1457. goto exit_perror;
  1458. }
  1459. vaddr_ef = vaddr + eppnt->p_filesz;
  1460. vaddr_em = vaddr + eppnt->p_memsz;
  1461. /* If the load segment requests extra zeros (e.g. bss), map it. */
  1462. if (vaddr_ef < vaddr_em) {
  1463. zero_bss(vaddr_ef, vaddr_em, elf_prot);
  1464. }
  1465. /* Find the full program boundaries. */
  1466. if (elf_prot & PROT_EXEC) {
  1467. if (vaddr < info->start_code) {
  1468. info->start_code = vaddr;
  1469. }
  1470. if (vaddr_ef > info->end_code) {
  1471. info->end_code = vaddr_ef;
  1472. }
  1473. }
  1474. if (elf_prot & PROT_WRITE) {
  1475. if (vaddr < info->start_data) {
  1476. info->start_data = vaddr;
  1477. }
  1478. if (vaddr_ef > info->end_data) {
  1479. info->end_data = vaddr_ef;
  1480. }
  1481. if (vaddr_em > info->brk) {
  1482. info->brk = vaddr_em;
  1483. }
  1484. }
  1485. } else if (eppnt->p_type == PT_INTERP && pinterp_name) {
  1486. char *interp_name;
  1487. if (*pinterp_name) {
  1488. errmsg = "Multiple PT_INTERP entries";
  1489. goto exit_errmsg;
  1490. }
  1491. interp_name = malloc(eppnt->p_filesz);
  1492. if (!interp_name) {
  1493. goto exit_perror;
  1494. }
  1495. if (eppnt->p_offset + eppnt->p_filesz <= BPRM_BUF_SIZE) {
  1496. memcpy(interp_name, bprm_buf + eppnt->p_offset,
  1497. eppnt->p_filesz);
  1498. } else {
  1499. retval = pread(image_fd, interp_name, eppnt->p_filesz,
  1500. eppnt->p_offset);
  1501. if (retval != eppnt->p_filesz) {
  1502. goto exit_perror;
  1503. }
  1504. }
  1505. if (interp_name[eppnt->p_filesz - 1] != 0) {
  1506. errmsg = "Invalid PT_INTERP entry";
  1507. goto exit_errmsg;
  1508. }
  1509. *pinterp_name = interp_name;
  1510. }
  1511. }
  1512. if (info->end_data == 0) {
  1513. info->start_data = info->end_code;
  1514. info->end_data = info->end_code;
  1515. info->brk = info->end_code;
  1516. }
  1517. if (qemu_log_enabled()) {
  1518. load_symbols(ehdr, image_fd, load_bias);
  1519. }
  1520. close(image_fd);
  1521. return;
  1522. exit_read:
  1523. if (retval >= 0) {
  1524. errmsg = "Incomplete read of file header";
  1525. goto exit_errmsg;
  1526. }
  1527. exit_perror:
  1528. errmsg = strerror(errno);
  1529. exit_errmsg:
  1530. fprintf(stderr, "%s: %s\n", image_name, errmsg);
  1531. exit(-1);
  1532. }
  1533. static void load_elf_interp(const char *filename, struct image_info *info,
  1534. char bprm_buf[BPRM_BUF_SIZE])
  1535. {
  1536. int fd, retval;
  1537. fd = open(path(filename), O_RDONLY);
  1538. if (fd < 0) {
  1539. goto exit_perror;
  1540. }
  1541. retval = read(fd, bprm_buf, BPRM_BUF_SIZE);
  1542. if (retval < 0) {
  1543. goto exit_perror;
  1544. }
  1545. if (retval < BPRM_BUF_SIZE) {
  1546. memset(bprm_buf + retval, 0, BPRM_BUF_SIZE - retval);
  1547. }
  1548. load_elf_image(filename, fd, info, NULL, bprm_buf);
  1549. return;
  1550. exit_perror:
  1551. fprintf(stderr, "%s: %s\n", filename, strerror(errno));
  1552. exit(-1);
  1553. }
  1554. static int symfind(const void *s0, const void *s1)
  1555. {
  1556. target_ulong addr = *(target_ulong *)s0;
  1557. struct elf_sym *sym = (struct elf_sym *)s1;
  1558. int result = 0;
  1559. if (addr < sym->st_value) {
  1560. result = -1;
  1561. } else if (addr >= sym->st_value + sym->st_size) {
  1562. result = 1;
  1563. }
  1564. return result;
  1565. }
  1566. static const char *lookup_symbolxx(struct syminfo *s, target_ulong orig_addr)
  1567. {
  1568. #if ELF_CLASS == ELFCLASS32
  1569. struct elf_sym *syms = s->disas_symtab.elf32;
  1570. #else
  1571. struct elf_sym *syms = s->disas_symtab.elf64;
  1572. #endif
  1573. // binary search
  1574. struct elf_sym *sym;
  1575. sym = bsearch(&orig_addr, syms, s->disas_num_syms, sizeof(*syms), symfind);
  1576. if (sym != NULL) {
  1577. return s->disas_strtab + sym->st_name;
  1578. }
  1579. return "";
  1580. }
  1581. /* FIXME: This should use elf_ops.h */
  1582. static int symcmp(const void *s0, const void *s1)
  1583. {
  1584. struct elf_sym *sym0 = (struct elf_sym *)s0;
  1585. struct elf_sym *sym1 = (struct elf_sym *)s1;
  1586. return (sym0->st_value < sym1->st_value)
  1587. ? -1
  1588. : ((sym0->st_value > sym1->st_value) ? 1 : 0);
  1589. }
  1590. /* Best attempt to load symbols from this ELF object. */
  1591. static void load_symbols(struct elfhdr *hdr, int fd, abi_ulong load_bias)
  1592. {
  1593. int i, shnum, nsyms, sym_idx = 0, str_idx = 0;
  1594. struct elf_shdr *shdr;
  1595. char *strings = NULL;
  1596. struct syminfo *s = NULL;
  1597. struct elf_sym *new_syms, *syms = NULL;
  1598. shnum = hdr->e_shnum;
  1599. i = shnum * sizeof(struct elf_shdr);
  1600. shdr = (struct elf_shdr *)alloca(i);
  1601. if (pread(fd, shdr, i, hdr->e_shoff) != i) {
  1602. return;
  1603. }
  1604. bswap_shdr(shdr, shnum);
  1605. for (i = 0; i < shnum; ++i) {
  1606. if (shdr[i].sh_type == SHT_SYMTAB) {
  1607. sym_idx = i;
  1608. str_idx = shdr[i].sh_link;
  1609. goto found;
  1610. }
  1611. }
  1612. /* There will be no symbol table if the file was stripped. */
  1613. return;
  1614. found:
  1615. /* Now know where the strtab and symtab are. Snarf them. */
  1616. s = malloc(sizeof(*s));
  1617. if (!s) {
  1618. goto give_up;
  1619. }
  1620. i = shdr[str_idx].sh_size;
  1621. s->disas_strtab = strings = malloc(i);
  1622. if (!strings || pread(fd, strings, i, shdr[str_idx].sh_offset) != i) {
  1623. goto give_up;
  1624. }
  1625. i = shdr[sym_idx].sh_size;
  1626. syms = malloc(i);
  1627. if (!syms || pread(fd, syms, i, shdr[sym_idx].sh_offset) != i) {
  1628. goto give_up;
  1629. }
  1630. nsyms = i / sizeof(struct elf_sym);
  1631. for (i = 0; i < nsyms; ) {
  1632. bswap_sym(syms + i);
  1633. /* Throw away entries which we do not need. */
  1634. if (syms[i].st_shndx == SHN_UNDEF
  1635. || syms[i].st_shndx >= SHN_LORESERVE
  1636. || ELF_ST_TYPE(syms[i].st_info) != STT_FUNC) {
  1637. if (i < --nsyms) {
  1638. syms[i] = syms[nsyms];
  1639. }
  1640. } else {
  1641. #if defined(TARGET_ARM) || defined (TARGET_MIPS)
  1642. /* The bottom address bit marks a Thumb or MIPS16 symbol. */
  1643. syms[i].st_value &= ~(target_ulong)1;
  1644. #endif
  1645. syms[i].st_value += load_bias;
  1646. i++;
  1647. }
  1648. }
  1649. /* No "useful" symbol. */
  1650. if (nsyms == 0) {
  1651. goto give_up;
  1652. }
  1653. /* Attempt to free the storage associated with the local symbols
  1654. that we threw away. Whether or not this has any effect on the
  1655. memory allocation depends on the malloc implementation and how
  1656. many symbols we managed to discard. */
  1657. new_syms = realloc(syms, nsyms * sizeof(*syms));
  1658. if (new_syms == NULL) {
  1659. goto give_up;
  1660. }
  1661. syms = new_syms;
  1662. qsort(syms, nsyms, sizeof(*syms), symcmp);
  1663. s->disas_num_syms = nsyms;
  1664. #if ELF_CLASS == ELFCLASS32
  1665. s->disas_symtab.elf32 = syms;
  1666. #else
  1667. s->disas_symtab.elf64 = syms;
  1668. #endif
  1669. s->lookup_symbol = lookup_symbolxx;
  1670. s->next = syminfos;
  1671. syminfos = s;
  1672. return;
  1673. give_up:
  1674. free(s);
  1675. free(strings);
  1676. free(syms);
  1677. }
  1678. int load_elf_binary(struct linux_binprm * bprm, struct target_pt_regs * regs,
  1679. struct image_info * info)
  1680. {
  1681. struct image_info interp_info;
  1682. struct elfhdr elf_ex;
  1683. char *elf_interpreter = NULL;
  1684. info->start_mmap = (abi_ulong)ELF_START_MMAP;
  1685. info->mmap = 0;
  1686. info->rss = 0;
  1687. load_elf_image(bprm->filename, bprm->fd, info,
  1688. &elf_interpreter, bprm->buf);
  1689. /* ??? We need a copy of the elf header for passing to create_elf_tables.
  1690. If we do nothing, we'll have overwritten this when we re-use bprm->buf
  1691. when we load the interpreter. */
  1692. elf_ex = *(struct elfhdr *)bprm->buf;
  1693. bprm->p = copy_elf_strings(1, &bprm->filename, bprm->page, bprm->p);
  1694. bprm->p = copy_elf_strings(bprm->envc,bprm->envp,bprm->page,bprm->p);
  1695. bprm->p = copy_elf_strings(bprm->argc,bprm->argv,bprm->page,bprm->p);
  1696. if (!bprm->p) {
  1697. fprintf(stderr, "%s: %s\n", bprm->filename, strerror(E2BIG));
  1698. exit(-1);
  1699. }
  1700. /* Do this so that we can load the interpreter, if need be. We will
  1701. change some of these later */
  1702. bprm->p = setup_arg_pages(bprm->p, bprm, info);
  1703. if (elf_interpreter) {
  1704. load_elf_interp(elf_interpreter, &interp_info, bprm->buf);
  1705. /* If the program interpreter is one of these two, then assume
  1706. an iBCS2 image. Otherwise assume a native linux image. */
  1707. if (strcmp(elf_interpreter, "/usr/lib/libc.so.1") == 0
  1708. || strcmp(elf_interpreter, "/usr/lib/ld.so.1") == 0) {
  1709. info->personality = PER_SVR4;
  1710. /* Why this, you ask??? Well SVr4 maps page 0 as read-only,
  1711. and some applications "depend" upon this behavior. Since
  1712. we do not have the power to recompile these, we emulate
  1713. the SVr4 behavior. Sigh. */
  1714. target_mmap(0, qemu_host_page_size, PROT_READ | PROT_EXEC,
  1715. MAP_FIXED | MAP_PRIVATE, -1, 0);
  1716. }
  1717. }
  1718. bprm->p = create_elf_tables(bprm->p, bprm->argc, bprm->envc, &elf_ex,
  1719. info, (elf_interpreter ? &interp_info : NULL));
  1720. info->start_stack = bprm->p;
  1721. /* If we have an interpreter, set that as the program's entry point.
  1722. Copy the load_bias as well, to help PPC64 interpret the entry
  1723. point as a function descriptor. Do this after creating elf tables
  1724. so that we copy the original program entry point into the AUXV. */
  1725. if (elf_interpreter) {
  1726. info->load_bias = interp_info.load_bias;
  1727. info->entry = interp_info.entry;
  1728. free(elf_interpreter);
  1729. }
  1730. #ifdef USE_ELF_CORE_DUMP
  1731. bprm->core_dump = &elf_core_dump;
  1732. #endif
  1733. return 0;
  1734. }
  1735. #ifdef USE_ELF_CORE_DUMP
  1736. /*
  1737. * Definitions to generate Intel SVR4-like core files.
  1738. * These mostly have the same names as the SVR4 types with "target_elf_"
  1739. * tacked on the front to prevent clashes with linux definitions,
  1740. * and the typedef forms have been avoided. This is mostly like
  1741. * the SVR4 structure, but more Linuxy, with things that Linux does
  1742. * not support and which gdb doesn't really use excluded.
  1743. *
  1744. * Fields we don't dump (their contents is zero) in linux-user qemu
  1745. * are marked with XXX.
  1746. *
  1747. * Core dump code is copied from linux kernel (fs/binfmt_elf.c).
  1748. *
  1749. * Porting ELF coredump for target is (quite) simple process. First you
  1750. * define USE_ELF_CORE_DUMP in target ELF code (where init_thread() for
  1751. * the target resides):
  1752. *
  1753. * #define USE_ELF_CORE_DUMP
  1754. *
  1755. * Next you define type of register set used for dumping. ELF specification
  1756. * says that it needs to be array of elf_greg_t that has size of ELF_NREG.
  1757. *
  1758. * typedef <target_regtype> target_elf_greg_t;
  1759. * #define ELF_NREG <number of registers>
  1760. * typedef taret_elf_greg_t target_elf_gregset_t[ELF_NREG];
  1761. *
  1762. * Last step is to implement target specific function that copies registers
  1763. * from given cpu into just specified register set. Prototype is:
  1764. *
  1765. * static void elf_core_copy_regs(taret_elf_gregset_t *regs,
  1766. * const CPUArchState *env);
  1767. *
  1768. * Parameters:
  1769. * regs - copy register values into here (allocated and zeroed by caller)
  1770. * env - copy registers from here
  1771. *
  1772. * Example for ARM target is provided in this file.
  1773. */
  1774. /* An ELF note in memory */
  1775. struct memelfnote {
  1776. const char *name;
  1777. size_t namesz;
  1778. size_t namesz_rounded;
  1779. int type;
  1780. size_t datasz;
  1781. size_t datasz_rounded;
  1782. void *data;
  1783. size_t notesz;
  1784. };
  1785. struct target_elf_siginfo {
  1786. target_int si_signo; /* signal number */
  1787. target_int si_code; /* extra code */
  1788. target_int si_errno; /* errno */
  1789. };
  1790. struct target_elf_prstatus {
  1791. struct target_elf_siginfo pr_info; /* Info associated with signal */
  1792. target_short pr_cursig; /* Current signal */
  1793. target_ulong pr_sigpend; /* XXX */
  1794. target_ulong pr_sighold; /* XXX */
  1795. target_pid_t pr_pid;
  1796. target_pid_t pr_ppid;
  1797. target_pid_t pr_pgrp;
  1798. target_pid_t pr_sid;
  1799. struct target_timeval pr_utime; /* XXX User time */
  1800. struct target_timeval pr_stime; /* XXX System time */
  1801. struct target_timeval pr_cutime; /* XXX Cumulative user time */
  1802. struct target_timeval pr_cstime; /* XXX Cumulative system time */
  1803. target_elf_gregset_t pr_reg; /* GP registers */
  1804. target_int pr_fpvalid; /* XXX */
  1805. };
  1806. #define ELF_PRARGSZ (80) /* Number of chars for args */
  1807. struct target_elf_prpsinfo {
  1808. char pr_state; /* numeric process state */
  1809. char pr_sname; /* char for pr_state */
  1810. char pr_zomb; /* zombie */
  1811. char pr_nice; /* nice val */
  1812. target_ulong pr_flag; /* flags */
  1813. target_uid_t pr_uid;
  1814. target_gid_t pr_gid;
  1815. target_pid_t pr_pid, pr_ppid, pr_pgrp, pr_sid;
  1816. /* Lots missing */
  1817. char pr_fname[16]; /* filename of executable */
  1818. char pr_psargs[ELF_PRARGSZ]; /* initial part of arg list */
  1819. };
  1820. /* Here is the structure in which status of each thread is captured. */
  1821. struct elf_thread_status {
  1822. QTAILQ_ENTRY(elf_thread_status) ets_link;
  1823. struct target_elf_prstatus prstatus; /* NT_PRSTATUS */
  1824. #if 0
  1825. elf_fpregset_t fpu; /* NT_PRFPREG */
  1826. struct task_struct *thread;
  1827. elf_fpxregset_t xfpu; /* ELF_CORE_XFPREG_TYPE */
  1828. #endif
  1829. struct memelfnote notes[1];
  1830. int num_notes;
  1831. };
  1832. struct elf_note_info {
  1833. struct memelfnote *notes;
  1834. struct target_elf_prstatus *prstatus; /* NT_PRSTATUS */
  1835. struct target_elf_prpsinfo *psinfo; /* NT_PRPSINFO */
  1836. QTAILQ_HEAD(thread_list_head, elf_thread_status) thread_list;
  1837. #if 0
  1838. /*
  1839. * Current version of ELF coredump doesn't support
  1840. * dumping fp regs etc.
  1841. */
  1842. elf_fpregset_t *fpu;
  1843. elf_fpxregset_t *xfpu;
  1844. int thread_status_size;
  1845. #endif
  1846. int notes_size;
  1847. int numnote;
  1848. };
  1849. struct vm_area_struct {
  1850. abi_ulong vma_start; /* start vaddr of memory region */
  1851. abi_ulong vma_end; /* end vaddr of memory region */
  1852. abi_ulong vma_flags; /* protection etc. flags for the region */
  1853. QTAILQ_ENTRY(vm_area_struct) vma_link;
  1854. };
  1855. struct mm_struct {
  1856. QTAILQ_HEAD(, vm_area_struct) mm_mmap;
  1857. int mm_count; /* number of mappings */
  1858. };
  1859. static struct mm_struct *vma_init(void);
  1860. static void vma_delete(struct mm_struct *);
  1861. static int vma_add_mapping(struct mm_struct *, abi_ulong,
  1862. abi_ulong, abi_ulong);
  1863. static int vma_get_mapping_count(const struct mm_struct *);
  1864. static struct vm_area_struct *vma_first(const struct mm_struct *);
  1865. static struct vm_area_struct *vma_next(struct vm_area_struct *);
  1866. static abi_ulong vma_dump_size(const struct vm_area_struct *);
  1867. static int vma_walker(void *priv, abi_ulong start, abi_ulong end,
  1868. unsigned long flags);
  1869. static void fill_elf_header(struct elfhdr *, int, uint16_t, uint32_t);
  1870. static void fill_note(struct memelfnote *, const char *, int,
  1871. unsigned int, void *);
  1872. static void fill_prstatus(struct target_elf_prstatus *, const TaskState *, int);
  1873. static int fill_psinfo(struct target_elf_prpsinfo *, const TaskState *);
  1874. static void fill_auxv_note(struct memelfnote *, const TaskState *);
  1875. static void fill_elf_note_phdr(struct elf_phdr *, int, off_t);
  1876. static size_t note_size(const struct memelfnote *);
  1877. static void free_note_info(struct elf_note_info *);
  1878. static int fill_note_info(struct elf_note_info *, long, const CPUArchState *);
  1879. static void fill_thread_info(struct elf_note_info *, const CPUArchState *);
  1880. static int core_dump_filename(const TaskState *, char *, size_t);
  1881. static int dump_write(int, const void *, size_t);
  1882. static int write_note(struct memelfnote *, int);
  1883. static int write_note_info(struct elf_note_info *, int);
  1884. #ifdef BSWAP_NEEDED
  1885. static void bswap_prstatus(struct target_elf_prstatus *prstatus)
  1886. {
  1887. prstatus->pr_info.si_signo = tswapl(prstatus->pr_info.si_signo);
  1888. prstatus->pr_info.si_code = tswapl(prstatus->pr_info.si_code);
  1889. prstatus->pr_info.si_errno = tswapl(prstatus->pr_info.si_errno);
  1890. prstatus->pr_cursig = tswap16(prstatus->pr_cursig);
  1891. prstatus->pr_sigpend = tswapl(prstatus->pr_sigpend);
  1892. prstatus->pr_sighold = tswapl(prstatus->pr_sighold);
  1893. prstatus->pr_pid = tswap32(prstatus->pr_pid);
  1894. prstatus->pr_ppid = tswap32(prstatus->pr_ppid);
  1895. prstatus->pr_pgrp = tswap32(prstatus->pr_pgrp);
  1896. prstatus->pr_sid = tswap32(prstatus->pr_sid);
  1897. /* cpu times are not filled, so we skip them */
  1898. /* regs should be in correct format already */
  1899. prstatus->pr_fpvalid = tswap32(prstatus->pr_fpvalid);
  1900. }
  1901. static void bswap_psinfo(struct target_elf_prpsinfo *psinfo)
  1902. {
  1903. psinfo->pr_flag = tswapl(psinfo->pr_flag);
  1904. psinfo->pr_uid = tswap16(psinfo->pr_uid);
  1905. psinfo->pr_gid = tswap16(psinfo->pr_gid);
  1906. psinfo->pr_pid = tswap32(psinfo->pr_pid);
  1907. psinfo->pr_ppid = tswap32(psinfo->pr_ppid);
  1908. psinfo->pr_pgrp = tswap32(psinfo->pr_pgrp);
  1909. psinfo->pr_sid = tswap32(psinfo->pr_sid);
  1910. }
  1911. static void bswap_note(struct elf_note *en)
  1912. {
  1913. bswap32s(&en->n_namesz);
  1914. bswap32s(&en->n_descsz);
  1915. bswap32s(&en->n_type);
  1916. }
  1917. #else
  1918. static inline void bswap_prstatus(struct target_elf_prstatus *p) { }
  1919. static inline void bswap_psinfo(struct target_elf_prpsinfo *p) {}
  1920. static inline void bswap_note(struct elf_note *en) { }
  1921. #endif /* BSWAP_NEEDED */
  1922. /*
  1923. * Minimal support for linux memory regions. These are needed
  1924. * when we are finding out what memory exactly belongs to
  1925. * emulated process. No locks needed here, as long as
  1926. * thread that received the signal is stopped.
  1927. */
  1928. static struct mm_struct *vma_init(void)
  1929. {
  1930. struct mm_struct *mm;
  1931. if ((mm = g_malloc(sizeof (*mm))) == NULL)
  1932. return (NULL);
  1933. mm->mm_count = 0;
  1934. QTAILQ_INIT(&mm->mm_mmap);
  1935. return (mm);
  1936. }
  1937. static void vma_delete(struct mm_struct *mm)
  1938. {
  1939. struct vm_area_struct *vma;
  1940. while ((vma = vma_first(mm)) != NULL) {
  1941. QTAILQ_REMOVE(&mm->mm_mmap, vma, vma_link);
  1942. g_free(vma);
  1943. }
  1944. g_free(mm);
  1945. }
  1946. static int vma_add_mapping(struct mm_struct *mm, abi_ulong start,
  1947. abi_ulong end, abi_ulong flags)
  1948. {
  1949. struct vm_area_struct *vma;
  1950. if ((vma = g_malloc0(sizeof (*vma))) == NULL)
  1951. return (-1);
  1952. vma->vma_start = start;
  1953. vma->vma_end = end;
  1954. vma->vma_flags = flags;
  1955. QTAILQ_INSERT_TAIL(&mm->mm_mmap, vma, vma_link);
  1956. mm->mm_count++;
  1957. return (0);
  1958. }
  1959. static struct vm_area_struct *vma_first(const struct mm_struct *mm)
  1960. {
  1961. return (QTAILQ_FIRST(&mm->mm_mmap));
  1962. }
  1963. static struct vm_area_struct *vma_next(struct vm_area_struct *vma)
  1964. {
  1965. return (QTAILQ_NEXT(vma, vma_link));
  1966. }
  1967. static int vma_get_mapping_count(const struct mm_struct *mm)
  1968. {
  1969. return (mm->mm_count);
  1970. }
  1971. /*
  1972. * Calculate file (dump) size of given memory region.
  1973. */
  1974. static abi_ulong vma_dump_size(const struct vm_area_struct *vma)
  1975. {
  1976. /* if we cannot even read the first page, skip it */
  1977. if (!access_ok(VERIFY_READ, vma->vma_start, TARGET_PAGE_SIZE))
  1978. return (0);
  1979. /*
  1980. * Usually we don't dump executable pages as they contain
  1981. * non-writable code that debugger can read directly from
  1982. * target library etc. However, thread stacks are marked
  1983. * also executable so we read in first page of given region
  1984. * and check whether it contains elf header. If there is
  1985. * no elf header, we dump it.
  1986. */
  1987. if (vma->vma_flags & PROT_EXEC) {
  1988. char page[TARGET_PAGE_SIZE];
  1989. copy_from_user(page, vma->vma_start, sizeof (page));
  1990. if ((page[EI_MAG0] == ELFMAG0) &&
  1991. (page[EI_MAG1] == ELFMAG1) &&
  1992. (page[EI_MAG2] == ELFMAG2) &&
  1993. (page[EI_MAG3] == ELFMAG3)) {
  1994. /*
  1995. * Mappings are possibly from ELF binary. Don't dump
  1996. * them.
  1997. */
  1998. return (0);
  1999. }
  2000. }
  2001. return (vma->vma_end - vma->vma_start);
  2002. }
  2003. static int vma_walker(void *priv, abi_ulong start, abi_ulong end,
  2004. unsigned long flags)
  2005. {
  2006. struct mm_struct *mm = (struct mm_struct *)priv;
  2007. vma_add_mapping(mm, start, end, flags);
  2008. return (0);
  2009. }
  2010. static void fill_note(struct memelfnote *note, const char *name, int type,
  2011. unsigned int sz, void *data)
  2012. {
  2013. unsigned int namesz;
  2014. namesz = strlen(name) + 1;
  2015. note->name = name;
  2016. note->namesz = namesz;
  2017. note->namesz_rounded = roundup(namesz, sizeof (int32_t));
  2018. note->type = type;
  2019. note->datasz = sz;
  2020. note->datasz_rounded = roundup(sz, sizeof (int32_t));
  2021. note->data = data;
  2022. /*
  2023. * We calculate rounded up note size here as specified by
  2024. * ELF document.
  2025. */
  2026. note->notesz = sizeof (struct elf_note) +
  2027. note->namesz_rounded + note->datasz_rounded;
  2028. }
  2029. static void fill_elf_header(struct elfhdr *elf, int segs, uint16_t machine,
  2030. uint32_t flags)
  2031. {
  2032. (void) memset(elf, 0, sizeof(*elf));
  2033. (void) memcpy(elf->e_ident, ELFMAG, SELFMAG);
  2034. elf->e_ident[EI_CLASS] = ELF_CLASS;
  2035. elf->e_ident[EI_DATA] = ELF_DATA;
  2036. elf->e_ident[EI_VERSION] = EV_CURRENT;
  2037. elf->e_ident[EI_OSABI] = ELF_OSABI;
  2038. elf->e_type = ET_CORE;
  2039. elf->e_machine = machine;
  2040. elf->e_version = EV_CURRENT;
  2041. elf->e_phoff = sizeof(struct elfhdr);
  2042. elf->e_flags = flags;
  2043. elf->e_ehsize = sizeof(struct elfhdr);
  2044. elf->e_phentsize = sizeof(struct elf_phdr);
  2045. elf->e_phnum = segs;
  2046. bswap_ehdr(elf);
  2047. }
  2048. static void fill_elf_note_phdr(struct elf_phdr *phdr, int sz, off_t offset)
  2049. {
  2050. phdr->p_type = PT_NOTE;
  2051. phdr->p_offset = offset;
  2052. phdr->p_vaddr = 0;
  2053. phdr->p_paddr = 0;
  2054. phdr->p_filesz = sz;
  2055. phdr->p_memsz = 0;
  2056. phdr->p_flags = 0;
  2057. phdr->p_align = 0;
  2058. bswap_phdr(phdr, 1);
  2059. }
  2060. static size_t note_size(const struct memelfnote *note)
  2061. {
  2062. return (note->notesz);
  2063. }
  2064. static void fill_prstatus(struct target_elf_prstatus *prstatus,
  2065. const TaskState *ts, int signr)
  2066. {
  2067. (void) memset(prstatus, 0, sizeof (*prstatus));
  2068. prstatus->pr_info.si_signo = prstatus->pr_cursig = signr;
  2069. prstatus->pr_pid = ts->ts_tid;
  2070. prstatus->pr_ppid = getppid();
  2071. prstatus->pr_pgrp = getpgrp();
  2072. prstatus->pr_sid = getsid(0);
  2073. bswap_prstatus(prstatus);
  2074. }
  2075. static int fill_psinfo(struct target_elf_prpsinfo *psinfo, const TaskState *ts)
  2076. {
  2077. char *base_filename;
  2078. unsigned int i, len;
  2079. (void) memset(psinfo, 0, sizeof (*psinfo));
  2080. len = ts->info->arg_end - ts->info->arg_start;
  2081. if (len >= ELF_PRARGSZ)
  2082. len = ELF_PRARGSZ - 1;
  2083. if (copy_from_user(&psinfo->pr_psargs, ts->info->arg_start, len))
  2084. return -EFAULT;
  2085. for (i = 0; i < len; i++)
  2086. if (psinfo->pr_psargs[i] == 0)
  2087. psinfo->pr_psargs[i] = ' ';
  2088. psinfo->pr_psargs[len] = 0;
  2089. psinfo->pr_pid = getpid();
  2090. psinfo->pr_ppid = getppid();
  2091. psinfo->pr_pgrp = getpgrp();
  2092. psinfo->pr_sid = getsid(0);
  2093. psinfo->pr_uid = getuid();
  2094. psinfo->pr_gid = getgid();
  2095. base_filename = g_path_get_basename(ts->bprm->filename);
  2096. /*
  2097. * Using strncpy here is fine: at max-length,
  2098. * this field is not NUL-terminated.
  2099. */
  2100. (void) strncpy(psinfo->pr_fname, base_filename,
  2101. sizeof(psinfo->pr_fname));
  2102. g_free(base_filename);
  2103. bswap_psinfo(psinfo);
  2104. return (0);
  2105. }
  2106. static void fill_auxv_note(struct memelfnote *note, const TaskState *ts)
  2107. {
  2108. elf_addr_t auxv = (elf_addr_t)ts->info->saved_auxv;
  2109. elf_addr_t orig_auxv = auxv;
  2110. void *ptr;
  2111. int len = ts->info->auxv_len;
  2112. /*
  2113. * Auxiliary vector is stored in target process stack. It contains
  2114. * {type, value} pairs that we need to dump into note. This is not
  2115. * strictly necessary but we do it here for sake of completeness.
  2116. */
  2117. /* read in whole auxv vector and copy it to memelfnote */
  2118. ptr = lock_user(VERIFY_READ, orig_auxv, len, 0);
  2119. if (ptr != NULL) {
  2120. fill_note(note, "CORE", NT_AUXV, len, ptr);
  2121. unlock_user(ptr, auxv, len);
  2122. }
  2123. }
  2124. /*
  2125. * Constructs name of coredump file. We have following convention
  2126. * for the name:
  2127. * qemu_<basename-of-target-binary>_<date>-<time>_<pid>.core
  2128. *
  2129. * Returns 0 in case of success, -1 otherwise (errno is set).
  2130. */
  2131. static int core_dump_filename(const TaskState *ts, char *buf,
  2132. size_t bufsize)
  2133. {
  2134. char timestamp[64];
  2135. char *filename = NULL;
  2136. char *base_filename = NULL;
  2137. struct timeval tv;
  2138. struct tm tm;
  2139. assert(bufsize >= PATH_MAX);
  2140. if (gettimeofday(&tv, NULL) < 0) {
  2141. (void) fprintf(stderr, "unable to get current timestamp: %s",
  2142. strerror(errno));
  2143. return (-1);
  2144. }
  2145. filename = strdup(ts->bprm->filename);
  2146. base_filename = strdup(basename(filename));
  2147. (void) strftime(timestamp, sizeof (timestamp), "%Y%m%d-%H%M%S",
  2148. localtime_r(&tv.tv_sec, &tm));
  2149. (void) snprintf(buf, bufsize, "qemu_%s_%s_%d.core",
  2150. base_filename, timestamp, (int)getpid());
  2151. free(base_filename);
  2152. free(filename);
  2153. return (0);
  2154. }
  2155. static int dump_write(int fd, const void *ptr, size_t size)
  2156. {
  2157. const char *bufp = (const char *)ptr;
  2158. ssize_t bytes_written, bytes_left;
  2159. struct rlimit dumpsize;
  2160. off_t pos;
  2161. bytes_written = 0;
  2162. getrlimit(RLIMIT_CORE, &dumpsize);
  2163. if ((pos = lseek(fd, 0, SEEK_CUR))==-1) {
  2164. if (errno == ESPIPE) { /* not a seekable stream */
  2165. bytes_left = size;
  2166. } else {
  2167. return pos;
  2168. }
  2169. } else {
  2170. if (dumpsize.rlim_cur <= pos) {
  2171. return -1;
  2172. } else if (dumpsize.rlim_cur == RLIM_INFINITY) {
  2173. bytes_left = size;
  2174. } else {
  2175. size_t limit_left=dumpsize.rlim_cur - pos;
  2176. bytes_left = limit_left >= size ? size : limit_left ;
  2177. }
  2178. }
  2179. /*
  2180. * In normal conditions, single write(2) should do but
  2181. * in case of socket etc. this mechanism is more portable.
  2182. */
  2183. do {
  2184. bytes_written = write(fd, bufp, bytes_left);
  2185. if (bytes_written < 0) {
  2186. if (errno == EINTR)
  2187. continue;
  2188. return (-1);
  2189. } else if (bytes_written == 0) { /* eof */
  2190. return (-1);
  2191. }
  2192. bufp += bytes_written;
  2193. bytes_left -= bytes_written;
  2194. } while (bytes_left > 0);
  2195. return (0);
  2196. }
  2197. static int write_note(struct memelfnote *men, int fd)
  2198. {
  2199. struct elf_note en;
  2200. en.n_namesz = men->namesz;
  2201. en.n_type = men->type;
  2202. en.n_descsz = men->datasz;
  2203. bswap_note(&en);
  2204. if (dump_write(fd, &en, sizeof(en)) != 0)
  2205. return (-1);
  2206. if (dump_write(fd, men->name, men->namesz_rounded) != 0)
  2207. return (-1);
  2208. if (dump_write(fd, men->data, men->datasz_rounded) != 0)
  2209. return (-1);
  2210. return (0);
  2211. }
  2212. static void fill_thread_info(struct elf_note_info *info, const CPUArchState *env)
  2213. {
  2214. TaskState *ts = (TaskState *)env->opaque;
  2215. struct elf_thread_status *ets;
  2216. ets = g_malloc0(sizeof (*ets));
  2217. ets->num_notes = 1; /* only prstatus is dumped */
  2218. fill_prstatus(&ets->prstatus, ts, 0);
  2219. elf_core_copy_regs(&ets->prstatus.pr_reg, env);
  2220. fill_note(&ets->notes[0], "CORE", NT_PRSTATUS, sizeof (ets->prstatus),
  2221. &ets->prstatus);
  2222. QTAILQ_INSERT_TAIL(&info->thread_list, ets, ets_link);
  2223. info->notes_size += note_size(&ets->notes[0]);
  2224. }
  2225. static int fill_note_info(struct elf_note_info *info,
  2226. long signr, const CPUArchState *env)
  2227. {
  2228. #define NUMNOTES 3
  2229. CPUArchState *cpu = NULL;
  2230. TaskState *ts = (TaskState *)env->opaque;
  2231. int i;
  2232. (void) memset(info, 0, sizeof (*info));
  2233. QTAILQ_INIT(&info->thread_list);
  2234. info->notes = g_malloc0(NUMNOTES * sizeof (struct memelfnote));
  2235. if (info->notes == NULL)
  2236. return (-ENOMEM);
  2237. info->prstatus = g_malloc0(sizeof (*info->prstatus));
  2238. if (info->prstatus == NULL)
  2239. return (-ENOMEM);
  2240. info->psinfo = g_malloc0(sizeof (*info->psinfo));
  2241. if (info->prstatus == NULL)
  2242. return (-ENOMEM);
  2243. /*
  2244. * First fill in status (and registers) of current thread
  2245. * including process info & aux vector.
  2246. */
  2247. fill_prstatus(info->prstatus, ts, signr);
  2248. elf_core_copy_regs(&info->prstatus->pr_reg, env);
  2249. fill_note(&info->notes[0], "CORE", NT_PRSTATUS,
  2250. sizeof (*info->prstatus), info->prstatus);
  2251. fill_psinfo(info->psinfo, ts);
  2252. fill_note(&info->notes[1], "CORE", NT_PRPSINFO,
  2253. sizeof (*info->psinfo), info->psinfo);
  2254. fill_auxv_note(&info->notes[2], ts);
  2255. info->numnote = 3;
  2256. info->notes_size = 0;
  2257. for (i = 0; i < info->numnote; i++)
  2258. info->notes_size += note_size(&info->notes[i]);
  2259. /* read and fill status of all threads */
  2260. cpu_list_lock();
  2261. for (cpu = first_cpu; cpu != NULL; cpu = cpu->next_cpu) {
  2262. if (cpu == thread_env)
  2263. continue;
  2264. fill_thread_info(info, cpu);
  2265. }
  2266. cpu_list_unlock();
  2267. return (0);
  2268. }
  2269. static void free_note_info(struct elf_note_info *info)
  2270. {
  2271. struct elf_thread_status *ets;
  2272. while (!QTAILQ_EMPTY(&info->thread_list)) {
  2273. ets = QTAILQ_FIRST(&info->thread_list);
  2274. QTAILQ_REMOVE(&info->thread_list, ets, ets_link);
  2275. g_free(ets);
  2276. }
  2277. g_free(info->prstatus);
  2278. g_free(info->psinfo);
  2279. g_free(info->notes);
  2280. }
  2281. static int write_note_info(struct elf_note_info *info, int fd)
  2282. {
  2283. struct elf_thread_status *ets;
  2284. int i, error = 0;
  2285. /* write prstatus, psinfo and auxv for current thread */
  2286. for (i = 0; i < info->numnote; i++)
  2287. if ((error = write_note(&info->notes[i], fd)) != 0)
  2288. return (error);
  2289. /* write prstatus for each thread */
  2290. for (ets = info->thread_list.tqh_first; ets != NULL;
  2291. ets = ets->ets_link.tqe_next) {
  2292. if ((error = write_note(&ets->notes[0], fd)) != 0)
  2293. return (error);
  2294. }
  2295. return (0);
  2296. }
  2297. /*
  2298. * Write out ELF coredump.
  2299. *
  2300. * See documentation of ELF object file format in:
  2301. * http://www.caldera.com/developers/devspecs/gabi41.pdf
  2302. *
  2303. * Coredump format in linux is following:
  2304. *
  2305. * 0 +----------------------+ \
  2306. * | ELF header | ET_CORE |
  2307. * +----------------------+ |
  2308. * | ELF program headers | |--- headers
  2309. * | - NOTE section | |
  2310. * | - PT_LOAD sections | |
  2311. * +----------------------+ /
  2312. * | NOTEs: |
  2313. * | - NT_PRSTATUS |
  2314. * | - NT_PRSINFO |
  2315. * | - NT_AUXV |
  2316. * +----------------------+ <-- aligned to target page
  2317. * | Process memory dump |
  2318. * : :
  2319. * . .
  2320. * : :
  2321. * | |
  2322. * +----------------------+
  2323. *
  2324. * NT_PRSTATUS -> struct elf_prstatus (per thread)
  2325. * NT_PRSINFO -> struct elf_prpsinfo
  2326. * NT_AUXV is array of { type, value } pairs (see fill_auxv_note()).
  2327. *
  2328. * Format follows System V format as close as possible. Current
  2329. * version limitations are as follows:
  2330. * - no floating point registers are dumped
  2331. *
  2332. * Function returns 0 in case of success, negative errno otherwise.
  2333. *
  2334. * TODO: make this work also during runtime: it should be
  2335. * possible to force coredump from running process and then
  2336. * continue processing. For example qemu could set up SIGUSR2
  2337. * handler (provided that target process haven't registered
  2338. * handler for that) that does the dump when signal is received.
  2339. */
  2340. static int elf_core_dump(int signr, const CPUArchState *env)
  2341. {
  2342. const TaskState *ts = (const TaskState *)env->opaque;
  2343. struct vm_area_struct *vma = NULL;
  2344. char corefile[PATH_MAX];
  2345. struct elf_note_info info;
  2346. struct elfhdr elf;
  2347. struct elf_phdr phdr;
  2348. struct rlimit dumpsize;
  2349. struct mm_struct *mm = NULL;
  2350. off_t offset = 0, data_offset = 0;
  2351. int segs = 0;
  2352. int fd = -1;
  2353. errno = 0;
  2354. getrlimit(RLIMIT_CORE, &dumpsize);
  2355. if (dumpsize.rlim_cur == 0)
  2356. return 0;
  2357. if (core_dump_filename(ts, corefile, sizeof (corefile)) < 0)
  2358. return (-errno);
  2359. if ((fd = open(corefile, O_WRONLY | O_CREAT,
  2360. S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH)) < 0)
  2361. return (-errno);
  2362. /*
  2363. * Walk through target process memory mappings and
  2364. * set up structure containing this information. After
  2365. * this point vma_xxx functions can be used.
  2366. */
  2367. if ((mm = vma_init()) == NULL)
  2368. goto out;
  2369. walk_memory_regions(mm, vma_walker);
  2370. segs = vma_get_mapping_count(mm);
  2371. /*
  2372. * Construct valid coredump ELF header. We also
  2373. * add one more segment for notes.
  2374. */
  2375. fill_elf_header(&elf, segs + 1, ELF_MACHINE, 0);
  2376. if (dump_write(fd, &elf, sizeof (elf)) != 0)
  2377. goto out;
  2378. /* fill in in-memory version of notes */
  2379. if (fill_note_info(&info, signr, env) < 0)
  2380. goto out;
  2381. offset += sizeof (elf); /* elf header */
  2382. offset += (segs + 1) * sizeof (struct elf_phdr); /* program headers */
  2383. /* write out notes program header */
  2384. fill_elf_note_phdr(&phdr, info.notes_size, offset);
  2385. offset += info.notes_size;
  2386. if (dump_write(fd, &phdr, sizeof (phdr)) != 0)
  2387. goto out;
  2388. /*
  2389. * ELF specification wants data to start at page boundary so
  2390. * we align it here.
  2391. */
  2392. data_offset = offset = roundup(offset, ELF_EXEC_PAGESIZE);
  2393. /*
  2394. * Write program headers for memory regions mapped in
  2395. * the target process.
  2396. */
  2397. for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
  2398. (void) memset(&phdr, 0, sizeof (phdr));
  2399. phdr.p_type = PT_LOAD;
  2400. phdr.p_offset = offset;
  2401. phdr.p_vaddr = vma->vma_start;
  2402. phdr.p_paddr = 0;
  2403. phdr.p_filesz = vma_dump_size(vma);
  2404. offset += phdr.p_filesz;
  2405. phdr.p_memsz = vma->vma_end - vma->vma_start;
  2406. phdr.p_flags = vma->vma_flags & PROT_READ ? PF_R : 0;
  2407. if (vma->vma_flags & PROT_WRITE)
  2408. phdr.p_flags |= PF_W;
  2409. if (vma->vma_flags & PROT_EXEC)
  2410. phdr.p_flags |= PF_X;
  2411. phdr.p_align = ELF_EXEC_PAGESIZE;
  2412. bswap_phdr(&phdr, 1);
  2413. dump_write(fd, &phdr, sizeof (phdr));
  2414. }
  2415. /*
  2416. * Next we write notes just after program headers. No
  2417. * alignment needed here.
  2418. */
  2419. if (write_note_info(&info, fd) < 0)
  2420. goto out;
  2421. /* align data to page boundary */
  2422. if (lseek(fd, data_offset, SEEK_SET) != data_offset)
  2423. goto out;
  2424. /*
  2425. * Finally we can dump process memory into corefile as well.
  2426. */
  2427. for (vma = vma_first(mm); vma != NULL; vma = vma_next(vma)) {
  2428. abi_ulong addr;
  2429. abi_ulong end;
  2430. end = vma->vma_start + vma_dump_size(vma);
  2431. for (addr = vma->vma_start; addr < end;
  2432. addr += TARGET_PAGE_SIZE) {
  2433. char page[TARGET_PAGE_SIZE];
  2434. int error;
  2435. /*
  2436. * Read in page from target process memory and
  2437. * write it to coredump file.
  2438. */
  2439. error = copy_from_user(page, addr, sizeof (page));
  2440. if (error != 0) {
  2441. (void) fprintf(stderr, "unable to dump " TARGET_ABI_FMT_lx "\n",
  2442. addr);
  2443. errno = -error;
  2444. goto out;
  2445. }
  2446. if (dump_write(fd, page, TARGET_PAGE_SIZE) < 0)
  2447. goto out;
  2448. }
  2449. }
  2450. out:
  2451. free_note_info(&info);
  2452. if (mm != NULL)
  2453. vma_delete(mm);
  2454. (void) close(fd);
  2455. if (errno != 0)
  2456. return (-errno);
  2457. return (0);
  2458. }
  2459. #endif /* USE_ELF_CORE_DUMP */
  2460. void do_init_thread(struct target_pt_regs *regs, struct image_info *infop)
  2461. {
  2462. init_thread(regs, infop);
  2463. }