21 #include <sys/fcntl.h>
23 #include <sys/types.h>
25 #include <linux/a.out.h>
29 # include <linux/elf.h>
33 #define BLOCK_SIZE 512
42 "usage: %s [-v] -p file primary\n"
53 char buf[8192], *inname;
59 unsigned long long e_entry;
64 for (i = 1; i < argc && argv[
i][0] ==
'-'; ++
i) {
65 for (j = 1; argv[
i][
j]; ++
j) {
105 unsigned long bb[64],
sum = 0;
114 if (fstat(fd, &st) == -1) {
120 memset(bb, 0,
sizeof(bb));
121 strcpy((
char *) bb,
"Linux SRM bootblock");
125 for (i = 0; i < 63; ++
i) {
129 if (
write(ofd, bb,
sizeof(bb)) !=
sizeof(bb)) {
130 perror(
"boot-block write");
139 if (
read(fd, buf,
sizeof(buf)) < 0) {
145 elf = (
struct elfhdr *) buf;
147 if (elf->e_ident[0] == 0x7f &&
strncmp((
char *)elf->e_ident + 1,
"ELF", 3) == 0) {
149 fprintf(stderr,
"%s: %s is not an ELF executable\n",
154 fprintf(stderr,
"%s: is not for this processor (e_machine=%d)\n",
158 if (elf->e_phnum != 1) {
160 "%s: %d program headers (forgot to link with -N?)\n",
164 e_entry = elf->e_entry;
167 if (
read(fd, buf,
sizeof(*elf_phdr)) !=
sizeof(*elf_phdr)) {
172 elf_phdr = (
struct elf_phdr *) buf;
173 offset = elf_phdr->p_offset;
174 mem_size = elf_phdr->p_memsz;
175 fil_size = elf_phdr->p_filesz;
178 if (elf_phdr->p_vaddr < e_entry) {
179 unsigned long delta = e_entry - elf_phdr->p_vaddr;
183 elf_phdr->p_vaddr +=
delta;
187 fprintf(stderr,
"%s: extracting %#016lx-%#016lx (at %lx)\n",
189 elf_phdr->p_vaddr + fil_size, offset);
194 aout = (
struct exec *) buf;
197 fprintf(stderr,
"%s: %s is not in executable format\n",
202 if (aout->
fh.f_opthdr !=
sizeof(aout->
ah)) {
203 fprintf(stderr,
"%s: %s has unexpected optional header size\n",
209 fprintf(stderr,
"%s: %s is not an OMAGIC file\n",
214 fil_size = aout->
ah.tsize + aout->
ah.dsize;
215 mem_size = fil_size + aout->
ah.bsize;
218 fprintf(stderr,
"%s: extracting %#016lx-%#016lx (at %lx)\n",
220 aout->
ah.text_start + fil_size, offset);
224 if (lseek(fd, offset,
SEEK_SET) != offset) {
230 fprintf(stderr,
"%s: copying %lu byte from %s\n",
231 prog_name, (
unsigned long) fil_size, inname);
237 if (n >
sizeof(buf)) {
241 if ((
size_t)
read(fd, buf, n) !=
n) {
246 nwritten =
write(ofd, buf, n);
247 if ((
ssize_t) nwritten == -1) {
256 mem_size = ((mem_size + pad - 1) / pad) *
pad;
259 tocopy = mem_size - fil_size;
262 "%s: zero-filling bss and aligning to %lu with %lu bytes\n",
265 memset(buf, 0x00,
sizeof(buf));
268 if (n >
sizeof(buf)) {
271 nwritten =
write(ofd, buf, n);
272 if ((
ssize_t) nwritten == -1) {
277 }
while (tocopy > 0);