View | Details | Raw Unified | Return to bug 6765
Collapse All | Expand All

(-)objcopy.c (-2 / +152 lines)
Lines 47-52 Link Here
47
  struct redefine_node *next;
47
  struct redefine_node *next;
48
};
48
};
49
49
50
struct unbind_node
51
{
52
  char *sym;
53
  char *ref_name;
54
  char *def_name;
55
  struct unbind_node *next;
56
};
57
50
typedef struct section_rename
58
typedef struct section_rename
51
{
59
{
52
  const char *            old_name;
60
  const char *            old_name;
Lines 170-175 Link Here
170
  asection *section;
178
  asection *section;
171
};
179
};
172
180
181
/* List of symbols to add. */
182
struct symbol_add
183
{
184
  struct symbol_add *next;
185
  asymbol *sp;
186
};
187
173
/* List of sections to add to the output BFD.  */
188
/* List of sections to add to the output BFD.  */
174
static struct section_add *add_sections;
189
static struct section_add *add_sections;
175
190
Lines 202-207 Link Here
202
static htab_t keepglobal_specific_htab = NULL;
217
static htab_t keepglobal_specific_htab = NULL;
203
static htab_t weaken_specific_htab = NULL;
218
static htab_t weaken_specific_htab = NULL;
204
static struct redefine_node *redefine_sym_list = NULL;
219
static struct redefine_node *redefine_sym_list = NULL;
220
static struct unbind_node *unbind_sym_list = NULL;
205
221
206
/* If this is TRUE, we weaken global symbols (set BSF_WEAK).  */
222
/* If this is TRUE, we weaken global symbols (set BSF_WEAK).  */
207
static bfd_boolean weaken = FALSE;
223
static bfd_boolean weaken = FALSE;
Lines 244-249 Link Here
244
    OPTION_WEAKEN,
260
    OPTION_WEAKEN,
245
    OPTION_REDEFINE_SYM,
261
    OPTION_REDEFINE_SYM,
246
    OPTION_REDEFINE_SYMS,
262
    OPTION_REDEFINE_SYMS,
263
    OPTION_UNBIND_SYM,
247
    OPTION_SREC_LEN,
264
    OPTION_SREC_LEN,
248
    OPTION_SREC_FORCES3,
265
    OPTION_SREC_FORCES3,
249
    OPTION_STRIP_SYMBOLS,
266
    OPTION_STRIP_SYMBOLS,
