elfload.c 83 KB

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