39 #include <sys/types.h>
44 #include <netinet/in.h>
52 #define PT_MIPS_REGINFO 0x70000000
71 if (lseek(in, offset,
SEEK_SET) < 0) {
72 perror(
"copy: lseek");
79 if (cur >
sizeof ibuf)
82 if ((count =
read(in, ibuf, cur)) != cur) {
83 fprintf(stderr,
"copy: read: %s\n",
85 "premature end of file");
88 if ((count =
write(out, ibuf, cur)) != cur) {
89 perror(
"copy: write");
99 static void combine(
struct sect *base,
struct sect *
new,
int pad)
104 if (base->
vaddr + base->
len != new->vaddr) {
106 base->
len =
new->vaddr - base->
vaddr;
109 "Non-contiguous data can't be converted.\n");
113 base->
len +=
new->len;
117 static int phcmp(
const void *
v1,
const void *
v2)
135 if ((off = lseek(file, offset,
SEEK_SET)) < 0) {
139 if (!(tmp = (
char *)
malloc(len))) {
140 fprintf(stderr,
"%s: Can't allocate %ld bytes.\n", name,
144 count =
read(file, tmp, len);
146 fprintf(stderr,
"%s: read: %s.\n",
148 count ?
strerror(errno) :
"End of file reached");
156 (((unsigned short)(x) & (unsigned short)0x00ffU) << 8) | \
157 (((unsigned short)(x) & (unsigned short)0xff00U) >> 8) ))
161 (((unsigned int)(x) & (unsigned int)0x000000ffUL) << 24) | \
162 (((unsigned int)(x) & (unsigned int)0x0000ff00UL) << 8) | \
163 (((unsigned int)(x) & (unsigned int)0x00ff0000UL) >> 8) | \
164 (((unsigned int)(x) & (unsigned int)0xff000000UL) >> 24) ))
183 static void convert_elf_phdrs(
Elf32_Phdr *
p,
int num)
187 for (i = 0; i < num; i++, p++) {
200 static void convert_elf_shdrs(
Elf32_Shdr *
s,
int num)
204 for (i = 0; i < num; i++, s++) {
218 static void convert_ecoff_filehdr(
struct filehdr *
f)
229 static void convert_ecoff_aouthdr(
struct aouthdr *
a)
248 static void convert_ecoff_esecs(
struct scnhdr *s,
int num)
252 for (i = 0; i < num; i++, s++) {
272 struct sect text, data, bss;
285 if (argc < 3 || argc > 4) {
288 "usage: elf2ecoff <elf executable> <ecoff executable> [-a]\n");
292 if (
strcmp(argv[3],
"-a"))
299 fprintf(stderr,
"Can't open %s for read: %s\n",
305 i =
read(infile, &ex,
sizeof ex);
306 if (i !=
sizeof ex) {
307 fprintf(stderr,
"ex: %s: %s.\n",
316 if (
ntohs(0xaa55) == 0xaa55) {
324 convert_elf_hdr(&ex);
331 convert_elf_phdrs(ph, ex.
e_phnum);
337 convert_elf_shdrs(sh, ex.
e_shnum);
350 for (i = 0; i < ex.
e_phnum; i++) {
357 else if (ph[i].p_type !=
PT_LOAD) {
359 "Program header %d type %d can't be converted.\n",
364 if (ph[i].p_flags &
PF_W) {
365 struct sect ndata, nbss;
372 combine(&data, &ndata, 0);
373 combine(&bss, &nbss, 1);
380 combine(&text, &ntxt, 0);
383 if (ph[i].p_vaddr < cur_vma)
392 "Sections ordering prevents a.out conversion.\n");
400 if (data.
len && !text.
len) {
442 memset(esecs, 0,
sizeof esecs);
443 strcpy(esecs[0].s_name,
".text");
444 strcpy(esecs[1].s_name,
".data");
445 strcpy(esecs[2].s_name,
".bss");
447 strcpy(esecs[3].s_name,
".rdata");
448 strcpy(esecs[4].s_name,
".sdata");
449 strcpy(esecs[5].s_name,
".sbss");
469 #define ECOFF_SEGMENT_ALIGNMENT(a) 0x10
470 #define ECOFF_ROUND(s, a) (((s)+(a)-1)&~((a)-1))
502 fprintf(stderr,
"Unable to create %s: %s\n", argv[2],
508 convert_ecoff_filehdr(&efh);
510 i =
write(outfile, &efh,
sizeof efh);
511 if (i !=
sizeof efh) {
512 perror(
"efh: write");
515 for (i = 0; i < nosecs; i++) {
517 (
"Section %d: %s phys %lx size %lx file offset %lx\n",
518 i, esecs[i].s_name, esecs[i].s_paddr,
519 esecs[i].s_size, esecs[i].s_scnptr);
522 fprintf(stderr,
"wrote %d byte file header.\n", i);
525 convert_ecoff_aouthdr(&eah);
526 i =
write(outfile, &eah,
sizeof eah);
527 if (i !=
sizeof eah) {
528 perror(
"eah: write");
531 fprintf(stderr,
"wrote %d byte a.out header.\n", i);
534 convert_ecoff_esecs(&esecs[0], nosecs);
535 i =
write(outfile, &esecs, nosecs *
sizeof(
struct scnhdr));
536 if (i != nosecs *
sizeof(
struct scnhdr)) {
537 perror(
"esecs: write");
540 fprintf(stderr,
"wrote %d bytes of section headers.\n", i);
542 pad = (
sizeof(efh) +
sizeof(eah) + nosecs *
sizeof(
struct scnhdr)) & 15;
545 i =
write(outfile,
"\0\0\0\0\0\0\0\0\0\0\0\0\0\0", pad);
547 perror(
"ipad: write");
550 fprintf(stderr,
"wrote %d byte pad.\n", i);
558 for (i = 0; i < ex.
e_phnum; i++) {
561 if (ph[i].p_type ==
PT_LOAD && ph[i].p_filesz) {
562 if (cur_vma != ph[i].p_vaddr) {
568 "Intersegment gap (%ld bytes) too large.\n",
573 "Warning: %ld byte intersegment gap.\n",
575 memset(obuf, 0,
sizeof obuf);
584 "Error writing gap: %s\n",
591 fprintf(stderr,
"writing %d bytes...\n",
593 copy(outfile, infile, ph[i].p_offset,
606 memset(obuf, 0,
sizeof obuf);
607 if (
write(outfile, obuf,
sizeof(obuf)) !=
sizeof(obuf)) {
608 fprintf(stderr,
"Error writing PROM padding: %s\n",