elfedit.c 20 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768
  1. /* elfedit.c -- Update the ELF header of an ELF format file
  2. Copyright (C) 2010-2015 Free Software Foundation, Inc.
  3. This file is part of GNU Binutils.
  4. This program is free software; you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program; if not, write to the Free Software
  14. Foundation, Inc., 51 Franklin Street - Fifth Floor, Boston, MA
  15. 02110-1301, USA. */
  16. #include "sysdep.h"
  17. #include <assert.h>
  18. #if __GNUC__ >= 2
  19. /* Define BFD64 here, even if our default architecture is 32 bit ELF
  20. as this will allow us to read in and parse 64bit and 32bit ELF files.
  21. Only do this if we believe that the compiler can support a 64 bit
  22. data type. For now we only rely on GCC being able to do this. */
  23. #define BFD64
  24. #endif
  25. #include "bfd.h"
  26. #include "elfcomm.h"
  27. #include "bucomm.h"
  28. #include "elf/common.h"
  29. #include "elf/external.h"
  30. #include "elf/internal.h"
  31. #include "getopt.h"
  32. #include "libiberty.h"
  33. #include "safe-ctype.h"
  34. #include "filenames.h"
  35. char * program_name = "elfedit";
  36. static long archive_file_offset;
  37. static unsigned long archive_file_size;
  38. static Elf_Internal_Ehdr elf_header;
  39. static Elf32_External_Ehdr ehdr32;
  40. static Elf64_External_Ehdr ehdr64;
  41. static int input_elf_machine = -1;
  42. static int output_elf_machine = -1;
  43. static int input_elf_type = -1;
  44. static int output_elf_type = -1;
  45. static int input_elf_osabi = -1;
  46. static int output_elf_osabi = -1;
  47. enum elfclass
  48. {
  49. ELF_CLASS_UNKNOWN = -1,
  50. ELF_CLASS_NONE = ELFCLASSNONE,
  51. ELF_CLASS_32 = ELFCLASS32,
  52. ELF_CLASS_64 = ELFCLASS64,
  53. ELF_CLASS_BOTH
  54. };
  55. static enum elfclass input_elf_class = ELF_CLASS_UNKNOWN;
  56. static enum elfclass output_elf_class = ELF_CLASS_BOTH;
  57. /* Return ELF class for a machine type, MACH. */
  58. static enum elfclass
  59. elf_class (int mach)
  60. {
  61. switch (mach)
  62. {
  63. case EM_386:
  64. case EM_IAMCU:
  65. return ELF_CLASS_32;
  66. case EM_L1OM:
  67. case EM_K1OM:
  68. return ELF_CLASS_64;
  69. case EM_X86_64:
  70. case EM_NONE:
  71. return ELF_CLASS_BOTH;
  72. default:
  73. return ELF_CLASS_BOTH;
  74. }
  75. }
  76. static int
  77. update_elf_header (const char *file_name, FILE *file)
  78. {
  79. int class, machine, type, status, osabi;
  80. if (elf_header.e_ident[EI_MAG0] != ELFMAG0
  81. || elf_header.e_ident[EI_MAG1] != ELFMAG1
  82. || elf_header.e_ident[EI_MAG2] != ELFMAG2
  83. || elf_header.e_ident[EI_MAG3] != ELFMAG3)
  84. {
  85. error
  86. (_("%s: Not an ELF file - wrong magic bytes at the start\n"),
  87. file_name);
  88. return 0;
  89. }
  90. if (elf_header.e_ident[EI_VERSION] != EV_CURRENT)
  91. {
  92. error
  93. (_("%s: Unsupported EI_VERSION: %d is not %d\n"),
  94. file_name, elf_header.e_ident[EI_VERSION],
  95. EV_CURRENT);
  96. return 0;
  97. }
  98. /* Return if e_machine is the same as output_elf_machine. */
  99. if (output_elf_machine == elf_header.e_machine)
  100. return 1;
  101. class = elf_header.e_ident[EI_CLASS];
  102. machine = elf_header.e_machine;
  103. /* Skip if class doesn't match. */
  104. if (input_elf_class == ELF_CLASS_UNKNOWN)
  105. input_elf_class = elf_class (machine);
  106. if (input_elf_class != ELF_CLASS_BOTH
  107. && (int) input_elf_class != class)
  108. {
  109. error
  110. (_("%s: Unmatched input EI_CLASS: %d is not %d\n"),
  111. file_name, class, input_elf_class);
  112. return 0;
  113. }
  114. if (output_elf_class != ELF_CLASS_BOTH
  115. && (int) output_elf_class != class)
  116. {
  117. error
  118. (_("%s: Unmatched output EI_CLASS: %d is not %d\n"),
  119. file_name, class, output_elf_class);
  120. return 0;
  121. }
  122. /* Skip if e_machine doesn't match. */
  123. if (input_elf_machine != -1 && machine != input_elf_machine)
  124. {
  125. error
  126. (_("%s: Unmatched e_machine: %d is not %d\n"),
  127. file_name, machine, input_elf_machine);
  128. return 0;
  129. }
  130. type = elf_header.e_type;
  131. /* Skip if e_type doesn't match. */
  132. if (input_elf_type != -1 && type != input_elf_type)
  133. {
  134. error
  135. (_("%s: Unmatched e_type: %d is not %d\n"),
  136. file_name, type, input_elf_type);
  137. return 0;
  138. }
  139. osabi = elf_header.e_ident[EI_OSABI];
  140. /* Skip if OSABI doesn't match. */
  141. if (input_elf_osabi != -1 && osabi != input_elf_osabi)
  142. {
  143. error
  144. (_("%s: Unmatched EI_OSABI: %d is not %d\n"),
  145. file_name, osabi, input_elf_osabi);
  146. return 0;
  147. }
  148. /* Update e_machine, e_type and EI_OSABI. */
  149. switch (class)
  150. {
  151. default:
  152. /* We should never get here. */
  153. abort ();
  154. break;
  155. case ELFCLASS32:
  156. if (output_elf_machine != -1)
  157. BYTE_PUT (ehdr32.e_machine, output_elf_machine);
  158. if (output_elf_type != -1)
  159. BYTE_PUT (ehdr32.e_type, output_elf_type);
  160. if (output_elf_osabi != -1)
  161. ehdr32.e_ident[EI_OSABI] = output_elf_osabi;
  162. status = fwrite (&ehdr32, sizeof (ehdr32), 1, file) == 1;
  163. break;
  164. case ELFCLASS64:
  165. if (output_elf_machine != -1)
  166. BYTE_PUT (ehdr64.e_machine, output_elf_machine);
  167. if (output_elf_type != -1)
  168. BYTE_PUT (ehdr64.e_type, output_elf_type);
  169. if (output_elf_osabi != -1)
  170. ehdr64.e_ident[EI_OSABI] = output_elf_osabi;
  171. status = fwrite (&ehdr64, sizeof (ehdr64), 1, file) == 1;
  172. break;
  173. }
  174. if (status != 1)
  175. error (_("%s: Failed to update ELF header: %s\n"),
  176. file_name, strerror (errno));
  177. return status;
  178. }
  179. static int
  180. get_file_header (FILE * file)
  181. {
  182. /* Read in the identity array. */
  183. if (fread (elf_header.e_ident, EI_NIDENT, 1, file) != 1)
  184. return 0;
  185. /* Determine how to read the rest of the header. */
  186. switch (elf_header.e_ident[EI_DATA])
  187. {
  188. default: /* fall through */
  189. case ELFDATANONE: /* fall through */
  190. case ELFDATA2LSB:
  191. byte_get = byte_get_little_endian;
  192. byte_put = byte_put_little_endian;
  193. break;
  194. case ELFDATA2MSB:
  195. byte_get = byte_get_big_endian;
  196. byte_put = byte_put_big_endian;
  197. break;
  198. }
  199. /* Read in the rest of the header. For now we only support 32 bit
  200. and 64 bit ELF files. */
  201. switch (elf_header.e_ident[EI_CLASS])
  202. {
  203. default:
  204. error (_("Unsupported EI_CLASS: %d\n"),
  205. elf_header.e_ident[EI_CLASS]);
  206. return 0;
  207. case ELFCLASS32:
  208. if (fread (ehdr32.e_type, sizeof (ehdr32) - EI_NIDENT,
  209. 1, file) != 1)
  210. return 0;
  211. elf_header.e_type = BYTE_GET (ehdr32.e_type);
  212. elf_header.e_machine = BYTE_GET (ehdr32.e_machine);
  213. elf_header.e_version = BYTE_GET (ehdr32.e_version);
  214. elf_header.e_entry = BYTE_GET (ehdr32.e_entry);
  215. elf_header.e_phoff = BYTE_GET (ehdr32.e_phoff);
  216. elf_header.e_shoff = BYTE_GET (ehdr32.e_shoff);
  217. elf_header.e_flags = BYTE_GET (ehdr32.e_flags);
  218. elf_header.e_ehsize = BYTE_GET (ehdr32.e_ehsize);
  219. elf_header.e_phentsize = BYTE_GET (ehdr32.e_phentsize);
  220. elf_header.e_phnum = BYTE_GET (ehdr32.e_phnum);
  221. elf_header.e_shentsize = BYTE_GET (ehdr32.e_shentsize);
  222. elf_header.e_shnum = BYTE_GET (ehdr32.e_shnum);
  223. elf_header.e_shstrndx = BYTE_GET (ehdr32.e_shstrndx);
  224. memcpy (&ehdr32, &elf_header, EI_NIDENT);
  225. break;
  226. case ELFCLASS64:
  227. /* If we have been compiled with sizeof (bfd_vma) == 4, then
  228. we will not be able to cope with the 64bit data found in
  229. 64 ELF files. Detect this now and abort before we start
  230. overwriting things. */
  231. if (sizeof (bfd_vma) < 8)
  232. {
  233. error (_("This executable has been built without support for a\n\
  234. 64 bit data type and so it cannot process 64 bit ELF files.\n"));
  235. return 0;
  236. }
  237. if (fread (ehdr64.e_type, sizeof (ehdr64) - EI_NIDENT,
  238. 1, file) != 1)
  239. return 0;
  240. elf_header.e_type = BYTE_GET (ehdr64.e_type);
  241. elf_header.e_machine = BYTE_GET (ehdr64.e_machine);
  242. elf_header.e_version = BYTE_GET (ehdr64.e_version);
  243. elf_header.e_entry = BYTE_GET (ehdr64.e_entry);
  244. elf_header.e_phoff = BYTE_GET (ehdr64.e_phoff);
  245. elf_header.e_shoff = BYTE_GET (ehdr64.e_shoff);
  246. elf_header.e_flags = BYTE_GET (ehdr64.e_flags);
  247. elf_header.e_ehsize = BYTE_GET (ehdr64.e_ehsize);
  248. elf_header.e_phentsize = BYTE_GET (ehdr64.e_phentsize);
  249. elf_header.e_phnum = BYTE_GET (ehdr64.e_phnum);
  250. elf_header.e_shentsize = BYTE_GET (ehdr64.e_shentsize);
  251. elf_header.e_shnum = BYTE_GET (ehdr64.e_shnum);
  252. elf_header.e_shstrndx = BYTE_GET (ehdr64.e_shstrndx);
  253. memcpy (&ehdr64, &elf_header, EI_NIDENT);
  254. break;
  255. }
  256. return 1;
  257. }
  258. /* Process one ELF object file according to the command line options.
  259. This file may actually be stored in an archive. The file is
  260. positioned at the start of the ELF object. */
  261. static int
  262. process_object (const char *file_name, FILE *file)
  263. {
  264. /* Rememeber where we are. */
  265. long offset = ftell (file);
  266. if (! get_file_header (file))
  267. {
  268. error (_("%s: Failed to read ELF header\n"), file_name);
  269. return 1;
  270. }
  271. /* Go to the position of the ELF header. */
  272. if (fseek (file, offset, SEEK_SET) != 0)
  273. {
  274. error (_("%s: Failed to seek to ELF header\n"), file_name);
  275. }
  276. if (! update_elf_header (file_name, file))
  277. return 1;
  278. return 0;
  279. }
  280. /* Process an ELF archive.
  281. On entry the file is positioned just after the ARMAG string. */
  282. static int
  283. process_archive (const char * file_name, FILE * file,
  284. bfd_boolean is_thin_archive)
  285. {
  286. struct archive_info arch;
  287. struct archive_info nested_arch;
  288. size_t got;
  289. int ret;
  290. /* The ARCH structure is used to hold information about this archive. */
  291. arch.file_name = NULL;
  292. arch.file = NULL;
  293. arch.index_array = NULL;
  294. arch.sym_table = NULL;
  295. arch.longnames = NULL;
  296. /* The NESTED_ARCH structure is used as a single-item cache of information
  297. about a nested archive (when members of a thin archive reside within
  298. another regular archive file). */
  299. nested_arch.file_name = NULL;
  300. nested_arch.file = NULL;
  301. nested_arch.index_array = NULL;
  302. nested_arch.sym_table = NULL;
  303. nested_arch.longnames = NULL;
  304. if (setup_archive (&arch, file_name, file, is_thin_archive, FALSE) != 0)
  305. {
  306. ret = 1;
  307. goto out;
  308. }
  309. ret = 0;
  310. while (1)
  311. {
  312. char * name;
  313. size_t namelen;
  314. char * qualified_name;
  315. /* Read the next archive header. */
  316. if (fseek (file, arch.next_arhdr_offset, SEEK_SET) != 0)
  317. {
  318. error (_("%s: failed to seek to next archive header\n"),
  319. file_name);
  320. return 1;
  321. }
  322. got = fread (&arch.arhdr, 1, sizeof arch.arhdr, file);
  323. if (got != sizeof arch.arhdr)
  324. {
  325. if (got == 0)
  326. break;
  327. error (_("%s: failed to read archive header\n"),
  328. file_name);
  329. ret = 1;
  330. break;
  331. }
  332. if (memcmp (arch.arhdr.ar_fmag, ARFMAG, 2) != 0)
  333. {
  334. error (_("%s: did not find a valid archive header\n"),
  335. arch.file_name);
  336. ret = 1;
  337. break;
  338. }
  339. arch.next_arhdr_offset += sizeof arch.arhdr;
  340. archive_file_size = strtoul (arch.arhdr.ar_size, NULL, 10);
  341. if (archive_file_size & 01)
  342. ++archive_file_size;
  343. name = get_archive_member_name (&arch, &nested_arch);
  344. if (name == NULL)
  345. {
  346. error (_("%s: bad archive file name\n"), file_name);
  347. ret = 1;
  348. break;
  349. }
  350. namelen = strlen (name);
  351. qualified_name = make_qualified_name (&arch, &nested_arch, name);
  352. if (qualified_name == NULL)
  353. {
  354. error (_("%s: bad archive file name\n"), file_name);
  355. ret = 1;
  356. break;
  357. }
  358. if (is_thin_archive && arch.nested_member_origin == 0)
  359. {
  360. /* This is a proxy for an external member of a thin archive. */
  361. FILE *member_file;
  362. char *member_file_name = adjust_relative_path (file_name,
  363. name, namelen);
  364. if (member_file_name == NULL)
  365. {
  366. ret = 1;
  367. break;
  368. }
  369. member_file = fopen (member_file_name, "r+b");
  370. if (member_file == NULL)
  371. {
  372. error (_("Input file '%s' is not readable\n"),
  373. member_file_name);
  374. free (member_file_name);
  375. ret = 1;
  376. break;
  377. }
  378. archive_file_offset = arch.nested_member_origin;
  379. ret |= process_object (qualified_name, member_file);
  380. fclose (member_file);
  381. free (member_file_name);
  382. }
  383. else if (is_thin_archive)
  384. {
  385. /* This is a proxy for a member of a nested archive. */
  386. archive_file_offset = arch.nested_member_origin + sizeof arch.arhdr;
  387. /* The nested archive file will have been opened and setup by
  388. get_archive_member_name. */
  389. if (fseek (nested_arch.file, archive_file_offset,
  390. SEEK_SET) != 0)
  391. {
  392. error (_("%s: failed to seek to archive member\n"),
  393. nested_arch.file_name);
  394. ret = 1;
  395. break;
  396. }
  397. ret |= process_object (qualified_name, nested_arch.file);
  398. }
  399. else
  400. {
  401. archive_file_offset = arch.next_arhdr_offset;
  402. arch.next_arhdr_offset += archive_file_size;
  403. ret |= process_object (qualified_name, file);
  404. }
  405. free (qualified_name);
  406. }
  407. out:
  408. if (nested_arch.file != NULL)
  409. fclose (nested_arch.file);
  410. release_archive (&nested_arch);
  411. release_archive (&arch);
  412. return ret;
  413. }
  414. static int
  415. check_file (const char *file_name, struct stat *statbuf_p)
  416. {
  417. struct stat statbuf;
  418. if (statbuf_p == NULL)
  419. statbuf_p = &statbuf;
  420. if (stat (file_name, statbuf_p) < 0)
  421. {
  422. if (errno == ENOENT)
  423. error (_("'%s': No such file\n"), file_name);
  424. else
  425. error (_("Could not locate '%s'. System error message: %s\n"),
  426. file_name, strerror (errno));
  427. return 1;
  428. }
  429. if (! S_ISREG (statbuf_p->st_mode))
  430. {
  431. error (_("'%s' is not an ordinary file\n"), file_name);
  432. return 1;
  433. }
  434. return 0;
  435. }
  436. static int
  437. process_file (const char *file_name)
  438. {
  439. FILE * file;
  440. char armag[SARMAG];
  441. int ret;
  442. if (check_file (file_name, NULL))
  443. return 1;
  444. file = fopen (file_name, "r+b");
  445. if (file == NULL)
  446. {
  447. error (_("Input file '%s' is not readable\n"), file_name);
  448. return 1;
  449. }
  450. if (fread (armag, SARMAG, 1, file) != 1)
  451. {
  452. error (_("%s: Failed to read file's magic number\n"),
  453. file_name);
  454. fclose (file);
  455. return 1;
  456. }
  457. if (memcmp (armag, ARMAG, SARMAG) == 0)
  458. ret = process_archive (file_name, file, FALSE);
  459. else if (memcmp (armag, ARMAGT, SARMAG) == 0)
  460. ret = process_archive (file_name, file, TRUE);
  461. else
  462. {
  463. rewind (file);
  464. archive_file_size = archive_file_offset = 0;
  465. ret = process_object (file_name, file);
  466. }
  467. fclose (file);
  468. return ret;
  469. }
  470. static const struct
  471. {
  472. int osabi;
  473. const char *name;
  474. }
  475. osabis[] =
  476. {
  477. { ELFOSABI_NONE, "none" },
  478. { ELFOSABI_HPUX, "HPUX" },
  479. { ELFOSABI_NETBSD, "NetBSD" },
  480. { ELFOSABI_GNU, "GNU" },
  481. { ELFOSABI_GNU, "Linux" },
  482. { ELFOSABI_SOLARIS, "Solaris" },
  483. { ELFOSABI_AIX, "AIX" },
  484. { ELFOSABI_IRIX, "Irix" },
  485. { ELFOSABI_FREEBSD, "FreeBSD" },
  486. { ELFOSABI_TRU64, "TRU64" },
  487. { ELFOSABI_MODESTO, "Modesto" },
  488. { ELFOSABI_OPENBSD, "OpenBSD" },
  489. { ELFOSABI_OPENVMS, "OpenVMS" },
  490. { ELFOSABI_NSK, "NSK" },
  491. { ELFOSABI_AROS, "AROS" },
  492. { ELFOSABI_FENIXOS, "FenixOS" }
  493. };
  494. /* Return ELFOSABI_XXX for an OSABI string, OSABI. */
  495. static int
  496. elf_osabi (const char *osabi)
  497. {
  498. unsigned int i;
  499. for (i = 0; i < ARRAY_SIZE (osabis); i++)
  500. if (strcasecmp (osabi, osabis[i].name) == 0)
  501. return osabis[i].osabi;
  502. error (_("Unknown OSABI: %s\n"), osabi);
  503. return -1;
  504. }
  505. /* Return EM_XXX for a machine string, MACH. */
  506. static int
  507. elf_machine (const char *mach)
  508. {
  509. if (strcasecmp (mach, "i386") == 0)
  510. return EM_386;
  511. if (strcasecmp (mach, "iamcu") == 0)
  512. return EM_IAMCU;
  513. if (strcasecmp (mach, "l1om") == 0)
  514. return EM_L1OM;
  515. if (strcasecmp (mach, "k1om") == 0)
  516. return EM_K1OM;
  517. if (strcasecmp (mach, "x86_64") == 0)
  518. return EM_X86_64;
  519. if (strcasecmp (mach, "x86-64") == 0)
  520. return EM_X86_64;
  521. if (strcasecmp (mach, "none") == 0)
  522. return EM_NONE;
  523. error (_("Unknown machine type: %s\n"), mach);
  524. return -1;
  525. }
  526. /* Return ET_XXX for a type string, TYPE. */
  527. static int
  528. elf_type (const char *type)
  529. {
  530. if (strcasecmp (type, "rel") == 0)
  531. return ET_REL;
  532. if (strcasecmp (type, "exec") == 0)
  533. return ET_EXEC;
  534. if (strcasecmp (type, "dyn") == 0)
  535. return ET_DYN;
  536. if (strcasecmp (type, "none") == 0)
  537. return ET_NONE;
  538. error (_("Unknown type: %s\n"), type);
  539. return -1;
  540. }
  541. enum command_line_switch
  542. {
  543. OPTION_INPUT_MACH = 150,
  544. OPTION_OUTPUT_MACH,
  545. OPTION_INPUT_TYPE,
  546. OPTION_OUTPUT_TYPE,
  547. OPTION_INPUT_OSABI,
  548. OPTION_OUTPUT_OSABI
  549. };
  550. static struct option options[] =
  551. {
  552. {"input-mach", required_argument, 0, OPTION_INPUT_MACH},
  553. {"output-mach", required_argument, 0, OPTION_OUTPUT_MACH},
  554. {"input-type", required_argument, 0, OPTION_INPUT_TYPE},
  555. {"output-type", required_argument, 0, OPTION_OUTPUT_TYPE},
  556. {"input-osabi", required_argument, 0, OPTION_INPUT_OSABI},
  557. {"output-osabi", required_argument, 0, OPTION_OUTPUT_OSABI},
  558. {"version", no_argument, 0, 'v'},
  559. {"help", no_argument, 0, 'h'},
  560. {0, no_argument, 0, 0}
  561. };
  562. static void
  563. usage (FILE *stream, int exit_status)
  564. {
  565. fprintf (stream, _("Usage: %s <option(s)> elffile(s)\n"),
  566. program_name);
  567. fprintf (stream, _(" Update the ELF header of ELF files\n"));
  568. fprintf (stream, _(" The options are:\n"));
  569. fprintf (stream, _("\
  570. --input-mach <machine> Set input machine type to <machine>\n\
  571. --output-mach <machine> Set output machine type to <machine>\n\
  572. --input-type <type> Set input file type to <type>\n\
  573. --output-type <type> Set output file type to <type>\n\
  574. --input-osabi <osabi> Set input OSABI to <osabi>\n\
  575. --output-osabi <osabi> Set output OSABI to <osabi>\n\
  576. -h --help Display this information\n\
  577. -v --version Display the version number of %s\n\
  578. "),
  579. program_name);
  580. if (REPORT_BUGS_TO[0] && exit_status == 0)
  581. fprintf (stream, _("Report bugs to %s\n"), REPORT_BUGS_TO);
  582. exit (exit_status);
  583. }
  584. int
  585. main (int argc, char ** argv)
  586. {
  587. int c, status;
  588. #if defined (HAVE_SETLOCALE) && defined (HAVE_LC_MESSAGES)
  589. setlocale (LC_MESSAGES, "");
  590. #endif
  591. #if defined (HAVE_SETLOCALE)
  592. setlocale (LC_CTYPE, "");
  593. #endif
  594. bindtextdomain (PACKAGE, LOCALEDIR);
  595. textdomain (PACKAGE);
  596. expandargv (&argc, &argv);
  597. while ((c = getopt_long (argc, argv, "hv",
  598. options, (int *) 0)) != EOF)
  599. {
  600. switch (c)
  601. {
  602. case OPTION_INPUT_MACH:
  603. input_elf_machine = elf_machine (optarg);
  604. if (input_elf_machine < 0)
  605. return 1;
  606. input_elf_class = elf_class (input_elf_machine);
  607. if (input_elf_class == ELF_CLASS_UNKNOWN)
  608. return 1;
  609. break;
  610. case OPTION_OUTPUT_MACH:
  611. output_elf_machine = elf_machine (optarg);
  612. if (output_elf_machine < 0)
  613. return 1;
  614. output_elf_class = elf_class (output_elf_machine);
  615. if (output_elf_class == ELF_CLASS_UNKNOWN)
  616. return 1;
  617. break;
  618. case OPTION_INPUT_TYPE:
  619. input_elf_type = elf_type (optarg);
  620. if (input_elf_type < 0)
  621. return 1;
  622. break;
  623. case OPTION_OUTPUT_TYPE:
  624. output_elf_type = elf_type (optarg);
  625. if (output_elf_type < 0)
  626. return 1;
  627. break;
  628. case OPTION_INPUT_OSABI:
  629. input_elf_osabi = elf_osabi (optarg);
  630. if (input_elf_osabi < 0)
  631. return 1;
  632. break;
  633. case OPTION_OUTPUT_OSABI:
  634. output_elf_osabi = elf_osabi (optarg);
  635. if (output_elf_osabi < 0)
  636. return 1;
  637. break;
  638. case 'h':
  639. usage (stdout, 0);
  640. case 'v':
  641. print_version (program_name);
  642. break;
  643. default:
  644. usage (stderr, 1);
  645. }
  646. }
  647. if (optind == argc
  648. || (output_elf_machine == -1
  649. && output_elf_type == -1
  650. && output_elf_osabi == -1))
  651. usage (stderr, 1);
  652. status = 0;
  653. while (optind < argc)
  654. status |= process_file (argv[optind++]);
  655. return status;
  656. }