readelf.c 30 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196
  1. /*
  2. * Copyright (c) Christos Zoulas 2003.
  3. * All Rights Reserved.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions
  7. * are met:
  8. * 1. Redistributions of source code must retain the above copyright
  9. * notice immediately at the beginning of the file, without modification,
  10. * this list of conditions, and the following disclaimer.
  11. * 2. Redistributions in binary form must reproduce the above copyright
  12. * notice, this list of conditions and the following disclaimer in the
  13. * documentation and/or other materials provided with the distribution.
  14. *
  15. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
  16. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  17. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  18. * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE FOR
  19. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  20. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
  21. * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
  22. * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
  23. * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
  24. * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
  25. * SUCH DAMAGE.
  26. */
  27. #include "file.h"
  28. #ifndef lint
  29. FILE_RCSID("@(#)$File: readelf.c,v 1.97 2013/03/06 03:35:30 christos Exp $")
  30. #endif
  31. #ifdef BUILTIN_ELF
  32. #include <string.h>
  33. #include <ctype.h>
  34. #include <stdlib.h>
  35. #ifdef HAVE_UNISTD_H
  36. #include <unistd.h>
  37. #endif
  38. #include "readelf.h"
  39. #include "magic.h"
  40. #ifdef ELFCORE
  41. private int dophn_core(struct magic_set *, int, int, int, off_t, int, size_t,
  42. off_t, int *);
  43. #endif
  44. private int dophn_exec(struct magic_set *, int, int, int, off_t, int, size_t,
  45. off_t, int *, int);
  46. private int doshn(struct magic_set *, int, int, int, off_t, int, size_t,
  47. off_t, int *, int, int);
  48. private size_t donote(struct magic_set *, void *, size_t, size_t, int,
  49. int, size_t, int *);
  50. #define ELF_ALIGN(a) ((((a) + align - 1) / align) * align)
  51. #define isquote(c) (strchr("'\"`", (c)) != NULL)
  52. private uint16_t getu16(int, uint16_t);
  53. private uint32_t getu32(int, uint32_t);
  54. private uint64_t getu64(int, uint64_t);
  55. private uint16_t
  56. getu16(int swap, uint16_t value)
  57. {
  58. union {
  59. uint16_t ui;
  60. char c[2];
  61. } retval, tmpval;
  62. if (swap) {
  63. tmpval.ui = value;
  64. retval.c[0] = tmpval.c[1];
  65. retval.c[1] = tmpval.c[0];
  66. return retval.ui;
  67. } else
  68. return value;
  69. }
  70. private uint32_t
  71. getu32(int swap, uint32_t value)
  72. {
  73. union {
  74. uint32_t ui;
  75. char c[4];
  76. } retval, tmpval;
  77. if (swap) {
  78. tmpval.ui = value;
  79. retval.c[0] = tmpval.c[3];
  80. retval.c[1] = tmpval.c[2];
  81. retval.c[2] = tmpval.c[1];
  82. retval.c[3] = tmpval.c[0];
  83. return retval.ui;
  84. } else
  85. return value;
  86. }
  87. private uint64_t
  88. getu64(int swap, uint64_t value)
  89. {
  90. union {
  91. uint64_t ui;
  92. char c[8];
  93. } retval, tmpval;
  94. if (swap) {
  95. tmpval.ui = value;
  96. retval.c[0] = tmpval.c[7];
  97. retval.c[1] = tmpval.c[6];
  98. retval.c[2] = tmpval.c[5];
  99. retval.c[3] = tmpval.c[4];
  100. retval.c[4] = tmpval.c[3];
  101. retval.c[5] = tmpval.c[2];
  102. retval.c[6] = tmpval.c[1];
  103. retval.c[7] = tmpval.c[0];
  104. return retval.ui;
  105. } else
  106. return value;
  107. }
  108. #define elf_getu16(swap, value) getu16(swap, value)
  109. #define elf_getu32(swap, value) getu32(swap, value)
  110. #define elf_getu64(swap, value) getu64(swap, value)
  111. #define xsh_addr (clazz == ELFCLASS32 \
  112. ? (void *)&sh32 \
  113. : (void *)&sh64)
  114. #define xsh_sizeof (clazz == ELFCLASS32 \
  115. ? sizeof(sh32) \
  116. : sizeof(sh64))
  117. #define xsh_size (size_t)(clazz == ELFCLASS32 \
  118. ? elf_getu32(swap, sh32.sh_size) \
  119. : elf_getu64(swap, sh64.sh_size))
  120. #define xsh_offset (off_t)(clazz == ELFCLASS32 \
  121. ? elf_getu32(swap, sh32.sh_offset) \
  122. : elf_getu64(swap, sh64.sh_offset))
  123. #define xsh_type (clazz == ELFCLASS32 \
  124. ? elf_getu32(swap, sh32.sh_type) \
  125. : elf_getu32(swap, sh64.sh_type))
  126. #define xsh_name (clazz == ELFCLASS32 \
  127. ? elf_getu32(swap, sh32.sh_name) \
  128. : elf_getu32(swap, sh64.sh_name))
  129. #define xph_addr (clazz == ELFCLASS32 \
  130. ? (void *) &ph32 \
  131. : (void *) &ph64)
  132. #define xph_sizeof (clazz == ELFCLASS32 \
  133. ? sizeof(ph32) \
  134. : sizeof(ph64))
  135. #define xph_type (clazz == ELFCLASS32 \
  136. ? elf_getu32(swap, ph32.p_type) \
  137. : elf_getu32(swap, ph64.p_type))
  138. #define xph_offset (off_t)(clazz == ELFCLASS32 \
  139. ? elf_getu32(swap, ph32.p_offset) \
  140. : elf_getu64(swap, ph64.p_offset))
  141. #define xph_align (size_t)((clazz == ELFCLASS32 \
  142. ? (off_t) (ph32.p_align ? \
  143. elf_getu32(swap, ph32.p_align) : 4) \
  144. : (off_t) (ph64.p_align ? \
  145. elf_getu64(swap, ph64.p_align) : 4)))
  146. #define xph_filesz (size_t)((clazz == ELFCLASS32 \
  147. ? elf_getu32(swap, ph32.p_filesz) \
  148. : elf_getu64(swap, ph64.p_filesz)))
  149. #define xnh_addr (clazz == ELFCLASS32 \
  150. ? (void *)&nh32 \
  151. : (void *)&nh64)
  152. #define xph_memsz (size_t)((clazz == ELFCLASS32 \
  153. ? elf_getu32(swap, ph32.p_memsz) \
  154. : elf_getu64(swap, ph64.p_memsz)))
  155. #define xnh_sizeof (clazz == ELFCLASS32 \
  156. ? sizeof nh32 \
  157. : sizeof nh64)
  158. #define xnh_type (clazz == ELFCLASS32 \
  159. ? elf_getu32(swap, nh32.n_type) \
  160. : elf_getu32(swap, nh64.n_type))
  161. #define xnh_namesz (clazz == ELFCLASS32 \
  162. ? elf_getu32(swap, nh32.n_namesz) \
  163. : elf_getu32(swap, nh64.n_namesz))
  164. #define xnh_descsz (clazz == ELFCLASS32 \
  165. ? elf_getu32(swap, nh32.n_descsz) \
  166. : elf_getu32(swap, nh64.n_descsz))
  167. #define prpsoffsets(i) (clazz == ELFCLASS32 \
  168. ? prpsoffsets32[i] \
  169. : prpsoffsets64[i])
  170. #define xcap_addr (clazz == ELFCLASS32 \
  171. ? (void *)&cap32 \
  172. : (void *)&cap64)
  173. #define xcap_sizeof (clazz == ELFCLASS32 \
  174. ? sizeof cap32 \
  175. : sizeof cap64)
  176. #define xcap_tag (clazz == ELFCLASS32 \
  177. ? elf_getu32(swap, cap32.c_tag) \
  178. : elf_getu64(swap, cap64.c_tag))
  179. #define xcap_val (clazz == ELFCLASS32 \
  180. ? elf_getu32(swap, cap32.c_un.c_val) \
  181. : elf_getu64(swap, cap64.c_un.c_val))
  182. #ifdef ELFCORE
  183. /*
  184. * Try larger offsets first to avoid false matches
  185. * from earlier data that happen to look like strings.
  186. */
  187. static const size_t prpsoffsets32[] = {
  188. #ifdef USE_NT_PSINFO
  189. 104, /* SunOS 5.x (command line) */
  190. 88, /* SunOS 5.x (short name) */
  191. #endif /* USE_NT_PSINFO */
  192. 100, /* SunOS 5.x (command line) */
  193. 84, /* SunOS 5.x (short name) */
  194. 44, /* Linux (command line) */
  195. 28, /* Linux 2.0.36 (short name) */
  196. 8, /* FreeBSD */
  197. };
  198. static const size_t prpsoffsets64[] = {
  199. #ifdef USE_NT_PSINFO
  200. 152, /* SunOS 5.x (command line) */
  201. 136, /* SunOS 5.x (short name) */
  202. #endif /* USE_NT_PSINFO */
  203. 136, /* SunOS 5.x, 64-bit (command line) */
  204. 120, /* SunOS 5.x, 64-bit (short name) */
  205. 56, /* Linux (command line) */
  206. 40, /* Linux (tested on core from 2.4.x, short name) */
  207. 16, /* FreeBSD, 64-bit */
  208. };
  209. #define NOFFSETS32 (sizeof prpsoffsets32 / sizeof prpsoffsets32[0])
  210. #define NOFFSETS64 (sizeof prpsoffsets64 / sizeof prpsoffsets64[0])
  211. #define NOFFSETS (clazz == ELFCLASS32 ? NOFFSETS32 : NOFFSETS64)
  212. /*
  213. * Look through the program headers of an executable image, searching
  214. * for a PT_NOTE section of type NT_PRPSINFO, with a name "CORE" or
  215. * "FreeBSD"; if one is found, try looking in various places in its
  216. * contents for a 16-character string containing only printable
  217. * characters - if found, that string should be the name of the program
  218. * that dropped core. Note: right after that 16-character string is,
  219. * at least in SunOS 5.x (and possibly other SVR4-flavored systems) and
  220. * Linux, a longer string (80 characters, in 5.x, probably other
  221. * SVR4-flavored systems, and Linux) containing the start of the
  222. * command line for that program.
  223. *
  224. * SunOS 5.x core files contain two PT_NOTE sections, with the types
  225. * NT_PRPSINFO (old) and NT_PSINFO (new). These structs contain the
  226. * same info about the command name and command line, so it probably
  227. * isn't worthwhile to look for NT_PSINFO, but the offsets are provided
  228. * above (see USE_NT_PSINFO), in case we ever decide to do so. The
  229. * NT_PRPSINFO and NT_PSINFO sections are always in order and adjacent;
  230. * the SunOS 5.x file command relies on this (and prefers the latter).
  231. *
  232. * The signal number probably appears in a section of type NT_PRSTATUS,
  233. * but that's also rather OS-dependent, in ways that are harder to
  234. * dissect with heuristics, so I'm not bothering with the signal number.
  235. * (I suppose the signal number could be of interest in situations where
  236. * you don't have the binary of the program that dropped core; if you
  237. * *do* have that binary, the debugger will probably tell you what
  238. * signal it was.)
  239. */
  240. #define OS_STYLE_SVR4 0
  241. #define OS_STYLE_FREEBSD 1
  242. #define OS_STYLE_NETBSD 2
  243. private const char os_style_names[][8] = {
  244. "SVR4",
  245. "FreeBSD",
  246. "NetBSD",
  247. };
  248. #define FLAGS_DID_CORE 0x01
  249. #define FLAGS_DID_NOTE 0x02
  250. #define FLAGS_DID_BUILD_ID 0x04
  251. #define FLAGS_DID_CORE_STYLE 0x08
  252. #define FLAGS_IS_CORE 0x10
  253. private int
  254. dophn_core(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
  255. int num, size_t size, off_t fsize, int *flags)
  256. {
  257. Elf32_Phdr ph32;
  258. Elf64_Phdr ph64;
  259. size_t offset, len;
  260. unsigned char nbuf[BUFSIZ];
  261. ssize_t bufsize;
  262. if (size != xph_sizeof) {
  263. if (file_printf(ms, ", corrupted program header size") == -1)
  264. return -1;
  265. return 0;
  266. }
  267. /*
  268. * Loop through all the program headers.
  269. */
  270. for ( ; num; num--) {
  271. if (pread(fd, xph_addr, xph_sizeof, off) == -1) {
  272. file_badread(ms);
  273. return -1;
  274. }
  275. off += size;
  276. if (xph_offset > fsize) {
  277. /* Perhaps warn here */
  278. continue;
  279. }
  280. if (xph_type != PT_NOTE)
  281. continue;
  282. /*
  283. * This is a PT_NOTE section; loop through all the notes
  284. * in the section.
  285. */
  286. len = xph_filesz < sizeof(nbuf) ? xph_filesz : sizeof(nbuf);
  287. if ((bufsize = pread(fd, nbuf, len, xph_offset)) == -1) {
  288. file_badread(ms);
  289. return -1;
  290. }
  291. offset = 0;
  292. for (;;) {
  293. if (offset >= (size_t)bufsize)
  294. break;
  295. offset = donote(ms, nbuf, offset, (size_t)bufsize,
  296. clazz, swap, 4, flags);
  297. if (offset == 0)
  298. break;
  299. }
  300. }
  301. return 0;
  302. }
  303. #endif
  304. private size_t
  305. donote(struct magic_set *ms, void *vbuf, size_t offset, size_t size,
  306. int clazz, int swap, size_t align, int *flags)
  307. {
  308. Elf32_Nhdr nh32;
  309. Elf64_Nhdr nh64;
  310. size_t noff, doff;
  311. #ifdef ELFCORE
  312. int os_style = -1;
  313. #endif
  314. uint32_t namesz, descsz;
  315. unsigned char *nbuf = CAST(unsigned char *, vbuf);
  316. (void)memcpy(xnh_addr, &nbuf[offset], xnh_sizeof);
  317. offset += xnh_sizeof;
  318. namesz = xnh_namesz;
  319. descsz = xnh_descsz;
  320. if ((namesz == 0) && (descsz == 0)) {
  321. /*
  322. * We're out of note headers.
  323. */
  324. return (offset >= size) ? offset : size;
  325. }
  326. if (namesz & 0x80000000) {
  327. (void)file_printf(ms, ", bad note name size 0x%lx",
  328. (unsigned long)namesz);
  329. return offset;
  330. }
  331. if (descsz & 0x80000000) {
  332. (void)file_printf(ms, ", bad note description size 0x%lx",
  333. (unsigned long)descsz);
  334. return offset;
  335. }
  336. noff = offset;
  337. doff = ELF_ALIGN(offset + namesz);
  338. if (offset + namesz > size) {
  339. /*
  340. * We're past the end of the buffer.
  341. */
  342. return doff;
  343. }
  344. offset = ELF_ALIGN(doff + descsz);
  345. if (doff + descsz > size) {
  346. /*
  347. * We're past the end of the buffer.
  348. */
  349. return (offset >= size) ? offset : size;
  350. }
  351. if ((*flags & (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID)) ==
  352. (FLAGS_DID_NOTE|FLAGS_DID_BUILD_ID))
  353. goto core;
  354. if (namesz == 5 && strcmp((char *)&nbuf[noff], "SuSE") == 0 &&
  355. xnh_type == NT_GNU_VERSION && descsz == 2) {
  356. file_printf(ms, ", for SuSE %d.%d", nbuf[doff], nbuf[doff + 1]);
  357. }
  358. if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
  359. xnh_type == NT_GNU_VERSION && descsz == 16) {
  360. uint32_t desc[4];
  361. (void)memcpy(desc, &nbuf[doff], sizeof(desc));
  362. if (file_printf(ms, ", for GNU/") == -1)
  363. return size;
  364. switch (elf_getu32(swap, desc[0])) {
  365. case GNU_OS_LINUX:
  366. if (file_printf(ms, "Linux") == -1)
  367. return size;
  368. break;
  369. case GNU_OS_HURD:
  370. if (file_printf(ms, "Hurd") == -1)
  371. return size;
  372. break;
  373. case GNU_OS_SOLARIS:
  374. if (file_printf(ms, "Solaris") == -1)
  375. return size;
  376. break;
  377. case GNU_OS_KFREEBSD:
  378. if (file_printf(ms, "kFreeBSD") == -1)
  379. return size;
  380. break;
  381. case GNU_OS_KNETBSD:
  382. if (file_printf(ms, "kNetBSD") == -1)
  383. return size;
  384. break;
  385. default:
  386. if (file_printf(ms, "<unknown>") == -1)
  387. return size;
  388. }
  389. if (file_printf(ms, " %d.%d.%d", elf_getu32(swap, desc[1]),
  390. elf_getu32(swap, desc[2]), elf_getu32(swap, desc[3])) == -1)
  391. return size;
  392. *flags |= FLAGS_DID_NOTE;
  393. return size;
  394. }
  395. if (namesz == 4 && strcmp((char *)&nbuf[noff], "GNU") == 0 &&
  396. xnh_type == NT_GNU_BUILD_ID && (descsz == 16 || descsz == 20)) {
  397. uint8_t desc[20];
  398. uint32_t i;
  399. if (file_printf(ms, ", BuildID[%s]=", descsz == 16 ? "md5/uuid" :
  400. "sha1") == -1)
  401. return size;
  402. (void)memcpy(desc, &nbuf[doff], descsz);
  403. for (i = 0; i < descsz; i++)
  404. if (file_printf(ms, "%02x", desc[i]) == -1)
  405. return size;
  406. *flags |= FLAGS_DID_BUILD_ID;
  407. }
  408. if (namesz == 7 && strcmp((char *)&nbuf[noff], "NetBSD") == 0 &&
  409. xnh_type == NT_NETBSD_VERSION && descsz == 4) {
  410. uint32_t desc;
  411. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  412. desc = elf_getu32(swap, desc);
  413. if (file_printf(ms, ", for NetBSD") == -1)
  414. return size;
  415. /*
  416. * The version number used to be stuck as 199905, and was thus
  417. * basically content-free. Newer versions of NetBSD have fixed
  418. * this and now use the encoding of __NetBSD_Version__:
  419. *
  420. * MMmmrrpp00
  421. *
  422. * M = major version
  423. * m = minor version
  424. * r = release ["",A-Z,Z[A-Z] but numeric]
  425. * p = patchlevel
  426. */
  427. if (desc > 100000000U) {
  428. uint32_t ver_patch = (desc / 100) % 100;
  429. uint32_t ver_rel = (desc / 10000) % 100;
  430. uint32_t ver_min = (desc / 1000000) % 100;
  431. uint32_t ver_maj = desc / 100000000;
  432. if (file_printf(ms, " %u.%u", ver_maj, ver_min) == -1)
  433. return size;
  434. if (ver_rel == 0 && ver_patch != 0) {
  435. if (file_printf(ms, ".%u", ver_patch) == -1)
  436. return size;
  437. } else if (ver_rel != 0) {
  438. while (ver_rel > 26) {
  439. if (file_printf(ms, "Z") == -1)
  440. return size;
  441. ver_rel -= 26;
  442. }
  443. if (file_printf(ms, "%c", 'A' + ver_rel - 1)
  444. == -1)
  445. return size;
  446. }
  447. }
  448. *flags |= FLAGS_DID_NOTE;
  449. return size;
  450. }
  451. if (namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0 &&
  452. xnh_type == NT_FREEBSD_VERSION && descsz == 4) {
  453. uint32_t desc;
  454. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  455. desc = elf_getu32(swap, desc);
  456. if (file_printf(ms, ", for FreeBSD") == -1)
  457. return size;
  458. /*
  459. * Contents is __FreeBSD_version, whose relation to OS
  460. * versions is defined by a huge table in the Porter's
  461. * Handbook. This is the general scheme:
  462. *
  463. * Releases:
  464. * Mmp000 (before 4.10)
  465. * Mmi0p0 (before 5.0)
  466. * Mmm0p0
  467. *
  468. * Development branches:
  469. * Mmpxxx (before 4.6)
  470. * Mmp1xx (before 4.10)
  471. * Mmi1xx (before 5.0)
  472. * M000xx (pre-M.0)
  473. * Mmm1xx
  474. *
  475. * M = major version
  476. * m = minor version
  477. * i = minor version increment (491000 -> 4.10)
  478. * p = patchlevel
  479. * x = revision
  480. *
  481. * The first release of FreeBSD to use ELF by default
  482. * was version 3.0.
  483. */
  484. if (desc == 460002) {
  485. if (file_printf(ms, " 4.6.2") == -1)
  486. return size;
  487. } else if (desc < 460100) {
  488. if (file_printf(ms, " %d.%d", desc / 100000,
  489. desc / 10000 % 10) == -1)
  490. return size;
  491. if (desc / 1000 % 10 > 0)
  492. if (file_printf(ms, ".%d", desc / 1000 % 10)
  493. == -1)
  494. return size;
  495. if ((desc % 1000 > 0) || (desc % 100000 == 0))
  496. if (file_printf(ms, " (%d)", desc) == -1)
  497. return size;
  498. } else if (desc < 500000) {
  499. if (file_printf(ms, " %d.%d", desc / 100000,
  500. desc / 10000 % 10 + desc / 1000 % 10) == -1)
  501. return size;
  502. if (desc / 100 % 10 > 0) {
  503. if (file_printf(ms, " (%d)", desc) == -1)
  504. return size;
  505. } else if (desc / 10 % 10 > 0) {
  506. if (file_printf(ms, ".%d", desc / 10 % 10)
  507. == -1)
  508. return size;
  509. }
  510. } else {
  511. if (file_printf(ms, " %d.%d", desc / 100000,
  512. desc / 1000 % 100) == -1)
  513. return size;
  514. if ((desc / 100 % 10 > 0) ||
  515. (desc % 100000 / 100 == 0)) {
  516. if (file_printf(ms, " (%d)", desc) == -1)
  517. return size;
  518. } else if (desc / 10 % 10 > 0) {
  519. if (file_printf(ms, ".%d", desc / 10 % 10)
  520. == -1)
  521. return size;
  522. }
  523. }
  524. *flags |= FLAGS_DID_NOTE;
  525. return size;
  526. }
  527. if (namesz == 8 && strcmp((char *)&nbuf[noff], "OpenBSD") == 0 &&
  528. xnh_type == NT_OPENBSD_VERSION && descsz == 4) {
  529. if (file_printf(ms, ", for OpenBSD") == -1)
  530. return size;
  531. /* Content of note is always 0 */
  532. *flags |= FLAGS_DID_NOTE;
  533. return size;
  534. }
  535. if (namesz == 10 && strcmp((char *)&nbuf[noff], "DragonFly") == 0 &&
  536. xnh_type == NT_DRAGONFLY_VERSION && descsz == 4) {
  537. uint32_t desc;
  538. if (file_printf(ms, ", for DragonFly") == -1)
  539. return size;
  540. (void)memcpy(&desc, &nbuf[doff], sizeof(desc));
  541. desc = elf_getu32(swap, desc);
  542. if (file_printf(ms, " %d.%d.%d", desc / 100000,
  543. desc / 10000 % 10, desc % 10000) == -1)
  544. return size;
  545. *flags |= FLAGS_DID_NOTE;
  546. return size;
  547. }
  548. core:
  549. /*
  550. * Sigh. The 2.0.36 kernel in Debian 2.1, at
  551. * least, doesn't correctly implement name
  552. * sections, in core dumps, as specified by
  553. * the "Program Linking" section of "UNIX(R) System
  554. * V Release 4 Programmer's Guide: ANSI C and
  555. * Programming Support Tools", because my copy
  556. * clearly says "The first 'namesz' bytes in 'name'
  557. * contain a *null-terminated* [emphasis mine]
  558. * character representation of the entry's owner
  559. * or originator", but the 2.0.36 kernel code
  560. * doesn't include the terminating null in the
  561. * name....
  562. */
  563. if ((namesz == 4 && strncmp((char *)&nbuf[noff], "CORE", 4) == 0) ||
  564. (namesz == 5 && strcmp((char *)&nbuf[noff], "CORE") == 0)) {
  565. os_style = OS_STYLE_SVR4;
  566. }
  567. if ((namesz == 8 && strcmp((char *)&nbuf[noff], "FreeBSD") == 0)) {
  568. os_style = OS_STYLE_FREEBSD;
  569. }
  570. if ((namesz >= 11 && strncmp((char *)&nbuf[noff], "NetBSD-CORE", 11)
  571. == 0)) {
  572. os_style = OS_STYLE_NETBSD;
  573. }
  574. #ifdef ELFCORE
  575. if ((*flags & FLAGS_DID_CORE) != 0)
  576. return size;
  577. if (os_style != -1 && (*flags & FLAGS_DID_CORE_STYLE) == 0) {
  578. if (file_printf(ms, ", %s-style", os_style_names[os_style])
  579. == -1)
  580. return size;
  581. *flags |= FLAGS_DID_CORE_STYLE;
  582. }
  583. switch (os_style) {
  584. case OS_STYLE_NETBSD:
  585. if (xnh_type == NT_NETBSD_CORE_PROCINFO) {
  586. uint32_t signo;
  587. /*
  588. * Extract the program name. It is at
  589. * offset 0x7c, and is up to 32-bytes,
  590. * including the terminating NUL.
  591. */
  592. if (file_printf(ms, ", from '%.31s'",
  593. &nbuf[doff + 0x7c]) == -1)
  594. return size;
  595. /*
  596. * Extract the signal number. It is at
  597. * offset 0x08.
  598. */
  599. (void)memcpy(&signo, &nbuf[doff + 0x08],
  600. sizeof(signo));
  601. if (file_printf(ms, " (signal %u)",
  602. elf_getu32(swap, signo)) == -1)
  603. return size;
  604. *flags |= FLAGS_DID_CORE;
  605. return size;
  606. }
  607. break;
  608. default:
  609. if (xnh_type == NT_PRPSINFO && *flags & FLAGS_IS_CORE) {
  610. size_t i, j;
  611. unsigned char c;
  612. /*
  613. * Extract the program name. We assume
  614. * it to be 16 characters (that's what it
  615. * is in SunOS 5.x and Linux).
  616. *
  617. * Unfortunately, it's at a different offset
  618. * in various OSes, so try multiple offsets.
  619. * If the characters aren't all printable,
  620. * reject it.
  621. */
  622. for (i = 0; i < NOFFSETS; i++) {
  623. unsigned char *cname, *cp;
  624. size_t reloffset = prpsoffsets(i);
  625. size_t noffset = doff + reloffset;
  626. size_t k;
  627. for (j = 0; j < 16; j++, noffset++,
  628. reloffset++) {
  629. /*
  630. * Make sure we're not past
  631. * the end of the buffer; if
  632. * we are, just give up.
  633. */
  634. if (noffset >= size)
  635. goto tryanother;
  636. /*
  637. * Make sure we're not past
  638. * the end of the contents;
  639. * if we are, this obviously
  640. * isn't the right offset.
  641. */
  642. if (reloffset >= descsz)
  643. goto tryanother;
  644. c = nbuf[noffset];
  645. if (c == '\0') {
  646. /*
  647. * A '\0' at the
  648. * beginning is
  649. * obviously wrong.
  650. * Any other '\0'
  651. * means we're done.
  652. */
  653. if (j == 0)
  654. goto tryanother;
  655. else
  656. break;
  657. } else {
  658. /*
  659. * A nonprintable
  660. * character is also
  661. * wrong.
  662. */
  663. if (!isprint(c) || isquote(c))
  664. goto tryanother;
  665. }
  666. }
  667. /*
  668. * Well, that worked.
  669. */
  670. /*
  671. * Try next offsets, in case this match is
  672. * in the middle of a string.
  673. */
  674. for (k = i + 1 ; k < NOFFSETS ; k++) {
  675. size_t no;
  676. int adjust = 1;
  677. if (prpsoffsets(k) >= prpsoffsets(i))
  678. continue;
  679. for (no = doff + prpsoffsets(k);
  680. no < doff + prpsoffsets(i); no++)
  681. adjust = adjust
  682. && isprint(nbuf[no]);
  683. if (adjust)
  684. i = k;
  685. }
  686. cname = (unsigned char *)
  687. &nbuf[doff + prpsoffsets(i)];
  688. for (cp = cname; *cp && isprint(*cp); cp++)
  689. continue;
  690. /*
  691. * Linux apparently appends a space at the end
  692. * of the command line: remove it.
  693. */
  694. while (cp > cname && isspace(cp[-1]))
  695. cp--;
  696. if (file_printf(ms, ", from '%.*s'",
  697. (int)(cp - cname), cname) == -1)
  698. return size;
  699. *flags |= FLAGS_DID_CORE;
  700. return size;
  701. tryanother:
  702. ;
  703. }
  704. }
  705. break;
  706. }
  707. #endif
  708. return offset;
  709. }
  710. /* SunOS 5.x hardware capability descriptions */
  711. typedef struct cap_desc {
  712. uint64_t cd_mask;
  713. const char *cd_name;
  714. } cap_desc_t;
  715. static const cap_desc_t cap_desc_sparc[] = {
  716. { AV_SPARC_MUL32, "MUL32" },
  717. { AV_SPARC_DIV32, "DIV32" },
  718. { AV_SPARC_FSMULD, "FSMULD" },
  719. { AV_SPARC_V8PLUS, "V8PLUS" },
  720. { AV_SPARC_POPC, "POPC" },
  721. { AV_SPARC_VIS, "VIS" },
  722. { AV_SPARC_VIS2, "VIS2" },
  723. { AV_SPARC_ASI_BLK_INIT, "ASI_BLK_INIT" },
  724. { AV_SPARC_FMAF, "FMAF" },
  725. { AV_SPARC_FJFMAU, "FJFMAU" },
  726. { AV_SPARC_IMA, "IMA" },
  727. { 0, NULL }
  728. };
  729. static const cap_desc_t cap_desc_386[] = {
  730. { AV_386_FPU, "FPU" },
  731. { AV_386_TSC, "TSC" },
  732. { AV_386_CX8, "CX8" },
  733. { AV_386_SEP, "SEP" },
  734. { AV_386_AMD_SYSC, "AMD_SYSC" },
  735. { AV_386_CMOV, "CMOV" },
  736. { AV_386_MMX, "MMX" },
  737. { AV_386_AMD_MMX, "AMD_MMX" },
  738. { AV_386_AMD_3DNow, "AMD_3DNow" },
  739. { AV_386_AMD_3DNowx, "AMD_3DNowx" },
  740. { AV_386_FXSR, "FXSR" },
  741. { AV_386_SSE, "SSE" },
  742. { AV_386_SSE2, "SSE2" },
  743. { AV_386_PAUSE, "PAUSE" },
  744. { AV_386_SSE3, "SSE3" },
  745. { AV_386_MON, "MON" },
  746. { AV_386_CX16, "CX16" },
  747. { AV_386_AHF, "AHF" },
  748. { AV_386_TSCP, "TSCP" },
  749. { AV_386_AMD_SSE4A, "AMD_SSE4A" },
  750. { AV_386_POPCNT, "POPCNT" },
  751. { AV_386_AMD_LZCNT, "AMD_LZCNT" },
  752. { AV_386_SSSE3, "SSSE3" },
  753. { AV_386_SSE4_1, "SSE4.1" },
  754. { AV_386_SSE4_2, "SSE4.2" },
  755. { 0, NULL }
  756. };
  757. private int
  758. doshn(struct magic_set *ms, int clazz, int swap, int fd, off_t off, int num,
  759. size_t size, off_t fsize, int *flags, int mach, int strtab)
  760. {
  761. Elf32_Shdr sh32;
  762. Elf64_Shdr sh64;
  763. int stripped = 1;
  764. void *nbuf;
  765. off_t noff, coff, name_off;
  766. uint64_t cap_hw1 = 0; /* SunOS 5.x hardware capabilites */
  767. uint64_t cap_sf1 = 0; /* SunOS 5.x software capabilites */
  768. char name[50];
  769. if (size != xsh_sizeof) {
  770. if (file_printf(ms, ", corrupted section header size") == -1)
  771. return -1;
  772. return 0;
  773. }
  774. /* Read offset of name section to be able to read section names later */
  775. if (pread(fd, xsh_addr, xsh_sizeof, off + size * strtab) == -1) {
  776. file_badread(ms);
  777. return -1;
  778. }
  779. name_off = xsh_offset;
  780. for ( ; num; num--) {
  781. /* Read the name of this section. */
  782. if (pread(fd, name, sizeof(name), name_off + xsh_name) == -1) {
  783. file_badread(ms);
  784. return -1;
  785. }
  786. name[sizeof(name) - 1] = '\0';
  787. if (strcmp(name, ".debug_info") == 0)
  788. stripped = 0;
  789. if (pread(fd, xsh_addr, xsh_sizeof, off) == -1) {
  790. file_badread(ms);
  791. return -1;
  792. }
  793. off += size;
  794. /* Things we can determine before we seek */
  795. switch (xsh_type) {
  796. case SHT_SYMTAB:
  797. #if 0
  798. case SHT_DYNSYM:
  799. #endif
  800. stripped = 0;
  801. break;
  802. default:
  803. if (xsh_offset > fsize) {
  804. /* Perhaps warn here */
  805. continue;
  806. }
  807. break;
  808. }
  809. /* Things we can determine when we seek */
  810. switch (xsh_type) {
  811. case SHT_NOTE:
  812. if ((nbuf = malloc(xsh_size)) == NULL) {
  813. file_error(ms, errno, "Cannot allocate memory"
  814. " for note");
  815. return -1;
  816. }
  817. if (pread(fd, nbuf, xsh_size, xsh_offset) == -1) {
  818. file_badread(ms);
  819. free(nbuf);
  820. return -1;
  821. }
  822. noff = 0;
  823. for (;;) {
  824. if (noff >= (off_t)xsh_size)
  825. break;
  826. noff = donote(ms, nbuf, (size_t)noff,
  827. xsh_size, clazz, swap, 4, flags);
  828. if (noff == 0)
  829. break;
  830. }
  831. free(nbuf);
  832. break;
  833. case SHT_SUNW_cap:
  834. switch (mach) {
  835. case EM_SPARC:
  836. case EM_SPARCV9:
  837. case EM_IA_64:
  838. case EM_386:
  839. case EM_AMD64:
  840. break;
  841. default:
  842. goto skip;
  843. }
  844. if (lseek(fd, xsh_offset, SEEK_SET) == (off_t)-1) {
  845. file_badseek(ms);
  846. return -1;
  847. }
  848. coff = 0;
  849. for (;;) {
  850. Elf32_Cap cap32;
  851. Elf64_Cap cap64;
  852. char cbuf[/*CONSTCOND*/
  853. MAX(sizeof cap32, sizeof cap64)];
  854. if ((coff += xcap_sizeof) > (off_t)xsh_size)
  855. break;
  856. if (read(fd, cbuf, (size_t)xcap_sizeof) !=
  857. (ssize_t)xcap_sizeof) {
  858. file_badread(ms);
  859. return -1;
  860. }
  861. (void)memcpy(xcap_addr, cbuf, xcap_sizeof);
  862. switch (xcap_tag) {
  863. case CA_SUNW_NULL:
  864. break;
  865. case CA_SUNW_HW_1:
  866. cap_hw1 |= xcap_val;
  867. break;
  868. case CA_SUNW_SF_1:
  869. cap_sf1 |= xcap_val;
  870. break;
  871. default:
  872. if (file_printf(ms,
  873. ", with unknown capability "
  874. "0x%" INT64_T_FORMAT "x = 0x%"
  875. INT64_T_FORMAT "x",
  876. (unsigned long long)xcap_tag,
  877. (unsigned long long)xcap_val) == -1)
  878. return -1;
  879. break;
  880. }
  881. }
  882. /*FALLTHROUGH*/
  883. skip:
  884. default:
  885. break;
  886. }
  887. }
  888. if (file_printf(ms, ", %sstripped", stripped ? "" : "not ") == -1)
  889. return -1;
  890. if (cap_hw1) {
  891. const cap_desc_t *cdp;
  892. switch (mach) {
  893. case EM_SPARC:
  894. case EM_SPARC32PLUS:
  895. case EM_SPARCV9:
  896. cdp = cap_desc_sparc;
  897. break;
  898. case EM_386:
  899. case EM_IA_64:
  900. case EM_AMD64:
  901. cdp = cap_desc_386;
  902. break;
  903. default:
  904. cdp = NULL;
  905. break;
  906. }
  907. if (file_printf(ms, ", uses") == -1)
  908. return -1;
  909. if (cdp) {
  910. while (cdp->cd_name) {
  911. if (cap_hw1 & cdp->cd_mask) {
  912. if (file_printf(ms,
  913. " %s", cdp->cd_name) == -1)
  914. return -1;
  915. cap_hw1 &= ~cdp->cd_mask;
  916. }
  917. ++cdp;
  918. }
  919. if (cap_hw1)
  920. if (file_printf(ms,
  921. " unknown hardware capability 0x%"
  922. INT64_T_FORMAT "x",
  923. (unsigned long long)cap_hw1) == -1)
  924. return -1;
  925. } else {
  926. if (file_printf(ms,
  927. " hardware capability 0x%" INT64_T_FORMAT "x",
  928. (unsigned long long)cap_hw1) == -1)
  929. return -1;
  930. }
  931. }
  932. if (cap_sf1) {
  933. if (cap_sf1 & SF1_SUNW_FPUSED) {
  934. if (file_printf(ms,
  935. (cap_sf1 & SF1_SUNW_FPKNWN)
  936. ? ", uses frame pointer"
  937. : ", not known to use frame pointer") == -1)
  938. return -1;
  939. }
  940. cap_sf1 &= ~SF1_SUNW_MASK;
  941. if (cap_sf1)
  942. if (file_printf(ms,
  943. ", with unknown software capability 0x%"
  944. INT64_T_FORMAT "x",
  945. (unsigned long long)cap_sf1) == -1)
  946. return -1;
  947. }
  948. return 0;
  949. }
  950. /*
  951. * Look through the program headers of an executable image, searching
  952. * for a PT_INTERP section; if one is found, it's dynamically linked,
  953. * otherwise it's statically linked.
  954. */
  955. private int
  956. dophn_exec(struct magic_set *ms, int clazz, int swap, int fd, off_t off,
  957. int num, size_t size, off_t fsize, int *flags, int sh_num)
  958. {
  959. Elf32_Phdr ph32;
  960. Elf64_Phdr ph64;
  961. const char *linking_style = "statically";
  962. const char *shared_libraries = "";
  963. unsigned char nbuf[BUFSIZ];
  964. ssize_t bufsize;
  965. size_t offset, align, len;
  966. if (size != xph_sizeof) {
  967. if (file_printf(ms, ", corrupted program header size") == -1)
  968. return -1;
  969. return 0;
  970. }
  971. for ( ; num; num--) {
  972. if (pread(fd, xph_addr, xph_sizeof, off) == -1) {
  973. file_badread(ms);
  974. return -1;
  975. }
  976. off += size;
  977. /* Things we can determine before we seek */
  978. switch (xph_type) {
  979. case PT_DYNAMIC:
  980. linking_style = "dynamically";
  981. break;
  982. case PT_INTERP:
  983. shared_libraries = " (uses shared libs)";
  984. break;
  985. default:
  986. if (xph_offset > fsize) {
  987. /* Maybe warn here? */
  988. continue;
  989. }
  990. break;
  991. }
  992. /* Things we can determine when we seek */
  993. switch (xph_type) {
  994. case PT_NOTE:
  995. if ((align = xph_align) & 0x80000000UL) {
  996. if (file_printf(ms,
  997. ", invalid note alignment 0x%lx",
  998. (unsigned long)align) == -1)
  999. return -1;
  1000. align = 4;
  1001. }
  1002. if (sh_num)
  1003. break;
  1004. /*
  1005. * This is a PT_NOTE section; loop through all the notes
  1006. * in the section.
  1007. */
  1008. len = xph_filesz < sizeof(nbuf) ? xph_filesz
  1009. : sizeof(nbuf);
  1010. bufsize = pread(fd, nbuf, len, xph_offset);
  1011. if (bufsize == -1) {
  1012. file_badread(ms);
  1013. return -1;
  1014. }
  1015. offset = 0;
  1016. for (;;) {
  1017. if (offset >= (size_t)bufsize)
  1018. break;
  1019. offset = donote(ms, nbuf, offset,
  1020. (size_t)bufsize, clazz, swap, align,
  1021. flags);
  1022. if (offset == 0)
  1023. break;
  1024. }
  1025. break;
  1026. default:
  1027. break;
  1028. }
  1029. }
  1030. if (file_printf(ms, ", %s linked%s", linking_style, shared_libraries)
  1031. == -1)
  1032. return -1;
  1033. return 0;
  1034. }
  1035. protected int
  1036. file_tryelf(struct magic_set *ms, int fd, const unsigned char *buf,
  1037. size_t nbytes)
  1038. {
  1039. union {
  1040. int32_t l;
  1041. char c[sizeof (int32_t)];
  1042. } u;
  1043. int clazz;
  1044. int swap;
  1045. struct stat st;
  1046. off_t fsize;
  1047. int flags = 0;
  1048. Elf32_Ehdr elf32hdr;
  1049. Elf64_Ehdr elf64hdr;
  1050. uint16_t type;
  1051. if (ms->flags & (MAGIC_MIME|MAGIC_APPLE))
  1052. return 0;
  1053. /*
  1054. * ELF executables have multiple section headers in arbitrary
  1055. * file locations and thus file(1) cannot determine it from easily.
  1056. * Instead we traverse thru all section headers until a symbol table
  1057. * one is found or else the binary is stripped.
  1058. * Return immediately if it's not ELF (so we avoid pipe2file unless needed).
  1059. */
  1060. if (buf[EI_MAG0] != ELFMAG0
  1061. || (buf[EI_MAG1] != ELFMAG1 && buf[EI_MAG1] != OLFMAG1)
  1062. || buf[EI_MAG2] != ELFMAG2 || buf[EI_MAG3] != ELFMAG3)
  1063. return 0;
  1064. /*
  1065. * If we cannot seek, it must be a pipe, socket or fifo.
  1066. */
  1067. if((lseek(fd, (off_t)0, SEEK_SET) == (off_t)-1) && (errno == ESPIPE))
  1068. fd = file_pipe2file(ms, fd, buf, nbytes);
  1069. if (fstat(fd, &st) == -1) {
  1070. file_badread(ms);
  1071. return -1;
  1072. }
  1073. fsize = st.st_size;
  1074. clazz = buf[EI_CLASS];
  1075. switch (clazz) {
  1076. case ELFCLASS32:
  1077. #undef elf_getu
  1078. #define elf_getu(a, b) elf_getu32(a, b)
  1079. #undef elfhdr
  1080. #define elfhdr elf32hdr
  1081. #include "elfclass.h"
  1082. case ELFCLASS64:
  1083. #undef elf_getu
  1084. #define elf_getu(a, b) elf_getu64(a, b)
  1085. #undef elfhdr
  1086. #define elfhdr elf64hdr
  1087. #include "elfclass.h"
  1088. default:
  1089. if (file_printf(ms, ", unknown class %d", clazz) == -1)
  1090. return -1;
  1091. break;
  1092. }
  1093. return 0;
  1094. }
  1095. #endif