Source-Changes-HG archive
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]
[src/trunk]: src/sys/lib/libsa Introduce two functions, and dedup code.
details: https://anonhg.NetBSD.org/src/rev/26736e1749cc
branches: trunk
changeset: 356800:26736e1749cc
user: maxv <maxv%NetBSD.org@localhost>
date: Fri Oct 13 10:39:26 2017 +0000
description:
Introduce two functions, and dedup code.
diffstat:
sys/lib/libsa/loadfile_elf32.c | 234 +++++++++++++++++-----------------------
1 files changed, 99 insertions(+), 135 deletions(-)
diffs (truncated from 373 to 300 lines):
diff -r 5a05a389b6d9 -r 26736e1749cc sys/lib/libsa/loadfile_elf32.c
--- a/sys/lib/libsa/loadfile_elf32.c Fri Oct 13 10:04:27 2017 +0000
+++ b/sys/lib/libsa/loadfile_elf32.c Fri Oct 13 10:39:26 2017 +0000
@@ -1,4 +1,4 @@
-/* $NetBSD: loadfile_elf32.c,v 1.46 2017/10/13 10:04:27 maxv Exp $ */
+/* $NetBSD: loadfile_elf32.c,v 1.47 2017/10/13 10:39:26 maxv Exp $ */
/*
* Copyright (c) 1997, 2008, 2017 The NetBSD Foundation, Inc.
@@ -268,6 +268,62 @@
#define KERNALIGN 4096 /* XXX should depend on marks[] */
/*
+ * Read some data from a file, and put it in the bootloader memory (local).
+ */
+static int
+ELFNAMEEND(readfile_local)(int fd, Elf_Off elfoff, void *addr, size_t size)
+{
+ ssize_t nr;
+
+ if (lseek(fd, elfoff, SEEK_SET) == -1) {
+ WARN(("lseek section headers"));
+ return -1;
+ }
+ nr = read(fd, addr, size);
+ if (nr == -1) {
+ WARN(("read section headers"));
+ return -1;
+ }
+ if (nr != (ssize_t)size) {
+ errno = EIO;
+ WARN(("read section headers"));
+ return -1;
+ }
+
+ return 0;
+}
+
+/*
+ * Read some data from a file, and put it in wherever in memory (global).
+ */
+static int
+ELFNAMEEND(readfile_global)(int fd, u_long offset, Elf_Off elfoff,
+ Elf_Addr addr, size_t size)
+{
+ ssize_t nr;
+
+ /* some ports dont use the offset */
+ (void)&offset;
+
+ if (lseek(fd, elfoff, SEEK_SET) == -1) {
+ WARN(("lseek section"));
+ return -1;
+ }
+ nr = READ(fd, addr, size);
+ if (nr == -1) {
+ WARN(("read section"));
+ return -1;
+ }
+ if (nr != (ssize_t)size) {
+ errno = EIO;
+ WARN(("read section"));
+ return -1;
+ }
+
+ return 0;
+}
+
+/*
* Load a dynamic ELF binary into memory. Layout of the memory:
* +------------+-----------------+-----------------+------------------+
* | ELF HEADER | SECTION HEADERS | KERNEL SECTIONS | SYM+REL SECTIONS |
@@ -287,32 +343,19 @@
Elf_Shdr *shdr;
Elf_Addr shpp, addr;
int i, j, loaded;
- size_t size;
- ssize_t sz, nr;
+ size_t size, shdrsz;
Elf_Addr maxp, elfp = 0;
-
- /* some ports dont use the offset */
- (void)&offset;
+ int ret;
maxp = marks[MARK_END];
internalize_ehdr(elf->e_ident[EI_DATA], elf);
/* Create a local copy of the SECTION HEADERS. */
- sz = elf->e_shnum * sizeof(Elf_Shdr);
- shdr = ALLOC(sz);
- if (lseek(fd, elf->e_shoff, SEEK_SET) == -1) {
- WARN(("lseek section headers"));
- goto out;
- }
- nr = read(fd, shdr, sz);
- if (nr == -1) {
- WARN(("read section headers"));
- goto out;
- }
- if (nr != sz) {
- errno = EIO;
- WARN(("read section headers"));
+ shdrsz = elf->e_shnum * sizeof(Elf_Shdr);
+ shdr = ALLOC(shdrsz);
+ ret = ELFNAMEEND(readfile_local)(fd, elf->e_shoff, shdr, shdrsz);
+ if (ret == -1) {
goto out;
}
@@ -337,7 +380,7 @@
/* Save location of the SECTION HEADERS. */
shpp = maxp;
- maxp += roundup(sz, ELFROUND);
+ maxp += roundup(shdrsz, ELFROUND);
/*
* Load the KERNEL SECTIONS.
@@ -355,21 +398,11 @@
loaded = 1;
break;
case SHT_PROGBITS:
- if (lseek(fd, shdr[i].sh_offset, SEEK_SET) == -1) {
- WARN(("lseek section"));
+ ret = ELFNAMEEND(readfile_global)(fd, offset,
+ shdr[i].sh_offset, addr, size);
+ if (ret == -1) {
goto out;
}
- nr = READ(fd, addr, size);
- if (nr == -1) {
- WARN(("read section"));
- goto out;
- }
- if (nr != (ssize_t)size) {
- errno = EIO;
- WARN(("read section"));
- goto out;
- }
-
loaded = 1;
break;
default:
@@ -409,21 +442,11 @@
case SHT_REL:
case SHT_RELA:
case SHT_SYMTAB:
- if (lseek(fd, shdr[i].sh_offset, SEEK_SET) == -1) {
- WARN(("lseek symbols"));
+ ret = ELFNAMEEND(readfile_global)(fd, offset,
+ shdr[i].sh_offset, addr, size);
+ if (ret == -1) {
goto out;
}
- nr = READ(fd, addr, size);
- if (nr == -1) {
- WARN(("read symbols"));
- goto out;
- }
- if (nr != (ssize_t)size) {
- errno = EIO;
- WARN(("read symbols"));
- goto out;
- }
-
shdr[i].sh_offset = maxp - elfp;
maxp += roundup(size, ELFROUND);
break;
@@ -438,9 +461,9 @@
for (i = 0; i < elf->e_shnum; i++)
externalize_shdr(elf->e_ident[EI_DATA], &shdr[i]);
#endif
- BCOPY(shdr, shpp, sz);
+ BCOPY(shdr, shpp, shdrsz);
- DEALLOC(shdr, sz);
+ DEALLOC(shdr, shdrsz);
/*
* Just update MARK_SYM and MARK_END without touching the rest.
@@ -450,7 +473,7 @@
return 0;
out:
- DEALLOC(shdr, sz);
+ DEALLOC(shdr, shdrsz);
return 1;
}
@@ -468,7 +491,7 @@
Elf_Shdr *shp;
Elf_Addr shpp;
char *shstr = NULL;
- ssize_t nr, sz;
+ size_t sz;
size_t i, j, shstrsz = 0;
struct __packed {
Elf_Nhdr nh;
@@ -476,25 +499,12 @@
uint8_t desc[ELF_NOTE_NETBSD_DESCSZ];
} note;
int first;
-
- /* some ports dont use the offset */
- (void)&offset;
+ int ret;
- if (lseek(fd, elf->e_shoff, SEEK_SET) == -1) {
- WARN(("lseek section headers"));
- return -1;
- }
sz = elf->e_shnum * sizeof(Elf_Shdr);
shp = ALLOC(sz);
-
- nr = read(fd, shp, sz);
- if (nr == -1) {
- WARN(("read section headers"));
- goto out;
- }
- if (nr != sz) {
- errno = EIO;
- WARN(("read section headers"));
+ ret = ELFNAMEEND(readfile_local)(fd, elf->e_shoff, shp, sz);
+ if (ret == -1) {
goto out;
}
@@ -513,31 +523,17 @@
Elf_Off shstroff = shp[elf->e_shstrndx].sh_offset;
shstrsz = shp[elf->e_shstrndx].sh_size;
if (flags & LOAD_SYM) {
- if (lseek(fd, shstroff, SEEK_SET) == -1) {
- WARN(("lseek symbols"));
- goto out;
- }
- nr = READ(fd, maxp, shstrsz);
- if (nr == -1) {
- WARN(("read symbols"));
- goto out;
- }
- if (nr != (ssize_t)shstrsz) {
- errno = EIO;
- WARN(("read symbols"));
+ ret = ELFNAMEEND(readfile_global)(fd, offset,
+ shstroff, maxp, shstrsz);
+ if (ret == -1) {
goto out;
}
}
/* Create a local copy */
shstr = ALLOC(shstrsz);
- if (lseek(fd, shstroff, SEEK_SET) == -1) {
- WARN(("lseek symbols"));
- goto out;
- }
- nr = read(fd, shstr, shstrsz);
- if (nr == -1) {
- WARN(("read symbols"));
+ ret = ELFNAMEEND(readfile_local)(fd, shstroff, shstr, shstrsz);
+ if (ret == -1) {
goto out;
}
shp[elf->e_shstrndx].sh_offset = maxp - elfp;
@@ -585,19 +581,9 @@
if (flags & LOAD_SYM) {
PROGRESS(("%s%ld", first ? " [" : "+",
(u_long)shp[i].sh_size));
- if (lseek(fd, shp[i].sh_offset,
- SEEK_SET) == -1) {
- WARN(("lseek symbols"));
- goto out;
- }
- nr = READ(fd, maxp, shp[i].sh_size);
- if (nr == -1) {
- WARN(("read symbols"));
- goto out;
- }
- if (nr != (ssize_t)shp[i].sh_size) {
- errno = EIO;
- WARN(("read symbols"));
+ ret = ELFNAMEEND(readfile_global)(fd, offset,
+ shp[i].sh_offset, maxp, shp[i].sh_size);
+ if (ret == -1) {
goto out;
}
}
@@ -612,15 +598,13 @@
shp[i].sh_offset = 0;
break;
}
- if (lseek(fd, shp[i].sh_offset, SEEK_SET) == -1) {
- WARN(("lseek note"));
+
+ ret = ELFNAMEEND(readfile_local)(fd, shp[i].sh_offset,
+ ¬e, sizeof(note));
+ if (ret == -1) {
goto out;
Home |
Main Index |
Thread Index |
Old Index