readelf.c 30 KB

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