--- objcopy.c 4 Dec 2002 18:22:19 -0000 +++ objcopy.c 4 Dec 2002 18:23:58 -0000 @@ -235,12 +235,17 @@ static struct symlist *weaken_specific_list = NULL; static struct redefine_node *redefine_sym_list = NULL; /* If this is TRUE, we weaken global symbols (set BSF_WEAK). */ static bfd_boolean weaken = FALSE; +/* Prefix symbols/sections. */ +static char *prefix_symbols_string = 0; +static char *prefix_sections_string = 0; +static char *prefix_alloc_sections_string = 0; + /* 150 isn't special; it's just an arbitrary non-ASCII char value. */ #define OPTION_ADD_SECTION 150 #define OPTION_CHANGE_ADDRESSES (OPTION_ADD_SECTION + 1) #define OPTION_CHANGE_LEADING_CHAR (OPTION_CHANGE_ADDRESSES + 1) #define OPTION_CHANGE_START (OPTION_CHANGE_LEADING_CHAR + 1) @@ -264,12 +269,15 @@ #define OPTION_KEEP_SYMBOLS (OPTION_STRIP_SYMBOLS + 1) #define OPTION_LOCALIZE_SYMBOLS (OPTION_KEEP_SYMBOLS + 1) #define OPTION_KEEPGLOBAL_SYMBOLS (OPTION_LOCALIZE_SYMBOLS + 1) #define OPTION_WEAKEN_SYMBOLS (OPTION_KEEPGLOBAL_SYMBOLS + 1) #define OPTION_RENAME_SECTION (OPTION_WEAKEN_SYMBOLS + 1) #define OPTION_ALT_MACH_CODE (OPTION_RENAME_SECTION + 1) +#define OPTION_PREFIX_SYMBOLS (OPTION_ALT_MACH_CODE + 1) +#define OPTION_PREFIX_SECTIONS (OPTION_PREFIX_SYMBOLS + 1) +#define OPTION_PREFIX_ALLOC_SECTIONS (OPTION_PREFIX_SECTIONS + 1) /* Options to handle if running as "strip". */ static struct option strip_options[] = { {"discard-all", no_argument, 0, 'x'}, @@ -351,12 +359,15 @@ {"keep-symbols", required_argument, 0, OPTION_KEEP_SYMBOLS}, {"strip-symbols", required_argument, 0, OPTION_STRIP_SYMBOLS}, {"keep-global-symbols", required_argument, 0, OPTION_KEEPGLOBAL_SYMBOLS}, {"localize-symbols", required_argument, 0, OPTION_LOCALIZE_SYMBOLS}, {"weaken-symbols", required_argument, 0, OPTION_WEAKEN_SYMBOLS}, {"alt-machine-code", required_argument, 0, OPTION_ALT_MACH_CODE}, + {"prefix-symbols", required_argument, 0, OPTION_PREFIX_SYMBOLS}, + {"prefix-sections", required_argument, 0, OPTION_PREFIX_SECTIONS}, + {"prefix-alloc-sections", required_argument, 0, OPTION_PREFIX_ALLOC_SECTIONS}, {0, no_argument, 0, 0} }; /* IMPORTS */ extern char *program_name; @@ -435,12 +446,17 @@ --strip-symbols -N for all symbols listed in \n\ --keep-symbols -K for all symbols listed in \n\ --localize-symbols -L for all symbols listed in \n\ --keep-global-symbols -G for all symbols listed in \n\ --weaken-symbols -W for all symbols listed in \n\ --alt-machine-code Use alternate machine code for output\n\ + --prefix-symbols Add to start of every symbol name\n\ + --prefix-sections Add to start of every section name\n\ + --prefix-alloc-sections \n\ + Add to start of every allocatable\n\ + section name\n\ -v --verbose List all object files modified\n\ -V --version Display this program's version number\n\ -h --help Display this output\n\ ")); list_supported_targets (program_name, stream); if (exit_status == 0) @@ -768,54 +784,74 @@ { asymbol *sym = from[src_count]; flagword flags = sym->flags; const char *name = bfd_asymbol_name (sym); int keep; bfd_boolean undefined; + bfd_boolean remove_leading_char; + bfd_boolean add_leading_char; + + undefined = bfd_is_und_section (bfd_get_section (sym)); if (redefine_sym_list) { const char *old_name, *new_name; old_name = bfd_asymbol_name (sym); new_name = lookup_sym_redefinition (old_name); name = bfd_asymbol_name (sym) = new_name; } - if (change_leading_char - && (bfd_get_symbol_leading_char (abfd) - != bfd_get_symbol_leading_char (obfd)) + /* Check if we will remove the old leading char. */ + remove_leading_char = (name[0] == bfd_get_symbol_leading_char (abfd)) + && (change_leading_char + || (remove_leading_char + && ((flags & BSF_GLOBAL) != 0 + || (flags & BSF_WEAK) != 0 + || undefined + || bfd_is_com_section (bfd_get_section (sym))))); + + /* Check if we will add a new leading char. */ + add_leading_char = change_leading_char + && (bfd_get_symbol_leading_char (obfd) != '\0') && (bfd_get_symbol_leading_char (abfd) == '\0' - || (name[0] == bfd_get_symbol_leading_char (abfd)))) - { - if (bfd_get_symbol_leading_char (obfd) == '\0') - name = bfd_asymbol_name (sym) = name + 1; - else - { - char *n; + || (name[0] == bfd_get_symbol_leading_char (abfd))); - n = xmalloc (strlen (name) + 2); - n[0] = bfd_get_symbol_leading_char (obfd); - if (bfd_get_symbol_leading_char (abfd) == '\0') - strcpy (n + 1, name); - else - strcpy (n + 1, name + 1); - name = bfd_asymbol_name (sym) = n; - } + /* Short circuit for change_leading_char if we can do it in-place. */ + if (remove_leading_char && add_leading_char && !prefix_symbols_string) + { + name[0] = bfd_get_symbol_leading_char (obfd); + bfd_asymbol_name (sym) = name; + remove_leading_char = FALSE; + add_leading_char = FALSE; + } + + /* Remove leading char. */ + if (remove_leading_char) + { + name = bfd_asymbol_name (sym) = name + 1; + } + + /* Add new leading char and/or prefix. */ + if (add_leading_char || prefix_symbols_string) + { + char *n, *ptr; + ptr = n = xmalloc ((add_leading_char ? 1 : 0) + strlen (prefix_symbols_string) + strlen (name) + 1); + if (add_leading_char) + { + *ptr++ = bfd_get_symbol_leading_char (obfd); + } + if (prefix_symbols_string) + { + strcpy (ptr, prefix_symbols_string); + ptr += strlen(prefix_symbols_string); + } + strcpy (ptr, name); + name = bfd_asymbol_name (sym) = n; } - undefined = bfd_is_und_section (bfd_get_section (sym)); - - if (remove_leading_char - && ((flags & BSF_GLOBAL) != 0 - || (flags & BSF_WEAK) != 0 - || undefined - || bfd_is_com_section (bfd_get_section (sym))) - && name[0] == bfd_get_symbol_leading_char (abfd)) - name = bfd_asymbol_name (sym) = name + 1; - if (strip_symbols == STRIP_ALL) keep = 0; else if ((flags & BSF_KEEP) != 0 /* Used in relocation. */ || ((flags & BSF_SECTION_SYM) != 0 && ((*bfd_get_section (sym)->symbol_ptr_ptr)->flags & BSF_KEEP) != 0)) @@ -1179,13 +1215,14 @@ || sections_removed || sections_copied || convert_debugging || change_leading_char || remove_leading_char || redefine_sym_list - || weaken) + || weaken + || prefix_symbols_string) { /* Mark symbols used in output relocations so that they are kept, even if they are local labels or static symbols. Note we iterate over the input sections examining their relocations since the relocations for the output sections @@ -1595,13 +1632,14 @@ sec_ptr osection; bfd_size_type size; bfd_vma vma; bfd_vma lma; flagword flags; const char *err; - const char * name; + const char *name; + char *prefix = NULL; if ((bfd_get_section_flags (ibfd, isection) & SEC_DEBUGGING) != 0 && (strip_symbols == STRIP_DEBUG || strip_symbols == STRIP_UNNEEDED || strip_symbols == STRIP_ALL || discard_locals == LOCALS_ALL @@ -1617,12 +1655,26 @@ if (sections_copied && (p == NULL || ! p->copy)) return; /* Get the, possibly new, name of the output section. */ name = find_section_rename (ibfd, isection, & flags); + /* Prefix sections. */ + if ((prefix_alloc_sections_string) && (bfd_get_section_flags (ibfd, isection) & SEC_ALLOC)) + prefix = prefix_alloc_sections_string; + else if (prefix_sections_string) + prefix = prefix_sections_string; + if (prefix) + { + char *n; + n = xmalloc (strlen (prefix) + strlen (name) + 1); + strcpy (n, prefix); + strcat (n, name); + name = n; + } + osection = bfd_make_section_anyway (obfd, name); if (osection == NULL) { err = _("making"); goto loser; @@ -2571,12 +2623,23 @@ break; case OPTION_ALT_MACH_CODE: use_alt_mach_code = atoi (optarg); if (use_alt_mach_code <= 0) fatal (_("alternate machine code index must be positive")); + break; + case OPTION_PREFIX_SYMBOLS: + prefix_symbols_string = optarg; + break; + + case OPTION_PREFIX_SECTIONS: + prefix_sections_string = optarg; + break; + + case OPTION_PREFIX_ALLOC_SECTIONS: + prefix_alloc_sections_string = optarg; break; case 0: break; /* we've been given a long option */ case 'H':