elfcomm.c 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911
  1. /* elfcomm.c -- common code for ELF format file.
  2. Copyright (C) 2010-2015 Free Software Foundation, Inc.
  3. Originally developed by Eric Youngdale <eric@andante.jic.com>
  4. Modifications by Nick Clifton <nickc@redhat.com>
  5. This file is part of GNU Binutils.
  6. This program is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 3 of the License, or
  9. (at your option) any later version.
  10. This program is distributed in the hope that it will be useful,
  11. but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. GNU General Public License for more details.
  14. You should have received a copy of the GNU General Public License
  15. along with this program; if not, write to the Free Software
  16. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  17. 02110-1301, USA. */
  18. #include "sysdep.h"
  19. #include "libiberty.h"
  20. #include "filenames.h"
  21. #include "bfd.h"
  22. #include "aout/ar.h"
  23. #include "bucomm.h"
  24. #include "elfcomm.h"
  25. #include <assert.h>
  26. void
  27. error (const char *message, ...)
  28. {
  29. va_list args;
  30. /* Try to keep error messages in sync with the program's normal output. */
  31. fflush (stdout);
  32. va_start (args, message);
  33. fprintf (stderr, _("%s: Error: "), program_name);
  34. vfprintf (stderr, message, args);
  35. va_end (args);
  36. }
  37. void
  38. warn (const char *message, ...)
  39. {
  40. va_list args;
  41. /* Try to keep warning messages in sync with the program's normal output. */
  42. fflush (stdout);
  43. va_start (args, message);
  44. fprintf (stderr, _("%s: Warning: "), program_name);
  45. vfprintf (stderr, message, args);
  46. va_end (args);
  47. }
  48. void (*byte_put) (unsigned char *, elf_vma, int);
  49. void
  50. byte_put_little_endian (unsigned char * field, elf_vma value, int size)
  51. {
  52. switch (size)
  53. {
  54. case 8:
  55. field[7] = (((value >> 24) >> 24) >> 8) & 0xff;
  56. field[6] = ((value >> 24) >> 24) & 0xff;
  57. field[5] = ((value >> 24) >> 16) & 0xff;
  58. field[4] = ((value >> 24) >> 8) & 0xff;
  59. /* Fall through. */
  60. case 4:
  61. field[3] = (value >> 24) & 0xff;
  62. /* Fall through. */
  63. case 3:
  64. field[2] = (value >> 16) & 0xff;
  65. /* Fall through. */
  66. case 2:
  67. field[1] = (value >> 8) & 0xff;
  68. /* Fall through. */
  69. case 1:
  70. field[0] = value & 0xff;
  71. break;
  72. default:
  73. error (_("Unhandled data length: %d\n"), size);
  74. abort ();
  75. }
  76. }
  77. void
  78. byte_put_big_endian (unsigned char * field, elf_vma value, int size)
  79. {
  80. switch (size)
  81. {
  82. case 8:
  83. field[7] = value & 0xff;
  84. field[6] = (value >> 8) & 0xff;
  85. field[5] = (value >> 16) & 0xff;
  86. field[4] = (value >> 24) & 0xff;
  87. value >>= 16;
  88. value >>= 16;
  89. /* Fall through. */
  90. case 4:
  91. field[3] = value & 0xff;
  92. value >>= 8;
  93. /* Fall through. */
  94. case 3:
  95. field[2] = value & 0xff;
  96. value >>= 8;
  97. /* Fall through. */
  98. case 2:
  99. field[1] = value & 0xff;
  100. value >>= 8;
  101. /* Fall through. */
  102. case 1:
  103. field[0] = value & 0xff;
  104. break;
  105. default:
  106. error (_("Unhandled data length: %d\n"), size);
  107. abort ();
  108. }
  109. }
  110. elf_vma (*byte_get) (unsigned char *, int);
  111. elf_vma
  112. byte_get_little_endian (unsigned char *field, int size)
  113. {
  114. switch (size)
  115. {
  116. case 1:
  117. return *field;
  118. case 2:
  119. return ((unsigned int) (field[0]))
  120. | (((unsigned int) (field[1])) << 8);
  121. case 3:
  122. return ((unsigned long) (field[0]))
  123. | (((unsigned long) (field[1])) << 8)
  124. | (((unsigned long) (field[2])) << 16);
  125. case 4:
  126. return ((unsigned long) (field[0]))
  127. | (((unsigned long) (field[1])) << 8)
  128. | (((unsigned long) (field[2])) << 16)
  129. | (((unsigned long) (field[3])) << 24);
  130. case 5:
  131. if (sizeof (elf_vma) == 8)
  132. return ((elf_vma) (field[0]))
  133. | (((elf_vma) (field[1])) << 8)
  134. | (((elf_vma) (field[2])) << 16)
  135. | (((elf_vma) (field[3])) << 24)
  136. | (((elf_vma) (field[4])) << 32);
  137. else if (sizeof (elf_vma) == 4)
  138. /* We want to extract data from an 8 byte wide field and
  139. place it into a 4 byte wide field. Since this is a little
  140. endian source we can just use the 4 byte extraction code. */
  141. return ((unsigned long) (field[0]))
  142. | (((unsigned long) (field[1])) << 8)
  143. | (((unsigned long) (field[2])) << 16)
  144. | (((unsigned long) (field[3])) << 24);
  145. case 6:
  146. if (sizeof (elf_vma) == 8)
  147. return ((elf_vma) (field[0]))
  148. | (((elf_vma) (field[1])) << 8)
  149. | (((elf_vma) (field[2])) << 16)
  150. | (((elf_vma) (field[3])) << 24)
  151. | (((elf_vma) (field[4])) << 32)
  152. | (((elf_vma) (field[5])) << 40);
  153. else if (sizeof (elf_vma) == 4)
  154. /* We want to extract data from an 8 byte wide field and
  155. place it into a 4 byte wide field. Since this is a little
  156. endian source we can just use the 4 byte extraction code. */
  157. return ((unsigned long) (field[0]))
  158. | (((unsigned long) (field[1])) << 8)
  159. | (((unsigned long) (field[2])) << 16)
  160. | (((unsigned long) (field[3])) << 24);
  161. case 7:
  162. if (sizeof (elf_vma) == 8)
  163. return ((elf_vma) (field[0]))
  164. | (((elf_vma) (field[1])) << 8)
  165. | (((elf_vma) (field[2])) << 16)
  166. | (((elf_vma) (field[3])) << 24)
  167. | (((elf_vma) (field[4])) << 32)
  168. | (((elf_vma) (field[5])) << 40)
  169. | (((elf_vma) (field[6])) << 48);
  170. else if (sizeof (elf_vma) == 4)
  171. /* We want to extract data from an 8 byte wide field and
  172. place it into a 4 byte wide field. Since this is a little
  173. endian source we can just use the 4 byte extraction code. */
  174. return ((unsigned long) (field[0]))
  175. | (((unsigned long) (field[1])) << 8)
  176. | (((unsigned long) (field[2])) << 16)
  177. | (((unsigned long) (field[3])) << 24);
  178. case 8:
  179. if (sizeof (elf_vma) == 8)
  180. return ((elf_vma) (field[0]))
  181. | (((elf_vma) (field[1])) << 8)
  182. | (((elf_vma) (field[2])) << 16)
  183. | (((elf_vma) (field[3])) << 24)
  184. | (((elf_vma) (field[4])) << 32)
  185. | (((elf_vma) (field[5])) << 40)
  186. | (((elf_vma) (field[6])) << 48)
  187. | (((elf_vma) (field[7])) << 56);
  188. else if (sizeof (elf_vma) == 4)
  189. /* We want to extract data from an 8 byte wide field and
  190. place it into a 4 byte wide field. Since this is a little
  191. endian source we can just use the 4 byte extraction code. */
  192. return ((unsigned long) (field[0]))
  193. | (((unsigned long) (field[1])) << 8)
  194. | (((unsigned long) (field[2])) << 16)
  195. | (((unsigned long) (field[3])) << 24);
  196. default:
  197. error (_("Unhandled data length: %d\n"), size);
  198. abort ();
  199. }
  200. }
  201. elf_vma
  202. byte_get_big_endian (unsigned char *field, int size)
  203. {
  204. switch (size)
  205. {
  206. case 1:
  207. return *field;
  208. case 2:
  209. return ((unsigned int) (field[1])) | (((int) (field[0])) << 8);
  210. case 3:
  211. return ((unsigned long) (field[2]))
  212. | (((unsigned long) (field[1])) << 8)
  213. | (((unsigned long) (field[0])) << 16);
  214. case 4:
  215. return ((unsigned long) (field[3]))
  216. | (((unsigned long) (field[2])) << 8)
  217. | (((unsigned long) (field[1])) << 16)
  218. | (((unsigned long) (field[0])) << 24);
  219. case 5:
  220. if (sizeof (elf_vma) == 8)
  221. return ((elf_vma) (field[4]))
  222. | (((elf_vma) (field[3])) << 8)
  223. | (((elf_vma) (field[2])) << 16)
  224. | (((elf_vma) (field[1])) << 24)
  225. | (((elf_vma) (field[0])) << 32);
  226. else if (sizeof (elf_vma) == 4)
  227. {
  228. /* Although we are extracting data from an 8 byte wide field,
  229. we are returning only 4 bytes of data. */
  230. field += 1;
  231. return ((unsigned long) (field[3]))
  232. | (((unsigned long) (field[2])) << 8)
  233. | (((unsigned long) (field[1])) << 16)
  234. | (((unsigned long) (field[0])) << 24);
  235. }
  236. case 6:
  237. if (sizeof (elf_vma) == 8)
  238. return ((elf_vma) (field[5]))
  239. | (((elf_vma) (field[4])) << 8)
  240. | (((elf_vma) (field[3])) << 16)
  241. | (((elf_vma) (field[2])) << 24)
  242. | (((elf_vma) (field[1])) << 32)
  243. | (((elf_vma) (field[0])) << 40);
  244. else if (sizeof (elf_vma) == 4)
  245. {
  246. /* Although we are extracting data from an 8 byte wide field,
  247. we are returning only 4 bytes of data. */
  248. field += 2;
  249. return ((unsigned long) (field[3]))
  250. | (((unsigned long) (field[2])) << 8)
  251. | (((unsigned long) (field[1])) << 16)
  252. | (((unsigned long) (field[0])) << 24);
  253. }
  254. case 7:
  255. if (sizeof (elf_vma) == 8)
  256. return ((elf_vma) (field[6]))
  257. | (((elf_vma) (field[5])) << 8)
  258. | (((elf_vma) (field[4])) << 16)
  259. | (((elf_vma) (field[3])) << 24)
  260. | (((elf_vma) (field[2])) << 32)
  261. | (((elf_vma) (field[1])) << 40)
  262. | (((elf_vma) (field[0])) << 48);
  263. else if (sizeof (elf_vma) == 4)
  264. {
  265. /* Although we are extracting data from an 8 byte wide field,
  266. we are returning only 4 bytes of data. */
  267. field += 3;
  268. return ((unsigned long) (field[3]))
  269. | (((unsigned long) (field[2])) << 8)
  270. | (((unsigned long) (field[1])) << 16)
  271. | (((unsigned long) (field[0])) << 24);
  272. }
  273. case 8:
  274. if (sizeof (elf_vma) == 8)
  275. return ((elf_vma) (field[7]))
  276. | (((elf_vma) (field[6])) << 8)
  277. | (((elf_vma) (field[5])) << 16)
  278. | (((elf_vma) (field[4])) << 24)
  279. | (((elf_vma) (field[3])) << 32)
  280. | (((elf_vma) (field[2])) << 40)
  281. | (((elf_vma) (field[1])) << 48)
  282. | (((elf_vma) (field[0])) << 56);
  283. else if (sizeof (elf_vma) == 4)
  284. {
  285. /* Although we are extracting data from an 8 byte wide field,
  286. we are returning only 4 bytes of data. */
  287. field += 4;
  288. return ((unsigned long) (field[3]))
  289. | (((unsigned long) (field[2])) << 8)
  290. | (((unsigned long) (field[1])) << 16)
  291. | (((unsigned long) (field[0])) << 24);
  292. }
  293. default:
  294. error (_("Unhandled data length: %d\n"), size);
  295. abort ();
  296. }
  297. }
  298. elf_vma
  299. byte_get_signed (unsigned char *field, int size)
  300. {
  301. elf_vma x = byte_get (field, size);
  302. switch (size)
  303. {
  304. case 1:
  305. return (x ^ 0x80) - 0x80;
  306. case 2:
  307. return (x ^ 0x8000) - 0x8000;
  308. case 3:
  309. return (x ^ 0x800000) - 0x800000;
  310. case 4:
  311. return (x ^ 0x80000000) - 0x80000000;
  312. case 5:
  313. case 6:
  314. case 7:
  315. case 8:
  316. /* Reads of 5-, 6-, and 7-byte numbers are the result of
  317. trying to read past the end of a buffer, and will therefore
  318. not have meaningful values, so we don't try to deal with
  319. the sign in these cases. */
  320. return x;
  321. default:
  322. abort ();
  323. }
  324. }
  325. /* Return the high-order 32-bits and the low-order 32-bits
  326. of an 8-byte value separately. */
  327. void
  328. byte_get_64 (unsigned char *field, elf_vma *high, elf_vma *low)
  329. {
  330. if (byte_get == byte_get_big_endian)
  331. {
  332. *high = byte_get_big_endian (field, 4);
  333. *low = byte_get_big_endian (field + 4, 4);
  334. }
  335. else
  336. {
  337. *high = byte_get_little_endian (field + 4, 4);
  338. *low = byte_get_little_endian (field, 4);
  339. }
  340. return;
  341. }
  342. /* Return the path name for a proxy entry in a thin archive, adjusted
  343. relative to the path name of the thin archive itself if necessary.
  344. Always returns a pointer to malloc'ed memory. */
  345. char *
  346. adjust_relative_path (const char *file_name, const char *name,
  347. unsigned long name_len)
  348. {
  349. char * member_file_name;
  350. const char * base_name = lbasename (file_name);
  351. size_t amt;
  352. /* This is a proxy entry for a thin archive member.
  353. If the extended name table contains an absolute path
  354. name, or if the archive is in the current directory,
  355. use the path name as given. Otherwise, we need to
  356. find the member relative to the directory where the
  357. archive is located. */
  358. if (IS_ABSOLUTE_PATH (name) || base_name == file_name)
  359. {
  360. amt = name_len + 1;
  361. if (amt == 0)
  362. return NULL;
  363. member_file_name = (char *) malloc (amt);
  364. if (member_file_name == NULL)
  365. {
  366. error (_("Out of memory\n"));
  367. return NULL;
  368. }
  369. memcpy (member_file_name, name, name_len);
  370. member_file_name[name_len] = '\0';
  371. }
  372. else
  373. {
  374. /* Concatenate the path components of the archive file name
  375. to the relative path name from the extended name table. */
  376. size_t prefix_len = base_name - file_name;
  377. amt = prefix_len + name_len + 1;
  378. /* PR 17531: file: 2896dc8b
  379. Catch wraparound. */
  380. if (amt < prefix_len || amt < name_len)
  381. {
  382. error (_("Abnormal length of thin archive member name: %lx\n"),
  383. name_len);
  384. return NULL;
  385. }
  386. member_file_name = (char *) malloc (amt);
  387. if (member_file_name == NULL)
  388. {
  389. error (_("Out of memory\n"));
  390. return NULL;
  391. }
  392. memcpy (member_file_name, file_name, prefix_len);
  393. memcpy (member_file_name + prefix_len, name, name_len);
  394. member_file_name[prefix_len + name_len] = '\0';
  395. }
  396. return member_file_name;
  397. }
  398. /* Processes the archive index table and symbol table in ARCH.
  399. Entries in the index table are SIZEOF_AR_INDEX bytes long.
  400. Fills in ARCH->next_arhdr_offset and ARCH->arhdr.
  401. If READ_SYMBOLS is true then fills in ARCH->index_num, ARCH->index_array,
  402. ARCH->sym_size and ARCH->sym_table.
  403. It is the caller's responsibility to free ARCH->index_array and
  404. ARCH->sym_table.
  405. Returns TRUE upon success, FALSE otherwise.
  406. If failure occurs an error message is printed. */
  407. static bfd_boolean
  408. process_archive_index_and_symbols (struct archive_info * arch,
  409. unsigned int sizeof_ar_index,
  410. bfd_boolean read_symbols)
  411. {
  412. size_t got;
  413. unsigned long size;
  414. size = strtoul (arch->arhdr.ar_size, NULL, 10);
  415. /* PR 17531: file: 912bd7de. */
  416. if ((signed long) size < 0)
  417. {
  418. error (_("%s: invalid archive header size: %ld\n"),
  419. arch->file_name, size);
  420. return FALSE;
  421. }
  422. size = size + (size & 1);
  423. arch->next_arhdr_offset += sizeof arch->arhdr + size;
  424. if (! read_symbols)
  425. {
  426. if (fseek (arch->file, size, SEEK_CUR) != 0)
  427. {
  428. error (_("%s: failed to skip archive symbol table\n"),
  429. arch->file_name);
  430. return FALSE;
  431. }
  432. }
  433. else
  434. {
  435. unsigned long i;
  436. /* A buffer used to hold numbers read in from an archive index.
  437. These are always SIZEOF_AR_INDEX bytes long and stored in
  438. big-endian format. */
  439. unsigned char integer_buffer[sizeof arch->index_num];
  440. unsigned char * index_buffer;
  441. assert (sizeof_ar_index <= sizeof integer_buffer);
  442. /* Check the size of the archive index. */
  443. if (size < sizeof_ar_index)
  444. {
  445. error (_("%s: the archive index is empty\n"), arch->file_name);
  446. return FALSE;
  447. }
  448. /* Read the number of entries in the archive index. */
  449. got = fread (integer_buffer, 1, sizeof_ar_index, arch->file);
  450. if (got != sizeof_ar_index)
  451. {
  452. error (_("%s: failed to read archive index\n"), arch->file_name);
  453. return FALSE;
  454. }
  455. arch->index_num = byte_get_big_endian (integer_buffer, sizeof_ar_index);
  456. size -= sizeof_ar_index;
  457. if (size < arch->index_num * sizeof_ar_index
  458. /* PR 17531: file: 585515d1. */
  459. || size < arch->index_num)
  460. {
  461. error (_("%s: the archive index is supposed to have 0x%lx entries of %d bytes, but the size is only 0x%lx\n"),
  462. arch->file_name, (long) arch->index_num, sizeof_ar_index, size);
  463. return FALSE;
  464. }
  465. /* Read in the archive index. */
  466. index_buffer = (unsigned char *)
  467. malloc (arch->index_num * sizeof_ar_index);
  468. if (index_buffer == NULL)
  469. {
  470. error (_("Out of memory whilst trying to read archive symbol index\n"));
  471. return FALSE;
  472. }
  473. got = fread (index_buffer, sizeof_ar_index, arch->index_num, arch->file);
  474. if (got != arch->index_num)
  475. {
  476. free (index_buffer);
  477. error (_("%s: failed to read archive index\n"), arch->file_name);
  478. return FALSE;
  479. }
  480. size -= arch->index_num * sizeof_ar_index;
  481. /* Convert the index numbers into the host's numeric format. */
  482. arch->index_array = (elf_vma *)
  483. malloc (arch->index_num * sizeof (* arch->index_array));
  484. if (arch->index_array == NULL)
  485. {
  486. free (index_buffer);
  487. error (_("Out of memory whilst trying to convert the archive symbol index\n"));
  488. return FALSE;
  489. }
  490. for (i = 0; i < arch->index_num; i++)
  491. arch->index_array[i] =
  492. byte_get_big_endian ((unsigned char *) (index_buffer + (i * sizeof_ar_index)),
  493. sizeof_ar_index);
  494. free (index_buffer);
  495. /* The remaining space in the header is taken up by the symbol table. */
  496. if (size < 1)
  497. {
  498. error (_("%s: the archive has an index but no symbols\n"),
  499. arch->file_name);
  500. return FALSE;
  501. }
  502. arch->sym_table = (char *) malloc (size);
  503. if (arch->sym_table == NULL)
  504. {
  505. error (_("Out of memory whilst trying to read archive index symbol table\n"));
  506. return FALSE;
  507. }
  508. arch->sym_size = size;
  509. got = fread (arch->sym_table, 1, size, arch->file);
  510. if (got != size)
  511. {
  512. error (_("%s: failed to read archive index symbol table\n"),
  513. arch->file_name);
  514. return FALSE;
  515. }
  516. }
  517. /* Read the next archive header. */
  518. got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file);
  519. if (got != sizeof arch->arhdr && got != 0)
  520. {
  521. error (_("%s: failed to read archive header following archive index\n"),
  522. arch->file_name);
  523. return FALSE;
  524. }
  525. return TRUE;
  526. }
  527. /* Read the symbol table and long-name table from an archive. */
  528. int
  529. setup_archive (struct archive_info *arch, const char *file_name,
  530. FILE *file, bfd_boolean is_thin_archive,
  531. bfd_boolean read_symbols)
  532. {
  533. size_t got;
  534. arch->file_name = strdup (file_name);
  535. arch->file = file;
  536. arch->index_num = 0;
  537. arch->index_array = NULL;
  538. arch->sym_table = NULL;
  539. arch->sym_size = 0;
  540. arch->longnames = NULL;
  541. arch->longnames_size = 0;
  542. arch->nested_member_origin = 0;
  543. arch->is_thin_archive = is_thin_archive;
  544. arch->uses_64bit_indicies = FALSE;
  545. arch->next_arhdr_offset = SARMAG;
  546. /* Read the first archive member header. */
  547. if (fseek (file, SARMAG, SEEK_SET) != 0)
  548. {
  549. error (_("%s: failed to seek to first archive header\n"), file_name);
  550. return 1;
  551. }
  552. got = fread (&arch->arhdr, 1, sizeof arch->arhdr, file);
  553. if (got != sizeof arch->arhdr)
  554. {
  555. if (got == 0)
  556. return 0;
  557. error (_("%s: failed to read archive header\n"), file_name);
  558. return 1;
  559. }
  560. /* See if this is the archive symbol table. */
  561. if (const_strneq (arch->arhdr.ar_name, "/ "))
  562. {
  563. if (! process_archive_index_and_symbols (arch, 4, read_symbols))
  564. return 1;
  565. }
  566. else if (const_strneq (arch->arhdr.ar_name, "/SYM64/ "))
  567. {
  568. arch->uses_64bit_indicies = TRUE;
  569. if (! process_archive_index_and_symbols (arch, 8, read_symbols))
  570. return 1;
  571. }
  572. else if (read_symbols)
  573. printf (_("%s has no archive index\n"), file_name);
  574. if (const_strneq (arch->arhdr.ar_name, "// "))
  575. {
  576. /* This is the archive string table holding long member names. */
  577. arch->longnames_size = strtoul (arch->arhdr.ar_size, NULL, 10);
  578. /* PR 17531: file: 01068045. */
  579. if (arch->longnames_size < 8)
  580. {
  581. error (_("%s: long name table is too small, (size = %ld)\n"),
  582. file_name, arch->longnames_size);
  583. return 1;
  584. }
  585. /* PR 17531: file: 639d6a26. */
  586. if ((signed long) arch->longnames_size < 0)
  587. {
  588. error (_("%s: long name table is too big, (size = 0x%lx)\n"),
  589. file_name, arch->longnames_size);
  590. return 1;
  591. }
  592. arch->next_arhdr_offset += sizeof arch->arhdr + arch->longnames_size;
  593. /* Plus one to allow for a string terminator. */
  594. arch->longnames = (char *) malloc (arch->longnames_size + 1);
  595. if (arch->longnames == NULL)
  596. {
  597. error (_("Out of memory reading long symbol names in archive\n"));
  598. return 1;
  599. }
  600. if (fread (arch->longnames, arch->longnames_size, 1, file) != 1)
  601. {
  602. free (arch->longnames);
  603. arch->longnames = NULL;
  604. error (_("%s: failed to read long symbol name string table\n"),
  605. file_name);
  606. return 1;
  607. }
  608. if ((arch->longnames_size & 1) != 0)
  609. getc (file);
  610. arch->longnames[arch->longnames_size] = 0;
  611. }
  612. return 0;
  613. }
  614. /* Open and setup a nested archive, if not already open. */
  615. int
  616. setup_nested_archive (struct archive_info *nested_arch,
  617. const char *member_file_name)
  618. {
  619. FILE * member_file;
  620. /* Have we already setup this archive? */
  621. if (nested_arch->file_name != NULL
  622. && streq (nested_arch->file_name, member_file_name))
  623. return 0;
  624. /* Close previous file and discard cached information. */
  625. if (nested_arch->file != NULL)
  626. fclose (nested_arch->file);
  627. release_archive (nested_arch);
  628. member_file = fopen (member_file_name, "rb");
  629. if (member_file == NULL)
  630. return 1;
  631. return setup_archive (nested_arch, member_file_name, member_file,
  632. FALSE, FALSE);
  633. }
  634. /* Release the memory used for the archive information. */
  635. void
  636. release_archive (struct archive_info * arch)
  637. {
  638. if (arch->file_name != NULL)
  639. free (arch->file_name);
  640. if (arch->index_array != NULL)
  641. free (arch->index_array);
  642. if (arch->sym_table != NULL)
  643. free (arch->sym_table);
  644. if (arch->longnames != NULL)
  645. free (arch->longnames);
  646. }
  647. /* Get the name of an archive member from the current archive header.
  648. For simple names, this will modify the ar_name field of the current
  649. archive header. For long names, it will return a pointer to the
  650. longnames table. For nested archives, it will open the nested archive
  651. and get the name recursively. NESTED_ARCH is a single-entry cache so
  652. we don't keep rereading the same information from a nested archive. */
  653. char *
  654. get_archive_member_name (struct archive_info *arch,
  655. struct archive_info *nested_arch)
  656. {
  657. unsigned long j, k;
  658. if (arch->arhdr.ar_name[0] == '/')
  659. {
  660. /* We have a long name. */
  661. char *endp;
  662. char *member_file_name;
  663. char *member_name;
  664. if (arch->longnames == NULL || arch->longnames_size == 0)
  665. {
  666. error (_("Archive member uses long names, but no longname table found\n"));
  667. return NULL;
  668. }
  669. arch->nested_member_origin = 0;
  670. k = j = strtoul (arch->arhdr.ar_name + 1, &endp, 10);
  671. if (arch->is_thin_archive && endp != NULL && * endp == ':')
  672. arch->nested_member_origin = strtoul (endp + 1, NULL, 10);
  673. if (j > arch->longnames_size)
  674. {
  675. error (_("Found long name index (%ld) beyond end of long name table\n"),j);
  676. return NULL;
  677. }
  678. while ((j < arch->longnames_size)
  679. && (arch->longnames[j] != '\n')
  680. && (arch->longnames[j] != '\0'))
  681. j++;
  682. if (j > 0 && arch->longnames[j-1] == '/')
  683. j--;
  684. if (j > arch->longnames_size)
  685. j = arch->longnames_size;
  686. arch->longnames[j] = '\0';
  687. if (!arch->is_thin_archive || arch->nested_member_origin == 0)
  688. return arch->longnames + k;
  689. /* PR 17531: file: 2896dc8b. */
  690. if (k >= j)
  691. {
  692. error (_("Invalid Thin archive member name\n"));
  693. return NULL;
  694. }
  695. /* This is a proxy for a member of a nested archive.
  696. Find the name of the member in that archive. */
  697. member_file_name = adjust_relative_path (arch->file_name,
  698. arch->longnames + k, j - k);
  699. if (member_file_name != NULL
  700. && setup_nested_archive (nested_arch, member_file_name) == 0)
  701. {
  702. member_name = get_archive_member_name_at (nested_arch,
  703. arch->nested_member_origin,
  704. NULL);
  705. if (member_name != NULL)
  706. {
  707. free (member_file_name);
  708. return member_name;
  709. }
  710. }
  711. free (member_file_name);
  712. /* Last resort: just return the name of the nested archive. */
  713. return arch->longnames + k;
  714. }
  715. /* We have a normal (short) name. */
  716. for (j = 0; j < sizeof (arch->arhdr.ar_name); j++)
  717. if (arch->arhdr.ar_name[j] == '/')
  718. {
  719. arch->arhdr.ar_name[j] = '\0';
  720. return arch->arhdr.ar_name;
  721. }
  722. /* The full ar_name field is used. Don't rely on ar_date starting
  723. with a zero byte. */
  724. {
  725. char *name = xmalloc (sizeof (arch->arhdr.ar_name) + 1);
  726. memcpy (name, arch->arhdr.ar_name, sizeof (arch->arhdr.ar_name));
  727. name[sizeof (arch->arhdr.ar_name)] = '\0';
  728. return name;
  729. }
  730. }
  731. /* Get the name of an archive member at a given OFFSET within an archive
  732. ARCH. */
  733. char *
  734. get_archive_member_name_at (struct archive_info *arch,
  735. unsigned long offset,
  736. struct archive_info *nested_arch)
  737. {
  738. size_t got;
  739. if (fseek (arch->file, offset, SEEK_SET) != 0)
  740. {
  741. error (_("%s: failed to seek to next file name\n"), arch->file_name);
  742. return NULL;
  743. }
  744. got = fread (&arch->arhdr, 1, sizeof arch->arhdr, arch->file);
  745. if (got != sizeof arch->arhdr)
  746. {
  747. error (_("%s: failed to read archive header\n"), arch->file_name);
  748. return NULL;
  749. }
  750. if (memcmp (arch->arhdr.ar_fmag, ARFMAG, 2) != 0)
  751. {
  752. error (_("%s: did not find a valid archive header\n"),
  753. arch->file_name);
  754. return NULL;
  755. }
  756. return get_archive_member_name (arch, nested_arch);
  757. }
  758. /* Construct a string showing the name of the archive member, qualified
  759. with the name of the containing archive file. For thin archives, we
  760. use square brackets to denote the indirection. For nested archives,
  761. we show the qualified name of the external member inside the square
  762. brackets (e.g., "thin.a[normal.a(foo.o)]"). */
  763. char *
  764. make_qualified_name (struct archive_info * arch,
  765. struct archive_info * nested_arch,
  766. const char *member_name)
  767. {
  768. const char * error_name = _("<corrupt>");
  769. size_t len;
  770. char * name;
  771. len = strlen (arch->file_name) + strlen (member_name) + 3;
  772. if (arch->is_thin_archive
  773. && arch->nested_member_origin != 0)
  774. {
  775. /* PR 15140: Allow for corrupt thin archives. */
  776. if (nested_arch->file_name)
  777. len += strlen (nested_arch->file_name) + 2;
  778. else
  779. len += strlen (error_name) + 2;
  780. }
  781. name = (char *) malloc (len);
  782. if (name == NULL)
  783. {
  784. error (_("Out of memory\n"));
  785. return NULL;
  786. }
  787. if (arch->is_thin_archive
  788. && arch->nested_member_origin != 0)
  789. {
  790. if (nested_arch->file_name)
  791. snprintf (name, len, "%s[%s(%s)]", arch->file_name,
  792. nested_arch->file_name, member_name);
  793. else
  794. snprintf (name, len, "%s[%s(%s)]", arch->file_name,
  795. error_name, member_name);
  796. }
  797. else if (arch->is_thin_archive)
  798. snprintf (name, len, "%s[%s]", arch->file_name, member_name);
  799. else
  800. snprintf (name, len, "%s(%s)", arch->file_name, member_name);
  801. return name;
  802. }