x86: Regex support and known-movable symbols for relocs, fix _end
authorH. Peter Anvin <hpa@zytor.com>
Mon, 14 Dec 2009 21:55:20 +0000 (13:55 -0800)
committerH. Peter Anvin <hpa@zytor.com>
Mon, 14 Dec 2009 21:55:20 +0000 (13:55 -0800)
This adds a new category of symbols to the relocs program: symbols
which are known to be relative, even though the linker emits them as
absolute; this is the case for symbols that live in the linker script,
which currently applies to _end.

Unfortunately the previous workaround of putting _end in its own empty
section was defeated by newer binutils, which remove empty sections
completely.

This patch also changes the symbol matching to use regular expressions
instead of hardcoded C for specific patterns.

This is a decidedly non-minimal patch: a modified version of the
relocs program is used as part of the Syslinux build, and this  is
basically a backport to Linux of some of those changes; they have
thus been well tested.

Signed-off-by: H. Peter Anvin <hpa@zytor.com>
LKML-Reference: <4AF86211.3070103@zytor.com>
Acked-by: Michal Marek <mmarek@suse.cz>
Tested-by: Sedat Dilek <sedat.dilek@gmail.com>
arch/x86/boot/compressed/relocs.c
arch/x86/kernel/vmlinux.lds.S

index bbeb0c3fbd90e7ba875c583ab3e2e57cca216714..89bbf4e4d05d834c7923a23de080b71dd5ebab24 100644 (file)
@@ -9,6 +9,9 @@
 #include <byteswap.h>
 #define USE_BSD
 #include <endian.h>
+#include <regex.h>
+
+static void die(char *fmt, ...);
 
 #define ARRAY_SIZE(x) (sizeof(x) / sizeof((x)[0]))
 static Elf32_Ehdr ehdr;
@@ -30,25 +33,47 @@ static struct section *secs;
  * the address for which it has been compiled. Don't warn user about
  * absolute relocations present w.r.t these symbols.
  */
-static const char* safe_abs_relocs[] = {
-               "xen_irq_disable_direct_reloc",
-               "xen_save_fl_direct_reloc",
-};
+static const char abs_sym_regex[] =
+       "^(xen_irq_disable_direct_reloc$|"
+       "xen_save_fl_direct_reloc$|"
+       "VDSO|"
+       "__crc_)";
+static regex_t abs_sym_regex_c;
+static int is_abs_reloc(const char *sym_name)
+{
+       return !regexec(&abs_sym_regex_c, sym_name, 0, NULL, 0);
+}
 
-static int is_safe_abs_reloc(const char* sym_name)
+/*
+ * These symbols are known to be relative, even if the linker marks them
+ * as absolute (typically defined outside any section in the linker script.)
+ */
+static const char rel_sym_regex[] =
+       "^_end$";
+static regex_t rel_sym_regex_c;
+static int is_rel_reloc(const char *sym_name)
 {
-       int i;
+       return !regexec(&rel_sym_regex_c, sym_name, 0, NULL, 0);
+}
 
-       for (i = 0; i < ARRAY_SIZE(safe_abs_relocs); i++) {
-               if (!strcmp(sym_name, safe_abs_relocs[i]))
-                       /* Match found */
-                       return 1;
-       }
-       if (strncmp(sym_name, "VDSO", 4) == 0)
-               return 1;
-       if (strncmp(sym_name, "__crc_", 6) == 0)
-               return 1;
-       return 0;
+static void regex_init(void)
+{
+        char errbuf[128];
+        int err;
+       
+        err = regcomp(&abs_sym_regex_c, abs_sym_regex,
+                      REG_EXTENDED|REG_NOSUB);
+        if (err) {
+                regerror(err, &abs_sym_regex_c, errbuf, sizeof errbuf);
+                die("%s", errbuf);
+        }
+
+        err = regcomp(&rel_sym_regex_c, rel_sym_regex,
+                      REG_EXTENDED|REG_NOSUB);
+        if (err) {
+                regerror(err, &rel_sym_regex_c, errbuf, sizeof errbuf);
+                die("%s", errbuf);
+        }
 }
 
 static void die(char *fmt, ...)
@@ -131,7 +156,7 @@ static const char *rel_type(unsigned type)
 #undef REL_TYPE
        };
        const char *name = "unknown type rel type name";
-       if (type < ARRAY_SIZE(type_name)) {
+       if (type < ARRAY_SIZE(type_name) && type_name[type]) {
                name = type_name[type];
        }
        return name;
@@ -448,7 +473,7 @@ static void print_absolute_relocs(void)
                         * Before warning check if this absolute symbol
                         * relocation is harmless.
                         */
-                       if (is_safe_abs_reloc(name))
+                       if (is_abs_reloc(name) || is_rel_reloc(name))
                                continue;
 
                        if (!printed) {
@@ -501,21 +526,26 @@ static void walk_relocs(void (*visit)(Elf32_Rel *rel, Elf32_Sym *sym))
                        sym = &sh_symtab[ELF32_R_SYM(rel->r_info)];
                        r_type = ELF32_R_TYPE(rel->r_info);
                        /* Don't visit relocations to absolute symbols */
-                       if (sym->st_shndx == SHN_ABS) {
+                       if (sym->st_shndx == SHN_ABS &&
+                           !is_rel_reloc(sym_name(sym_strtab, sym))) {
                                continue;
                        }
-                       if (r_type == R_386_NONE || r_type == R_386_PC32) {
+                       switch (r_type) {
+                       case R_386_NONE:
+                       case R_386_PC32:
                                /*
                                 * NONE can be ignored and and PC relative
                                 * relocations don't need to be adjusted.
                                 */
-                       }
-                       else if (r_type == R_386_32) {
+                               break;
+                       case R_386_32:
                                /* Visit relocations that need to be adjusted */
                                visit(rel, sym);
-                       }
-                       else {
-                               die("Unsupported relocation type: %d\n", r_type);
+                               break;
+                       default:
+                               die("Unsupported relocation type: %s (%d)\n",
+                                   rel_type(r_type), r_type);
+                               break;
                        }
                }
        }
@@ -571,16 +601,15 @@ static void emit_relocs(int as_text)
        }
        else {
                unsigned char buf[4];
-               buf[0] = buf[1] = buf[2] = buf[3] = 0;
                /* Print a stop */
-               printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
+               fwrite("\0\0\0\0", 4, 1, stdout);
                /* Now print each relocation */
                for (i = 0; i < reloc_count; i++) {
                        buf[0] = (relocs[i] >>  0) & 0xff;
                        buf[1] = (relocs[i] >>  8) & 0xff;
                        buf[2] = (relocs[i] >> 16) & 0xff;
                        buf[3] = (relocs[i] >> 24) & 0xff;
-                       printf("%c%c%c%c", buf[0], buf[1], buf[2], buf[3]);
+                       fwrite(buf, 4, 1, stdout);
                }
        }
 }
@@ -598,6 +627,8 @@ int main(int argc, char **argv)
        FILE *fp;
        int i;
 
+       regex_init();
+
        show_absolute_syms = 0;
        show_absolute_relocs = 0;
        as_text = 0;
index f3f2104408d932ab05172fe4edb42cba420ec7d4..f92a0da608cb3ade16374118320e4d73220e4b95 100644 (file)
@@ -319,9 +319,7 @@ SECTIONS
                __brk_limit = .;
        }
 
-       .end : AT(ADDR(.end) - LOAD_OFFSET) {
-               _end = .;
-       }
+       _end = .;
 
         STABS_DEBUG
         DWARF_DEBUG