elfload.c 94 KB

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