Browse Source

unify the headers/random defines into porting.h to avoid duplication/rot

Mike Frysinger 15 years ago
parent
commit
32242e7e10
7 changed files with 116 additions and 153 deletions
  1. 10 21
      utils/Makefile.in
  2. 1 17
      utils/chroot_realpath.c
  3. 25 52
      utils/ldconfig.c
  4. 10 34
      utils/ldd.c
  5. 57 0
      utils/porting.h
  6. 8 22
      utils/readelf.c
  7. 5 7
      utils/readsoname2.c

+ 10 - 21
utils/Makefile.in

@@ -9,6 +9,9 @@
 
 CFLAGS-utils := \
     $(SSP_ALL_CFLAGS) \
+    -I$(top_srcdir)ldso/include \
+    -DUCLIBC_RUNTIME_PREFIX=\"$(RUNTIME_PREFIX)\" \
+    -DUCLIBC_LDSO=$(UCLIBC_LDSO) \
     -I$(top_srcdir)/$(KERNEL_HEADERS) \
     -DNOT_IN_libc \
     -B$(top_builddir)lib \
@@ -20,21 +23,14 @@ else
 CFLAGS-utils-shared :=
 endif
 
-CFLAGS-ldconfig := \
-    -I$(top_srcdir)ldso/include \
-    -DUCLIBC_RUNTIME_PREFIX=\"$(RUNTIME_PREFIX)\" \
-    -DUCLIBC_LDSO=$(UCLIBC_LDSO)
+CFLAGS-ldconfig :=
 ifeq ($(UCLIBC_STATIC_LDCONFIG),y)
 CFLAGS-ldconfig += -static
 else
 CFLAGS-ldconfig += $(CFLAGS-utils-shared)
 endif
 
-CFLAGS-ldd := \
-    -I$(top_srcdir)ldso/include \
-    -DUCLIBC_RUNTIME_PREFIX=\"$(RUNTIME_PREFIX)\" \
-    -DUCLIBC_LDSO=$(UCLIBC_LDSO) \
-    $(CFLAGS-utils-shared)
+CFLAGS-ldd := $(CFLAGS-utils-shared)
 
 CFLAGS-readelf := $(CFLAGS-utils-shared)
 
@@ -52,22 +48,15 @@ ifeq ($(LDSO_CACHE_SUPPORT),y)
 LDSO_CACHE_SUPPORT := -D__LDSO_CACHE_SUPPORT__=1
 endif
 
-BUILD_CFLAGS-ldconfig.host := \
+BUILD_CFLAGS-utils := \
     -include $(top_srcdir)include/elf.h \
     -I$(top_srcdir)ldso/include \
     -DUCLIBC_RUNTIME_PREFIX=\"$(RUNTIME_PREFIX)\" \
     -DUCLIBC_LDSO=$(UCLIBC_LDSO) \
-    $(LDSO_CACHE_SUPPORT) \
-
-BUILD_CFLAGS-ldd.host := \
-    -include $(top_srcdir)include/elf.h \
-    -I$(top_srcdir)ldso/include \
-    -DUCLIBC_RUNTIME_PREFIX=\"$(RUNTIME_PREFIX)\" \
-    -DUCLIBC_LDSO=$(UCLIBC_LDSO) \
-    $(LDSO_CACHE_SUPPORT) \
-
-BUILD_CFLAGS-readelf.host := \
-    -include $(top_srcdir)include/elf.h \
+    $(LDSO_CACHE_SUPPORT)
+BUILD_CFLAGS-ldconfig.host := $(BUILD_CFLAGS-utils)
+BUILD_CFLAGS-ldd.host      := $(BUILD_CFLAGS-utils)
+BUILD_CFLAGS-readelf.host  := $(BUILD_CFLAGS-utils)
 
 
 # Rules

+ 1 - 17
utils/chroot_realpath.c

@@ -20,23 +20,7 @@
  * 2005/09/12: Dan Howell (modified from realpath.c to emulate chroot)
  */
 
