3 #include "/sys/src/libmach/elf.h"
9 #define ROUND(n) ((n+Page-1)&~(Page-1))
17 return p[0] | p[1]<<8;
24 return p[0] | p[1]<<8 | p[2]<<16 | p[3]<<24;
28 PUTS(void *a, ushort v)
36 PUTL(void *a, ulong v)
46 copy(int fin, int fout, ulong src, ulong dst, ulong size)
64 main(int argc, char **argv)
69 int efd, ofd, ns, i, n;
70 ulong shoff, off, noff, size, msize;
74 sysfatal("Usage: xenelf input-elf-file output-elf-file section-name section-contents");
75 efd = open(argv[1], OREAD);
77 sysfatal("%s: %r", argv[1]);
78 ofd = create(argv[2], OWRITE, 0666);
80 sysfatal("%s: %r", argv[2]);
84 read(efd, &e, sizeof e);
86 // sysfatal("section header string index already present");
88 /* page-align loadable segments in file */
90 shoff = GETL(&e.phoff);
91 noff = shoff+ns*sizeof(Phdr);
93 for (i = 0; i < ns; i++) {
94 seek(efd, shoff+i*sizeof(Phdr), 0);
95 read(efd, &p, sizeof p);
96 off = GETL(&p.offset);
97 PUTL(&p.offset, noff);
98 size = GETL(&p.filesz);
99 copy(efd, ofd, off, noff, size);
100 if (GETL(&p.type) == LOAD) {
102 PUTL(&p.filesz, size);
103 if ((msize = GETL(&p.memsz)) != 0 && size > msize)
104 PUTL(&p.memsz, size);
106 /* memory size for symtab segment is actually line number table size */
107 msize = GETL(&p.memsz);
108 copy(efd, ofd, off+size, noff+size, msize);
112 seek(ofd, shoff+i*sizeof(Phdr), 0);
113 write(ofd, &p, sizeof p);
116 /* append single-entry shstrndx */
117 PUTL(&isect.offset, seek(ofd, noff, 0));
119 PUTL(&isect.size, n+2);
120 write(ofd, sname+n, 1);
121 write(ofd, sname, n+1);
123 /* append comment section contents */
124 PUTL(&csect.name, 1);
125 PUTL(&csect.offset, seek(ofd, 0, 2));
127 PUTL(&csect.size, n+1);
128 write(ofd, sval, n+1);
130 /* copy existing section headers to end */
131 ns = 0; //GETS(&e.shnum);
132 shoff = GETL(&e.shoff);
133 PUTL(&e.shoff, seek(ofd, 0, 2));
134 for (i = 0; i < ns; i++) {
135 seek(efd, shoff+i*sizeof(Shdr), 0);
136 read(efd, &s, sizeof s);
138 write(ofd, &s, sizeof s);
141 /* append section header for comment section */
142 write(ofd, &csect, sizeof csect);
145 /* append section header for shstrndx */
146 PUTS(&e.shstrndx, ns);
148 write(ofd, &isect, sizeof isect);
150 /* rewrite elf header */
151 PUTS(&e.shentsize, sizeof(Shdr));
154 write(ofd, &e, sizeof e);