elfload.c 93 KB

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