^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 1) // SPDX-License-Identifier: GPL-2.0
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 2) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 3) * arch/alpha/boot/tools/objstrip.c
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 4) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 5) * Strip the object file headers/trailers from an executable (ELF or ECOFF).
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 6) *
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 7) * Copyright (C) 1996 David Mosberger-Tang.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 8) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 9) /*
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 10) * Converts an ECOFF or ELF object file into a bootable file. The
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 11) * object file must be a OMAGIC file (i.e., data and bss follow immediately
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 12) * behind the text). See DEC "Assembly Language Programmer's Guide"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 13) * documentation for details. The SRM boot process is documented in
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 14) * the Alpha AXP Architecture Reference Manual, Second Edition by
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 15) * Richard L. Sites and Richard T. Witek.
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 16) */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 17) #include <stdio.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 18) #include <string.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 19) #include <stdlib.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 20) #include <unistd.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 21)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 22) #include <sys/fcntl.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 23) #include <sys/stat.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 24) #include <sys/types.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 25)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 26) #include <linux/a.out.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 27) #include <linux/coff.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 28) #include <linux/param.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 29) #ifdef __ELF__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 30) # include <linux/elf.h>
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 31) # define elfhdr elf64_hdr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 32) # define elf_phdr elf64_phdr
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 33) # define elf_check_arch(x) ((x)->e_machine == EM_ALPHA)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 34) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 35)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 36) /* bootfile size must be multiple of BLOCK_SIZE: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 37) #define BLOCK_SIZE 512
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 38)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 39) const char * prog_name;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 40)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 41)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 42) static void
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 43) usage (void)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 44) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 45) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 46) "usage: %s [-v] -p file primary\n"
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 47) " %s [-vb] file [secondary]\n", prog_name, prog_name);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 48) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 49) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 50)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 51)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 52) int
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 53) main (int argc, char *argv[])
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 54) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 55) size_t nwritten, tocopy, n, mem_size, fil_size, pad = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 56) int fd, ofd, i, j, verbose = 0, primary = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 57) char buf[8192], *inname;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 58) struct exec * aout; /* includes file & aout header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 59) long offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 60) #ifdef __ELF__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 61) struct elfhdr *elf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 62) struct elf_phdr *elf_phdr; /* program header */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 63) unsigned long long e_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 64) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 65)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 66) prog_name = argv[0];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 67)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 68) for (i = 1; i < argc && argv[i][0] == '-'; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 69) for (j = 1; argv[i][j]; ++j) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 70) switch (argv[i][j]) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 71) case 'v':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 72) verbose = ~verbose;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 73) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 74)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 75) case 'b':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 76) pad = BLOCK_SIZE;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 77) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 78)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 79) case 'p':
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 80) primary = 1; /* make primary bootblock */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 81) break;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 82) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 83) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 84) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 85)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 86) if (i >= argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 87) usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 88) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 89) inname = argv[i++];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 90)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 91) fd = open(inname, O_RDONLY);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 92) if (fd == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 93) perror("open");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 94) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 95) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 96)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 97) ofd = 1;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 98) if (i < argc) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 99) ofd = open(argv[i++], O_WRONLY | O_CREAT | O_TRUNC, 0666);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 100) if (ofd == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 101) perror("open");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 102) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 103) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 104) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 105)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 106) if (primary) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 107) /* generate bootblock for primary loader */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 108)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 109) unsigned long bb[64], sum = 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 110) struct stat st;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 111) off_t size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 112) int i;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 113)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 114) if (ofd == 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 115) usage();
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 116) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 117)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 118) if (fstat(fd, &st) == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 119) perror("fstat");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 120) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 121) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 122)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 123) size = (st.st_size + BLOCK_SIZE - 1) & ~(BLOCK_SIZE - 1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 124) memset(bb, 0, sizeof(bb));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 125) strcpy((char *) bb, "Linux SRM bootblock");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 126) bb[60] = size / BLOCK_SIZE; /* count */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 127) bb[61] = 1; /* starting sector # */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 128) bb[62] = 0; /* flags---must be 0 */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 129) for (i = 0; i < 63; ++i) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 130) sum += bb[i];
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 131) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 132) bb[63] = sum;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 133) if (write(ofd, bb, sizeof(bb)) != sizeof(bb)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 134) perror("boot-block write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 135) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 136) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 137) printf("%lu\n", size);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 138) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 139) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 140)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 141) /* read and inspect exec header: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 142)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 143) if (read(fd, buf, sizeof(buf)) < 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 144) perror("read");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 145) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 146) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 147)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 148) #ifdef __ELF__
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 149) elf = (struct elfhdr *) buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 150)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 151) if (elf->e_ident[0] == 0x7f && str_has_prefix((char *)elf->e_ident + 1, "ELF")) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 152) if (elf->e_type != ET_EXEC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 153) fprintf(stderr, "%s: %s is not an ELF executable\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 154) prog_name, inname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 155) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 156) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 157) if (!elf_check_arch(elf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 158) fprintf(stderr, "%s: is not for this processor (e_machine=%d)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 159) prog_name, elf->e_machine);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 160) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 161) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 162) if (elf->e_phnum != 1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 163) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 164) "%s: %d program headers (forgot to link with -N?)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 165) prog_name, elf->e_phnum);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 166) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 167)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 168) e_entry = elf->e_entry;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 169)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 170) lseek(fd, elf->e_phoff, SEEK_SET);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 171) if (read(fd, buf, sizeof(*elf_phdr)) != sizeof(*elf_phdr)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 172) perror("read");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 173) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 174) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 175)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 176) elf_phdr = (struct elf_phdr *) buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 177) offset = elf_phdr->p_offset;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 178) mem_size = elf_phdr->p_memsz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 179) fil_size = elf_phdr->p_filesz;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 180)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 181) /* work around ELF bug: */
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 182) if (elf_phdr->p_vaddr < e_entry) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 183) unsigned long delta = e_entry - elf_phdr->p_vaddr;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 184) offset += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 185) mem_size -= delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 186) fil_size -= delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 187) elf_phdr->p_vaddr += delta;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 188) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 189)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 190) if (verbose) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 191) fprintf(stderr, "%s: extracting %#016lx-%#016lx (at %lx)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 192) prog_name, (long) elf_phdr->p_vaddr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 193) elf_phdr->p_vaddr + fil_size, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 194) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 195) } else
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 196) #endif
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 197) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 198) aout = (struct exec *) buf;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 199)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 200) if (!(aout->fh.f_flags & COFF_F_EXEC)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 201) fprintf(stderr, "%s: %s is not in executable format\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 202) prog_name, inname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 203) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 204) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 205)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 206) if (aout->fh.f_opthdr != sizeof(aout->ah)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 207) fprintf(stderr, "%s: %s has unexpected optional header size\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 208) prog_name, inname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 209) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 210) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 211)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 212) if (N_MAGIC(*aout) != OMAGIC) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 213) fprintf(stderr, "%s: %s is not an OMAGIC file\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 214) prog_name, inname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 215) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 216) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 217) offset = N_TXTOFF(*aout);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 218) fil_size = aout->ah.tsize + aout->ah.dsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 219) mem_size = fil_size + aout->ah.bsize;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 220)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 221) if (verbose) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 222) fprintf(stderr, "%s: extracting %#016lx-%#016lx (at %lx)\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 223) prog_name, aout->ah.text_start,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 224) aout->ah.text_start + fil_size, offset);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 225) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 226) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 227)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 228) if (lseek(fd, offset, SEEK_SET) != offset) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 229) perror("lseek");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 230) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 231) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 232)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 233) if (verbose) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 234) fprintf(stderr, "%s: copying %lu byte from %s\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 235) prog_name, (unsigned long) fil_size, inname);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 236) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 237)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 238) tocopy = fil_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 239) while (tocopy > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 240) n = tocopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 241) if (n > sizeof(buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 242) n = sizeof(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 243) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 244) tocopy -= n;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 245) if ((size_t) read(fd, buf, n) != n) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 246) perror("read");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 247) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 248) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 249) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 250) nwritten = write(ofd, buf, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 251) if ((ssize_t) nwritten == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 252) perror("write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 253) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 254) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 255) n -= nwritten;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 256) } while (n > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 257) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 258)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 259) if (pad) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 260) mem_size = ((mem_size + pad - 1) / pad) * pad;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 261) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 262)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 263) tocopy = mem_size - fil_size;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 264) if (tocopy > 0) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 265) fprintf(stderr,
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 266) "%s: zero-filling bss and aligning to %lu with %lu bytes\n",
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 267) prog_name, pad, (unsigned long) tocopy);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 268)
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 269) memset(buf, 0x00, sizeof(buf));
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 270) do {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 271) n = tocopy;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 272) if (n > sizeof(buf)) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 273) n = sizeof(buf);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 274) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 275) nwritten = write(ofd, buf, n);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 276) if ((ssize_t) nwritten == -1) {
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 277) perror("write");
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 278) exit(1);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 279) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 280) tocopy -= nwritten;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 281) } while (tocopy > 0);
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 282) }
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 283) return 0;
^8f3ce5b39 (kx 2023-10-28 12:00:06 +0300 284) }