Lines 360-365 Link Here
360
  {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
377
  {"readonly-text", no_argument, 0, OPTION_READONLY_TEXT},
361
  {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
378
  {"redefine-sym", required_argument, 0, OPTION_REDEFINE_SYM},
362
  {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
379
  {"redefine-syms", required_argument, 0, OPTION_REDEFINE_SYMS},
380
  {"unbind-sym", required_argument, 0, OPTION_UNBIND_SYM},
363
  {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
381
  {"remove-leading-char", no_argument, 0, OPTION_REMOVE_LEADING_CHAR},
364
  {"remove-section", required_argument, 0, 'R'},
382
  {"remove-section", required_argument, 0, 'R'},
365
  {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
383
  {"rename-section", required_argument, 0, OPTION_RENAME_SECTION},
Lines 417-422 Link Here
417
static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
435
static void mark_symbols_used_in_relocations (bfd *, asection *, void *);
418
static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
436
static bfd_boolean write_debugging_info (bfd *, void *, long *, asymbol ***);
419
static const char *lookup_sym_redefinition (const char *);
437
static const char *lookup_sym_redefinition (const char *);
438
static const char *get_sym_unbound_ref_name (const char *);
439
static const char *get_sym_unbound_def_name (const char *);
420
440
421
static void
441
static void
422
copy_usage (FILE *stream, int exit_status)
442
copy_usage (FILE *stream, int exit_status)
Lines 481-486 Link Here
481
     --redefine-sym <old>=<new>    Redefine symbol name <old> to <new>\n\
501
     --redefine-sym <old>=<new>    Redefine symbol name <old> to <new>\n\
482
     --redefine-syms <file>        --redefine-sym for all symbol pairs \n\
502
     --redefine-syms <file>        --redefine-sym for all symbol pairs \n\
483
                                     listed in <file>\n\
503
                                     listed in <file>\n\
504
     --unbind-sym <sym>            Separate <sym> into  __def_<sym> and __ref<sym>\n\
484
     --srec-len <number>           Restrict the length of generated Srecords\n\
505
     --srec-len <number>           Restrict the length of generated Srecords\n\
485
     --srec-forceS3                Restrict the type of generated Srecords to S3\n\
506
     --srec-forceS3                Restrict the type of generated Srecords to S3\n\
486
     --strip-symbols <file>        -N for all symbols listed in <file>\n\
507
     --strip-symbols <file>        -N for all symbols listed in <file>\n\
Lines 947-952 Link Here
947
{
968
{
948
  asymbol **from = isyms, **to = osyms;
969
  asymbol **from = isyms, **to = osyms;
949
  long src_count = 0, dst_count = 0;
970
  long src_count = 0, dst_count = 0;
971
  struct symbol_add *symbol_add_list = NULL;
972
  struct symbol_add *sa_ptr; /* iterator for loop */
950
  int relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
973
  int relocatable = (abfd->flags & (EXEC_P | DYNAMIC)) == 0;
951
974
952
  for (; src_count < symcount; src_count++)
975
  for (; src_count < symcount; src_count++)
Lines 971-976 Link Here
971
	  bfd_asymbol_name (sym) = new_name;
994
	  bfd_asymbol_name (sym) = new_name;
972
	  name = new_name;
995
	  name = new_name;
973
	}
996
	}
997
	
998
      if (unbind_sym_list)
999
	{
1000
	  char *old_name, *new_name;
1001
1002
	  old_name = (char *) bfd_asymbol_name (sym);
1003
	  new_name = (char *) get_sym_unbound_ref_name (old_name);
1004
	  bfd_asymbol_name (sym) = new_name;
1005
	  name = new_name;
1006
          
1007
          if (new_name != old_name && !undefined)
1008
            {
1009
              /* We have renamed a symbol which must be unbound.
1010
               * Create a new symbol which is a copy of this one,
1011
               * renamed, and modify this one so that it is 
1012
               * undefined. We will add the new symbols later,
1013
               * but keep them in a list for now. */
1014
               
1015
              struct symbol_add *sa;
1016
              sa = xmalloc(sizeof (struct symbol_add));
1017
              /* Make a new symbol. */
1018
              sa->sp = bfd_make_empty_symbol (abfd);
1019
              /* Set name appropriately. */
1020
              bfd_asymbol_name (sa->sp) = get_sym_unbound_def_name (old_name);
1021
              /* Set section, flags, value as in existing symbol. */
1022
              bfd_set_section(sa->sp, bfd_get_section(sym));
1023
              sa->sp->flags = sym->flags;
1024
              sa->sp->value = sym->value;
1025
              /* Copy private data. */
1026
              bfd_copy_private_symbol_data(abfd, sym, obfd, sa->sp);
1027
              /* Add the new symbol to the list of new symbols. */
1028
              sa->next = symbol_add_list;
1029
              symbol_add_list = sa;
1030
              
1031
              /* Update the old symbol's flags, section and value. */
1032
              bfd_set_section(sym, bfd_und_section_ptr);
1033
              sym->value = 0;
1034
              sym->flags &= ~(BSF_LOCAL | BSF_GLOBAL | BSF_EXPORT | BSF_WEAK);
1035
              
1036
              /* The current symbol has now been made undefined. */
1037
              undefined = 1;
1038
           }
1039
	}
974
1040
975
      /* Check if we will remove the current leading character.  */
1041
      /* Check if we will remove the current leading character.  */
976
      rem_leading_char =
1042
      rem_leading_char =
Lines 1118-1123 Link Here
1118
	}
1184
	}
1119
    }
1185
    }
1120
1186
1187
  for (sa_ptr = symbol_add_list; sa_ptr != NULL; sa_ptr = sa_ptr->next)
1188
    to[dst_count++] = sa_ptr->sp;
1189
1121
  to[dst_count] = NULL;
1190
  to[dst_count] = NULL;
1122
1191
1123
  return dst_count;
1192
  return dst_count;
Lines 1137-1142 Link Here
1137
  return source;
1206
  return source;
1138
}
1207
}
1139
1208
1209
/* Find the name to which the references to SOURCE should be rewritten */
1210
1211
static const char *
1212
get_sym_unbound_ref_name (const char *sym)
1213
{
1214
  struct unbind_node *list;
1215
1216
  for (list = unbind_sym_list; list != NULL; list = list->next)
1217
    if (strcmp (sym, list->sym) == 0)
1218
      {
1219
        return list->ref_name;
1220
      }
1221
1222
  return sym;
1223
}
1224
1225
/* Find the name to which the definition of SOURCE should be rewritten */
1226
1227
static const char *
1228
get_sym_unbound_def_name (const char *sym)
1229
{
1230
  struct unbind_node *list;
1231
1232
  for (list = unbind_sym_list; list != NULL; list = list->next)
1233
    if (strcmp (sym, list->sym) == 0)
1234
      {
1235
        return list->def_name;
1236
      }
1237
1238
  return sym;
1239
}
1240
1140
/* Add a node to a symbol redefine list.  */
1241
/* Add a node to a symbol redefine list.  */
1141
1242
1142
static void
1243
static void
Lines 1166-1171 Link Here
1166
  *p = new_node;
