[PATCH] Make the compiler do the math 2.

Nick Clifton nickc@redhat.com
Tue Sep 26 08:49:00 GMT 2006


Hi Andreas,

> How about just using strcpy?  

strcpy also copies the NUL byte at the end of the string, which may not 
be desired in all circumstances, (although in this particular case it is 
OK).

> Also, the surrounding code has too many
> occurrences of magic numbers that should be replaced. 

A very good idea.

> Like this perhaps:

> --- bfd/elflink.c.~1.230.~	2006-09-18 10:58:08.000000000 +0200
> +++ bfd/elflink.c	2006-09-25 22:34:35.000000000 +0200
> @@ -9699,13 +9699,16 @@ bfd_elf_gc_sections (bfd *abfd, struct b
>  		unsigned long len;
>  		char *fn_name;
>  		asection *fn_text;
> +		int o_name_prefix_len = strlen (".gcc_except_table.");
> +		int fn_name_prefix_len = strlen (".text.");
>  
> -		len = strlen (o->name + 18) + 1;
> -		fn_name = bfd_malloc (len + 6);
> +		len = strlen (o->name + o_name_prefix_len) + 1;
> +		fn_name = bfd_malloc (len + fn_name_prefix_len);
>  		if (fn_name == NULL)
>  		  return FALSE;
> -		memcpy (fn_name, STRING_COMMA_LEN (".text."));
> -		memcpy (fn_name + 6, o->name + 18, len);
> +		strcpy (fn_name, ".text.");
> +		memcpy (fn_name + fn_name_prefix_len,
> +			o->name + o_name_prefix_len, len);
>  		fn_text = bfd_get_section_by_name (sub, fn_name);
>  		free (fn_name);
>  		if (fn_text == NULL || !fn_text->gc_mark)

I like this, although for my money I think that we would be better off 
replacing the multiple occurrences of the same constant string with a 
#define, just to make it clear that we are intentionally dealing with 
the same piece of text.  eg:

   #define DOT_TEXT_DOT ".text."
   ...
   int fn_name_prefix_len = sizeof (DOT_TEXT_DOT) - 1;
   ...
   strcpy (fn_name, DOT_TEXT_DOT);

Cheers
   Nick



More information about the Binutils mailing list