[PATCH 10/10] A simple demonstrational program that looks up symbols in sparse object files.

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



From: Alexey Zaytsev <alexey.zaytsev@xxxxxxxxx>

Signed-off-by: Alexey Zaytsev <alexey.zaytsev@xxxxxxxxx>
---
 Makefile |    6 +++++-
 where.c  |   61 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 66 insertions(+), 1 deletions(-)
 create mode 100644 where.c

diff --git a/Makefile b/Makefile
index 877634c..446e053 100644
--- a/Makefile
+++ b/Makefile
@@ -26,7 +26,7 @@ INCLUDEDIR=$(PREFIX)/include
 PKGCONFIGDIR=$(LIBDIR)/pkgconfig
 
 PROGRAMS=test-lexing test-parsing obfuscate compile graph sparse test-linearize example \
-	 test-unssa test-dissect ctags serialization-test sold
+	 test-unssa test-dissect ctags serialization-test sold where
 
 
 INST_PROGRAMS=sparse sold cgcc cld car
@@ -141,6 +141,9 @@ serialization-test: serialization-test.o $(LIBS)
 sold: sold.o $(LIBS)
 	$(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $< $(LIBS) -ldl
 
+where: where.o $(LIBS)
+	$(QUIET_LINK)$(CC) $(LDFLAGS) -o $@ $< $(LIBS) -ldl
+
 $(LIB_FILE): $(LIB_OBJS)
 	$(QUIET_AR)$(AR) rcs $@ $(LIB_OBJS)
 
@@ -195,6 +198,7 @@ serialization.o: $(LIB_H)
 serialization-test.o: $(LIB_H)
 link.o: $(LIB_H)
 sold.o: $(LIB_H)
+where: $(LIB_H)
 
 pre-process.h:
 	$(QUIET_GEN)echo "#define GCC_INTERNAL_INCLUDE \"`$(CC) -print-file-name=`\"" > pre-process.h
diff --git a/where.c b/where.c
new file mode 100644
index 0000000..03241f9
--- /dev/null
+++ b/where.c
@@ -0,0 +1,61 @@
+
+#include <stdio.h>
+#include <dlfcn.h>
+#include <stdlib.h>
+#include <limits.h>
+
+#include "lib.h"
+#include "link.h"
+
+int main(int argc, char **argv)
+{
+	void *handle;
+	char *err;
+	struct sold_symbol *sym;
+	struct sold_symbol_list **symbols;
+	const char sym_type_tbl[] = {'F', 'D', 'O'};
+	char input[PATH_MAX];
+
+
+	if (argc < 2) {
+		fprintf(stderr, "usage: %s <sparse_object_file> [symbol_name]\n", argv[0]);
+		exit(1);
+	}
+
+	snprintf(input, PATH_MAX, "./%s", argv[1]);
+
+	dlerror();
+	handle = dlopen(input, RTLD_NOW);
+	if (!handle) {
+		fprintf(stderr, "%s: Can't open input file %s: %s\n",
+			argv[0], input, dlerror());
+		exit(1);
+	}
+
+	symbols = dlsym(handle, "symbols");
+	err = dlerror();
+	if (!symbols) {
+		if (err) {
+			fprintf(stderr, "%s: Can't process the input file %s: %s\n",
+				argv[0], input, err);
+			exit(1);
+		}
+		/* empty symbol list. */
+		exit(0);
+	}
+
+	if (argc > 2) {/* Look up the symbol */
+		FOR_EACH_PTR(*symbols, sym) {
+			if (!strcmp(argv[2], sym->name))
+				printf("%c %s %s\n", sym_type_tbl[sym->type],
+					sym->name, sym->source->source);
+		} END_FOR_EACH_PTR(sym);
+	} else {/* Just list all symbols */
+		FOR_EACH_PTR(*symbols, sym) {
+			printf("%c %s %s\n", sym_type_tbl[sym->type],
+				sym->name, sym->source->source);
+		} END_FOR_EACH_PTR(sym);
+	}
+
+	return 0;
+}
-- 
1.5.6.3

--
To unsubscribe from this list: send the line "unsubscribe linux-sparse" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html

[Index of Archives]     [Newbies FAQ]     [LKML]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Trinity Fuzzer Tool]

  Powered by Linux