1267
  *p = new_node;
1167
}
1268
}
1168
1269
1270
static void
1271
unbind_list_append (const char *cause, const char *sym)
1272
{
1273
  struct unbind_node **p;
1274
  struct unbind_node *list;
1275
  struct unbind_node *new_node;
1276
  const char def_prefix[] = "__def_";
1277
  const char ref_prefix[] = "__ref_";
1278
  size_t sym_len = strlen(sym);
1279
1280
  for (p = &unbind_sym_list; (list = *p) != NULL; p = &list->next)
1281
    {
1282
      if (strcmp (sym, list->sym) == 0)
1283
	fatal (_("%s: Multiple unbinding of symbol \"%s\""),
1284
	       cause, sym);
1285
    }
1286
1287
  new_node = xmalloc (sizeof (struct unbind_node));
1288
1289
  new_node->sym = strdup (sym);
1290
  
1291
  new_node->def_name = xmalloc (sizeof def_prefix + sym_len);
1292
  strncpy(new_node->def_name, def_prefix, sizeof def_prefix - 1);
1293
  strncpy(new_node->def_name + sizeof def_prefix - 1, sym, sym_len + 1);
1294
  
1295
  new_node->ref_name = xmalloc (sizeof ref_prefix + strlen(sym));
1296
  strncpy(new_node->ref_name, ref_prefix, sizeof ref_prefix - 1);
1297
  strncpy(new_node->ref_name + sizeof ref_prefix - 1, sym, sym_len + 1);
1298
  
1299
  new_node->next = NULL;
1300
1301
  *p = new_node;
1302
}
1303
1169
/* Handle the --redefine-syms option.  Read lines containing "old new"
1304
/* Handle the --redefine-syms option.  Read lines containing "old new"
1170
   from the file, and add them to the symbol redefine list.  */
1305
   from the file, and add them to the symbol redefine list.  */
1171
1306
Lines 1695-1700 Link Here
1695
      || change_leading_char
1830
      || change_leading_char
1696
      || remove_leading_char
1831
      || remove_leading_char
1697
      || redefine_sym_list
1832
      || redefine_sym_list
1833
      || unbind_sym_list
1698
      || weaken)
1834
      || weaken)
1699
    {
1835
    {
1700
      /* Mark symbols used in output relocations so that they
1836
      /* Mark symbols used in output relocations so that they
Lines 1705-1715 Link Here
1705
	 haven't been set yet.  mark_symbols_used_in_relocations will
1841
	 haven't been set yet.  mark_symbols_used_in_relocations will
1706
	 ignore input sections which have no corresponding output
1842
	 ignore input sections which have no corresponding output
1707
	 section.  */
1843
	 section.  */
1844
      
1845
      int added_syms_upper_bound = 0;
1846
      struct unbind_node *n;
1847
      if (unbind_sym_list)
1848
        for (n = unbind_sym_list; n->next != NULL; n = n->next)
1849
          ++added_syms_upper_bound;
1850
      
1708
      if (strip_symbols != STRIP_ALL)
1851
      if (strip_symbols != STRIP_ALL)
1709
	bfd_map_over_sections (ibfd,
1852
	bfd_map_over_sections (ibfd,
1710
			       mark_symbols_used_in_relocations,
1853
			       mark_symbols_used_in_relocations,
1711
			       isympp);
1854
			       isympp);
1712
      osympp = xmalloc ((symcount + 1) * sizeof (asymbol *));
1855
      osympp = xmalloc ((symcount + added_syms_upper_bound + 1) * sizeof (asymbol *));
1713
      symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1856
      symcount = filter_symbols (ibfd, obfd, osympp, isympp, symcount);
1714
    }
1857
    }
1715
1858
Lines 3228-3234 Link Here
3228
	case OPTION_REDEFINE_SYMS:
3371
	case OPTION_REDEFINE_SYMS:
3229
	  add_redefine_syms_file (optarg);
3372
	  add_redefine_syms_file (optarg);
3230
	  break;
3373
	  break;
3231
3374
	
3375
	case OPTION_UNBIND_SYM:
3376
	  {
3377
	    /* Push this unbinding onto unbind_symbol_list.  */
3378
	    unbind_list_append("--unbind-sym", optarg);
3379
	  }	
3380
	  break;
3381
	
3232
	case OPTION_SET_SECTION_FLAGS:
3382
	case OPTION_SET_SECTION_FLAGS:
3233
	  {
3383
	  {
3234
	    const char *s;
3384
	    const char *s;

Return to bug 6765