elfcomm.c 26 KB

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