This is the mail archive of the binutils@sourceware.org mailing list for the binutils project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

[mach-o/committed]: preliminary work to support .section


Hi,

this is the first patch of a small set to improve the mach-o backend.

This one adds three new small functions that will be used to implement the .section pseudo-op.

Committed on trunk.

Tristan.

bfd/
2011-08-08  Tristan Gingold  <gingold@adacore.com>

	* mach-o.c (bfd_mach_o_normalize_section_name): New function.
	(bfd_mach_o_convert_section_name_to_bfd): Use it.
	(bfd_mach_o_get_section_type_from_name): New function.
	(bfd_mach_o_get_section_attribute_from_name): Ditto.
	* mach-o.h (bfd_mach_o_section): Move bfdsection field at the end.
	Add comments.  Add prototypes for the above new functions.

Index: mach-o.c
===================================================================
RCS file: /cvs/src/src/bfd/mach-o.c,v
retrieving revision 1.59
diff -c -r1.59 mach-o.c
*** mach-o.c	3 Aug 2011 08:12:07 -0000	1.59
--- mach-o.c	8 Aug 2011 07:37:05 -0000
***************
*** 144,187 ****
      { NULL, NULL }
    };
  
- 
  /* Mach-O to bfd names.  */
  
! static void
! bfd_mach_o_convert_section_name_to_bfd (bfd *abfd, bfd_mach_o_section *section,
!                                         char **name, flagword *flags)
  {
    const struct mach_o_segment_name_xlat *seg;
-   char *res;
-   unsigned int len;
-   const char *pfx = "";
  
    *name = NULL;
    *flags = SEC_NO_FLAGS;
  
    for (seg = segsec_names_xlat; seg->segname; seg++)
      {
!       if (strcmp (seg->segname, section->segname) == 0)
          {
            const struct mach_o_section_name_xlat *sec;
  
            for (sec = seg->sections; sec->mach_o_name; sec++)
              {
!               if (strcmp (sec->mach_o_name, section->sectname) == 0)
                  {
!                   len = strlen (sec->bfd_name);
!                   res = bfd_alloc (abfd, len + 1);
! 
!                   if (res == NULL)
!                     return;
!                   strcpy (res, sec->bfd_name);
!                   *name = res;
                    *flags = sec->flags;
                    return;
                  }
              }
          }
      }
  
    len = strlen (section->segname) + 1
      + strlen (section->sectname) + 1;
--- 144,195 ----
      { NULL, NULL }
    };
  
  /* Mach-O to bfd names.  */
  
! void
! bfd_mach_o_normalize_section_name (const char *segname, const char *sectname,
!                                    const char **name, flagword *flags)
  {
    const struct mach_o_segment_name_xlat *seg;
  
    *name = NULL;
    *flags = SEC_NO_FLAGS;
  
    for (seg = segsec_names_xlat; seg->segname; seg++)
      {
!       if (strcmp (seg->segname, segname) == 0)
          {
            const struct mach_o_section_name_xlat *sec;
  
            for (sec = seg->sections; sec->mach_o_name; sec++)
              {
!               if (strcmp (sec->mach_o_name, sectname) == 0)
                  {
!                   *name = sec->bfd_name;
                    *flags = sec->flags;
                    return;
                  }
              }
+           return;
          }
      }
+ }
+ 
+ static void
+ bfd_mach_o_convert_section_name_to_bfd (bfd *abfd, bfd_mach_o_section *section,
+                                         const char **name, flagword *flags)
+ {
+   char *res;
+   unsigned int len;
+   const char *pfx = "";
+ 
+   /* First search for a canonical name.  */
+   bfd_mach_o_normalize_section_name (section->segname, section->sectname,
+                                      name, flags);
+ 
+   /* Return now if found.  */
+   if (*name)
+     return;
  
    len = strlen (section->segname) + 1
      + strlen (section->sectname) + 1;
***************
*** 201,206 ****
--- 209,215 ----
      return;
    snprintf (res, len, "%s%s.%s", pfx, section->segname, section->sectname);
    *name = res;
+   *flags = SEC_NO_FLAGS;
  }
  
  /* Convert a bfd section name to a Mach-O segment + section name.  */
***************
*** 1496,1502 ****
  			     unsigned long prot)
  {
    asection *bfdsec;
!   char *sname;
    flagword flags;
  
    bfd_mach_o_convert_section_name_to_bfd (abfd, section, &sname, &flags);
--- 1505,1511 ----
  			     unsigned long prot)
  {
    asection *bfdsec;
!   const char *sname;
    flagword flags;
  
    bfd_mach_o_convert_section_name_to_bfd (abfd, section, &sname, &flags);
***************
*** 3404,3409 ****
--- 3413,3444 ----
    { NULL, 0}
  };
  
+ /* Get the section type from NAME.  Return -1 if NAME is unknown.  */
+ 
+ unsigned int
+ bfd_mach_o_get_section_type_from_name (const char *name)
+ {
+   bfd_mach_o_xlat_name *x;
+ 
+   for (x = bfd_mach_o_section_type_name; x->name; x++)
+     if (strcmp (x->name, name) == 0)
+       return x->val;
+   return (unsigned int)-1;
+ }
+ 
+ /* Get the section attribute from NAME.  Return -1 if NAME is unknown.  */
+ 
+ unsigned int
+ bfd_mach_o_get_section_attribute_from_name (const char *name)
+ {
+   bfd_mach_o_xlat_name *x;
+ 
+   for (x = bfd_mach_o_section_attribute_name; x->name; x++)
+     if (strcmp (x->name, name) == 0)
+       return x->val;
+   return (unsigned int)-1;
+ }
+ 
  static void
  bfd_mach_o_print_private_header (bfd *abfd, FILE *file)
  {
Index: mach-o.h
===================================================================
RCS file: /cvs/src/src/bfd/mach-o.h,v
retrieving revision 1.25
diff -c -r1.25 mach-o.h
*** mach-o.h	6 Jul 2011 07:22:20 -0000	1.25
--- mach-o.h	8 Aug 2011 07:37:05 -0000
***************
*** 46,52 ****
  
  typedef struct bfd_mach_o_section
  {
!   asection *bfdsection;
    char sectname[16 + 1];
    char segname[16 + 1];
    bfd_vma addr;
--- 46,52 ----
  
  typedef struct bfd_mach_o_section
  {
!   /* Fields present in the file.  */
    char sectname[16 + 1];
    char segname[16 + 1];
    bfd_vma addr;
***************
*** 59,64 ****
    unsigned long reserved1;
    unsigned long reserved2;
    unsigned long reserved3;
+ 
+   /* Corresponding bfd section.  */
+   asection *bfdsection;
  }
  bfd_mach_o_section;
  #define BFD_MACH_O_SECTION_SIZE 68
***************
*** 610,615 ****
--- 613,623 ----
                                               file_ptr, bfd_size_type);
  unsigned int bfd_mach_o_version (bfd *);
  
+ unsigned int bfd_mach_o_get_section_type_from_name (const char *);
+ unsigned int bfd_mach_o_get_section_attribute_from_name (const char *);
+ void bfd_mach_o_normalize_section_name (const char *, const char *,
+                                         const char **, flagword *);
+ 
  extern const bfd_target mach_o_fat_vec;
  
  #endif /* _BFD_MACH_O_H_ */


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]