-#ifdef HAVE_CONFIG_H
-#include <config.h>
-#endif
-
-#include <sys/types.h>
-#include <unistd.h>
-#include <stdio.h>
-#include <string.h>
-#include <strings.h>
-#include <limits.h>		/* for PATH_MAX */
-#include <sys/param.h>		/* for MAXPATHLEN */
-#include <errno.h>
-#include <sys/stat.h>		/* for S_IFLNK */
-
-#ifndef PATH_MAX
-#define PATH_MAX _POSIX_PATH_MAX
-#endif
+#include "porting.h"
 
 #define MAX_READLINKS 32
 

+ 25 - 52
utils/ldconfig.c

@@ -26,20 +26,7 @@
  * 2005/09/16: Dan Howell (modified for cross-development)
  */
 
-#include <stdio.h>
-#include <stdlib.h>
-#include <stdarg.h>
-#include <string.h>
-#include <ctype.h>
-#include <dirent.h>
-#include <unistd.h>
-#include <link.h>
-#include <fcntl.h>
-#include <errno.h>
-#include <sys/stat.h>
-#include <sys/mman.h>
-#include "bswap.h"
-#include "dl-defs.h"
+#include "porting.h"
 
 #define BUFFER_SIZE 4096
 
@@ -72,20 +59,6 @@ struct exec {
 
 char *___strtok = NULL;
 
-/* For SunOS */
-#ifndef PATH_MAX
-#include <limits.h>
-#define PATH_MAX _POSIX_PATH_MAX
-#endif
-
-/* For SunOS */
-#ifndef N_MAGIC
-#define N_MAGIC(exec) ((exec).a_magic & 0xffff)
-#endif
-
-#define EXIT_OK    0
-#define EXIT_FATAL 128
-
 char *prog = NULL;
 int debug = 0;			/* debug mode */
 int verbose = 0;		/* verbose mode */
@@ -177,7 +150,7 @@ static void *xmalloc(size_t size)
 {
 	void *ptr;
 	if ((ptr = malloc(size)) == NULL)
-		err(EXIT_FATAL, "out of memory");
+		err(EXIT_FAILURE, "out of memory");
 	return ptr;
 }
 
@@ -185,7 +158,7 @@ static char *xstrdup(const char *str)
 {
 	char *ptr;
 	if ((ptr = strdup(str)) == NULL)
-		err(EXIT_FATAL, "out of memory");
+		err(EXIT_FAILURE, "out of memory");
 	return ptr;
 }
 
@@ -452,7 +425,7 @@ void scan_dir(const char *rawname)
 	/* We need a writable copy of this string */
 	path = strdup(rawname);
 	if (!path) {
-		err(EXIT_FATAL, "Out of memory!\n");
+		err(EXIT_FAILURE, "Out of memory!\n");
 	}
 	/* Eliminate all double //s */
 	path_n = path;
@@ -678,17 +651,17 @@ void cache_write(void)
 		return;
 
 	if (!chroot_realpath(chroot_dir, cachefile, realcachefile))
-		err(EXIT_FATAL, "can't resolve %s in chroot %s (%s)",
+		err(EXIT_FAILURE, "can't resolve %s in chroot %s (%s)",
 		    cachefile, chroot_dir, strerror(errno));
 
 	sprintf(tempfile, "%s~", realcachefile);
 
 	if (unlink(tempfile) && errno != ENOENT)
-		err(EXIT_FATAL, "can't unlink %s~ (%s)", cachefile,
+		err(EXIT_FAILURE, "can't unlink %s~ (%s)", cachefile,
 		    strerror(errno));
 
 	if ((cachefd = creat(tempfile, 0644)) < 0)
-		err(EXIT_FATAL, "can't create %s~ (%s)", cachefile,
+		err(EXIT_FAILURE, "can't create %s~ (%s)", cachefile,
 		    strerror(errno));
 
 	if (byteswap) {
@@ -699,7 +672,7 @@ void cache_write(void)
 		magic_ptr = &magic;
 	}
 	if (write(cachefd, magic_ptr, sizeof(header_t)) != sizeof(header_t))
-		err(EXIT_FATAL, "can't write %s~ (%s)", cachefile,
+		err(EXIT_FAILURE, "can't write %s~ (%s)", cachefile,
 		    strerror(errno));
 
 	for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next) {
@@ -717,31 +690,31 @@ void cache_write(void)
 		}
 		if (write(cachefd, lib_ptr, sizeof(libentry_t)) !=
 		    sizeof(libentry_t))
-			err(EXIT_FATAL, "can't write %s~ (%s)", cachefile,
+			err(EXIT_FAILURE, "can't write %s~ (%s)", cachefile,
 			    strerror(errno));
 	}
 
 	for (cur_lib = lib_head; cur_lib != NULL; cur_lib = cur_lib->next) {
 		if ((size_t)write(cachefd, cur_lib->soname, strlen(cur_lib->soname) + 1)
 		    != strlen(cur_lib->soname) + 1)
-			err(EXIT_FATAL, "can't write %s~ (%s)", cachefile,
+			err(EXIT_FAILURE, "can't write %s~ (%s)", cachefile,
 			    strerror(errno));
 		if ((size_t)write(cachefd, cur_lib->libname, strlen(cur_lib->libname) + 1)
 		    != strlen(cur_lib->libname) + 1)
-			err(EXIT_FATAL, "can't write %s~ (%s)", cachefile,
+			err(EXIT_FAILURE, "can't write %s~ (%s)", cachefile,
 			    strerror(errno));
 	}
 
 	if (close(cachefd))
-		err(EXIT_FATAL, "can't close %s~ (%s)", cachefile,
+		err(EXIT_FAILURE, "can't close %s~ (%s)", cachefile,
 		    strerror(errno));
 
 	if (chmod(tempfile, 0644))
-		err(EXIT_FATAL, "can't chmod %s~ (%s)", cachefile,
+		err(EXIT_FAILURE, "can't chmod %s~ (%s)", cachefile,
 		    strerror(errno));
 
 	if (rename(tempfile, realcachefile))
-		err(EXIT_FATAL, "can't rename %s~ (%s)", cachefile,
+		err(EXIT_FAILURE, "can't rename %s~ (%s)", cachefile,
 		    strerror(errno));
 }
 
@@ -756,22 +729,22 @@ void cache_print(void)
 	char realcachefile[BUFFER_SIZE];
 
 	if (!chroot_realpath(chroot_dir, cachefile, realcachefile))
-		err(EXIT_FATAL, "can't resolve %s in chroot %s (%s)",
+		err(EXIT_FAILURE, "can't resolve %s in chroot %s (%s)",
 		    cachefile, chroot_dir, strerror(errno));
 
 	if (stat(realcachefile, &st) || (fd = open(realcachefile, O_RDONLY)) < 0)
-		err(EXIT_FATAL, "can't read %s (%s)", cachefile, strerror(errno));
+		err(EXIT_FAILURE, "can't read %s (%s)", cachefile, strerror(errno));
 
 	c = mmap(0, st.st_size, PROT_READ, LDSO_CACHE_MMAP_FLAGS, fd, 0);
 	if (c == MAP_FAILED)
-		err(EXIT_FATAL, "can't map %s (%s)", cachefile, strerror(errno));
+		err(EXIT_FAILURE, "can't map %s (%s)", cachefile, strerror(errno));
 	close(fd);
 
 	if (memcmp(((header_t *) c)->magic, LDSO_CACHE_MAGIC, LDSO_CACHE_MAGIC_LEN))
-		err(EXIT_FATAL, "%s cache corrupt", cachefile);
+		err(EXIT_FAILURE, "%s cache corrupt", cachefile);
 
 	if (memcmp(((header_t *) c)->version, LDSO_CACHE_VER, LDSO_CACHE_VER_LEN))
-		err(EXIT_FATAL, "wrong cache version - expected %s",
+		err(EXIT_FAILURE, "wrong cache version - expected %s",
 		    LDSO_CACHE_VER);
 
 	header = (header_t *) c;
@@ -843,7 +816,7 @@ void usage(void)
 		"\tlib ... :\tlibraries to link\n\n"
 #endif
 	    );
-	exit(EXIT_FATAL);
+	exit(EXIT_FAILURE);
 }
 
 #define DIR_SEP      ":, \t\n"
@@ -916,11 +889,11 @@ int main(int argc, char **argv)
 	if (chroot_dir && *chroot_dir) {
 		if (chroot(chroot_dir) < 0) {
 			if (chdir(chroot_dir) < 0)
-				err(EXIT_FATAL, "couldn't chroot to %s (%s)", chroot_dir, strerror(errno));
+				err(EXIT_FAILURE, "couldn't chroot to %s (%s)", chroot_dir, strerror(errno));
 			chroot_dir = ".";
 		} else {
 			if (chdir("/") < 0)
-				err(EXIT_FATAL, "couldn't chdir to / (%s)", strerror(errno));
+				err(EXIT_FAILURE, "couldn't chdir to / (%s)", strerror(errno));
 			chroot_dir = NULL;
 		}
 	}
@@ -932,7 +905,7 @@ int main(int argc, char **argv)
 	if (printcache) {
 		/* print the cache -- don't you trust me? */
 		cache_print();
-		exit(EXIT_OK);
+		exit(EXIT_SUCCESS);
 	} else if (libmode) {
 		/* so you want to do things manually, eh? */
 
@@ -952,7 +925,7 @@ int main(int argc, char **argv)
 
 			/* we'd better do a little bit of checking */
 			if ((so = is_shlib(dir, cp, &libtype, &islink, LIB_ANY)) == NULL)
-				err(EXIT_FATAL, "%s%s%s is not a shared library",
+				err(EXIT_FAILURE, "%s%s%s is not a shared library",
 				    dir, (*dir && strcmp(dir, "/")) ? "/" : "", cp);
 
 			/* so far, so good, maybe he knows what he's doing */
@@ -1005,5 +978,5 @@ int main(int argc, char **argv)
 			cache_write();
 	}
 
-	exit(EXIT_OK);
+	exit(EXIT_SUCCESS);
 }

+ 10 - 34
utils/ldd.c

@@ -13,25 +13,7 @@
  * Licensed under GPLv2 or later
  */
 
-#include <stdlib.h>
-#include <stdio.h>
-#include <fcntl.h>
-#include <string.h>
-#include <unistd.h>
-#include <stdint.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-#include <sys/wait.h>
-
-#include "bswap.h"
-#include "link.h"
-#include "dl-defs.h"
-/* makefile will include elf.h for us */
-
-#ifdef DMALLOC
-#include <dmalloc.h>
-#endif
+#include "porting.h"
 
 #if defined(__alpha__)
 #define MATCH_MACHINE(x) (x == EM_ALPHA)
@@ -134,16 +116,12 @@
 # warning "You really should add a MATCH_MACHINE() macro for your architecture"
 #endif
 
-#if __BYTE_ORDER == __LITTLE_ENDIAN
+#if UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_LITTLE
 #define ELFDATAM	ELFDATA2LSB
-#elif __BYTE_ORDER == __BIG_ENDIAN
+#elif UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_BIG
 #define ELFDATAM	ELFDATA2MSB
 #endif
 
-#ifndef UCLIBC_RUNTIME_PREFIX
-# define UCLIBC_RUNTIME_PREFIX "/"
-#endif
-
 struct library {
 	char *name;
 	int resolved;
@@ -247,15 +225,13 @@ int check_elf_header(ElfW(Ehdr) *const ehdr)
 
 	/* Check if the target endianness matches the host's endianness */
 	byteswap = 0;
-#if __BYTE_ORDER == __LITTLE_ENDIAN
-	if (ehdr->e_ident[5] == ELFDATA2MSB)
-		byteswap = 1;
-#elif __BYTE_ORDER == __BIG_ENDIAN
-	if (ehdr->e_ident[5] == ELFDATA2LSB)
-		byteswap = 1;
-#else
-#error Unknown host byte order!
-#endif
+	if (UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_LITTLE) {
+		if (ehdr->e_ident[5] == ELFDATA2MSB)
+			byteswap = 1;
+	} else if (UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_BIG) {
+		if (ehdr->e_ident[5] == ELFDATA2LSB)
+			byteswap = 1;
+	}
 
 	/* Be very lazy, and only byteswap the stuff we use */
 	if (byteswap) {

+ 57 - 0
utils/porting.h

@@ -0,0 +1,57 @@
+/* Misc system-specific crap */
+
+#ifndef _PORTING_H_
+#define _PORTING_H_
+
+#ifdef HAVE_CONFIG_H
+#include <config.h>
+#endif
+
+#include <ctype.h>
+#include <dirent.h>
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <stdarg.h>
+#include <stdint.h>
+#include <stdio.h>
+#include <string.h>
+#include <strings.h>
+#include <stdlib.h>
+#include <unistd.h>
+#include <sys/param.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <sys/wait.h>
+
+#include <link.h>
+#include <sys/mman.h>
+/* makefile will include elf.h for us */
+
+#include "bswap.h"
+#include "dl-defs.h"
+
+#ifdef DMALLOC
+#include <dmalloc.h>
+#endif
+
+/* For SunOS */
+#ifndef PATH_MAX
+#define PATH_MAX _POSIX_PATH_MAX
+#endif
+
+#ifndef UCLIBC_RUNTIME_PREFIX
+# define UCLIBC_RUNTIME_PREFIX "/"
+#endif
+
+#define UCLIBC_ENDIAN_LITTLE 1234
+#define UCLIBC_ENDIAN_BIG    4321
+#if __BYTE_ORDER == __LITTLE_ENDIAN
+# define UCLIBC_ENDIAN_HOST UCLIBC_ENDIAN_LITTLE
+#elif __BYTE_ORDER == __BIG_ENDIAN
+# define UCLIBC_ENDIAN_HOST UCLIBC_ENDIAN_BIG
+#else
+# error "Unknown host byte order!"
+#endif
+
+#endif

+ 8 - 22
utils/readelf.c

@@ -13,19 +13,7 @@
  * Licensed under GPLv2 or later
  */
 
-
-#include <fcntl.h>
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <unistd.h>
-#include <sys/mman.h>
-#include <sys/stat.h>
-#include <sys/types.h>
-
-#include "bswap.h"
-#include "link.h"
-/* makefile will include elf.h for us */
+#include "porting.h"
 
 static int byteswap;
 static __inline__ uint32_t byteswap32_to_host(uint32_t value)
@@ -103,15 +91,13 @@ static int check_elf_header(ElfW(Ehdr) *const ehdr)
 
 	/* Check if the target endianness matches the host's endianness */
 	byteswap = 0;
-#if __BYTE_ORDER == __LITTLE_ENDIAN
-	if (ehdr->e_ident[5] == ELFDATA2MSB)
-		byteswap = 1;
-#elif __BYTE_ORDER == __BIG_ENDIAN
-	if (ehdr->e_ident[5] == ELFDATA2LSB)
-		byteswap = 1;
-#else
-#error Unknown host byte order!
-#endif
+	if (UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_LITTLE) {
+		if (ehdr->e_ident[5] == ELFDATA2MSB)
+			byteswap = 1;
+	} else if (UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_BIG) {
+		if (ehdr->e_ident[5] == ELFDATA2LSB)
+			byteswap = 1;
+	}
 	/* Be very lazy, and only byteswap the stuff we use */
 	if (byteswap) {
 		ehdr->e_type = bswap_16(ehdr->e_type);

+ 5 - 7
utils/readsoname2.c

@@ -35,13 +35,11 @@ char *readsonameXX(char *name, FILE *infile, int expected_type, int *type)
 	if ((char *)(epnt + 1) > (char *)(header + st.st_size))
 		goto skip;
 
-#if __BYTE_ORDER == __LITTLE_ENDIAN
-	byteswap = (epnt->e_ident[5] == ELFDATA2MSB) ? 1 : 0;
-#elif __BYTE_ORDER == __BIG_ENDIAN
-	byteswap = (epnt->e_ident[5] == ELFDATA2LSB) ? 1 : 0;
-#else
-#error Unknown host byte order!
-#endif
+	if (UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_LITTLE)
+		byteswap = (epnt->e_ident[5] == ELFDATA2MSB) ? 1 : 0;
+	else if (UCLIBC_ENDIAN_HOST == UCLIBC_ENDIAN_BIG)
+		byteswap = (epnt->e_ident[5] == ELFDATA2LSB) ? 1 : 0;
+
 	/* Be very lazy, and only byteswap the stuff we use */
 	if (byteswap == 1) {
 		epnt->e_phoff = bswap_32(epnt->e_phoff);