From b59f980dbdb21de19e1c25db4b9b9d558ab69def Mon Sep 17 00:00:00 2001 From: Toomas Soome Date: Mon, 26 Nov 2018 16:41:01 +0200 Subject: 10009 btxld: handle versioning better Reviewed by: Robert Mustacchi Reviewed by: Gergő Mihály Doma Approved by: Richard Lowe MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- usr/src/tools/btxld/btxld.c | 41 ++++++++++++++++++++++++++--------------- usr/src/tools/btxld/version.c | 6 +++--- 2 files changed, 29 insertions(+), 18 deletions(-) diff --git a/usr/src/tools/btxld/btxld.c b/usr/src/tools/btxld/btxld.c index 9d01796e37..794049af85 100644 --- a/usr/src/tools/btxld/btxld.c +++ b/usr/src/tools/btxld/btxld.c @@ -40,6 +40,7 @@ #include #include #include +#include #include #include #include @@ -60,7 +61,6 @@ #define F_CNT 3 /* Number of formats */ #define IMPURE 1 /* Writable text */ -#define MAXU32 0xffffffff /* Maximum unsigned 32-bit quantity */ #define align(x, y) (((x) + (y) - 1) & ~((y) - 1)) @@ -94,10 +94,10 @@ static const char *oname = static int ppage = -1; /* First page present */ static int wpage = -1; /* First page writable */ -static unsigned int format; /* Output format */ +static unsigned int format; /* Output format */ -static uint32_t centry; /* Client entry address */ -static uint32_t lentry; /* Loader entry address */ +static uint32_t centry; /* Client entry address */ +static uint32_t lentry; /* Loader entry address */ static int Eflag; /* Client entry option */ @@ -121,7 +121,7 @@ static uint32_t optaddr(const char *); static int optpage(const char *, int); static void Warn(const char *, const char *, ...); static void usage(void); -extern void add_version(const char *, char *); +extern void add_version(const char *, const char *, char *); /* * A link editor for BTX clients. @@ -178,8 +178,20 @@ main(int argc, char *argv[]) atexit(cleanup); if (lname != NULL && bname != NULL) btxld(*argv); - if (version != NULL) - add_version(oname, version); + + if (version != NULL) { + if (tname != NULL) { + add_version(tname, oname, version); + cleanup(); + } else { + add_version(*argv, oname, version); + } + } else { + if (rename(tname, oname)) + err(2, "%s: Can't rename to %s", tname, oname); + free((void *)(intptr_t)tname); + tname = NULL; + } return 0; } @@ -189,8 +201,11 @@ main(int argc, char *argv[]) static void cleanup(void) { - if (tname) - (void) remove(tname); + if (tname) { + (void) remove(tname); + free((void *)(intptr_t)tname); + tname = NULL; + } } /* @@ -286,10 +301,6 @@ btxld(const char *iname) } if (close(fdo)) err(2, "%s", tname); - if (rename(tname, oname)) - err(2, "%s: Can't rename to %s", tname, oname); - free((void *)(intptr_t)tname); - tname = NULL; if (verbose) { printf(binfo, btx.btx_majver, btx.btx_minver, btx.btx_textsz, BTX_ORIGIN(btx), BTX_ENTRY(btx), BTX_MAPPED(btx) * @@ -335,7 +346,7 @@ gethdr(int fd, struct hdr *hdr) memset(hdr, 0, sizeof(*hdr)); if (fstat(fd, &sb)) err(2, "%s", fname); - if (sb.st_size > MAXU32) + if (sb.st_size > UINT32_MAX) errx(1, "%s: Too big", fname); hdr->size = sb.st_size; if (!hdr->size) @@ -525,7 +536,7 @@ optaddr(const char *arg) errno = 0; x = strtoul(arg, &s, 0); - if (errno || !*arg || *s || x > MAXU32) + if (errno || !*arg || *s || x > UINT32_MAX) errx(1, "%s: Illegal address", arg); return x; } diff --git a/usr/src/tools/btxld/version.c b/usr/src/tools/btxld/version.c index c61c4ad784..4b5c24c524 100644 --- a/usr/src/tools/btxld/version.c +++ b/usr/src/tools/btxld/version.c @@ -43,7 +43,7 @@ */ void -add_version(char *file, char *version) +add_version(const char *ifile, const char *ofile, char *version) { int fd; int ret; @@ -56,7 +56,7 @@ add_version(char *file, char *version) char *buf; bblk_hs_t hs; - fd = open(file, O_RDONLY); + fd = open(ifile, O_RDONLY); if (fd == -1) { perror("open"); return; @@ -114,7 +114,7 @@ add_version(char *file, char *version) memset(buf+sb.st_size, 0, buf_size - sb.st_size); add_einfo(buf + extra, version, &hs, avail_space); - fd = open(file, O_WRONLY | O_TRUNC); + fd = open(ofile, O_CREAT | O_WRONLY | O_TRUNC, 0644); if (fd == -1) { perror("open"); free(buf); -- cgit v1.2.3