[Patch] Mach-O: add write support for symbols

Tristan Gingold gingold@adacore.com
Mon Jun 8 12:51:00 GMT 2009


Hi,

I plan to commit this patch to add symbols write support for Mach-O  
back-end.  This patch also checks the
return status of bfd_seek.

(Next step is relocations...)

Tristan.

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

	* mach-o.h (bfd_mach_o_symtab_command): Remove stabs_segment
	and stabstr_segment fields.
	(mach_o_be_vec, mach_o_le_vec): Removed
	(bfd_mach_o_version): New prototype.

	* mach-o.c (bfd_mach_o_version): Make this function public.
	(mach_o_wide_p): New function.
	(bfd_mach_o_wide_p): Ditto.
	(bfd_mach_o_convert_section_name_to_bfd): Add prefix only for
	weird names.
	(bfd_mach_o_convert_section_name_to_mach_o): Fix typo in comment.
	Search in the list only if the name starts with a dot.
	(bfd_mach_o_write_header): Use mach_o_wide_p instead of hard-coded
	test.  Check bfd_seek status.
	(bfd_mach_o_scan_write_thread): Check bfd_seek status.
	(bfd_mach_o_scan_write_section_32): Ditto.
	(bfd_mach_o_scan_write_section_64): Ditto.
	(bfd_mach_o_scan_write_section): Removed.
	(bfd_mach_o_scan_write_segment): Split into ...
	(bfd_mach_o_scan_write_segment_32): ... this and ...
	(bfd_mach_o_scan_write_segment_64): ... this.  Check bfd_seek status.
	(bfd_mach_o_scan_write_symtab_symbols): Moved into ...
	(bfd_mach_o_scan_write_symtab): ... this.  Write symtab from BFD
	symbol table.  Now returns a boolean.
	(bfd_mach_o_write_contents): Set filetype.  Check bfd_seek status.
	Adjust for status type.
	(bfd_mach_o_build_commands): Use mach_o_wide_p instead of hard-coded
	test.  Write symbol table.  Numbers the sections.
	(bfd_mach_o_read_header): Check bfd_seek status.
	Use mach_o_wide_p instead of hard-coded test.
	(bfd_mach_o_scan_read_section_32): Check bfd_seek status.
	(bfd_mach_o_scan_read_section_64): Ditto.
	(bfd_mach_o_scan_read_symtab_symbol): Ditto.  Check bfd_seek status.
	Use BFD_MACH_O_N_TYPE instead of hard-coded value.  Correctly
	handled common symbols.
	(bfd_mach_o_scan_read_symtab_strtab): Check bfd_seek status.
	(bfd_mach_o_scan_read_dysymtab_symbol): Ditto.
	(bfd_mach_o_scan_read_dylinker): Ditto.
	(bfd_mach_o_scan_read_dylib): Ditto.
	(bfd_mach_o_scan_read_thread): Ditto.
	(bfd_mach_o_scan_read_symtab): Ditto.
	Do not create a section for the stabs.
	(bfd_mach_o_scan_read_uuid): Check bfd_seek status.
	(bfd_mach_o_scan_read_segment): Ditto.
	(bfd_mach_o_scan_read_command): Ditto.
	(bfd_mach_o_scan_start_address): Ditto.
	(bfd_mach_o_scan): Use mach_o_wide_p instead of hard-coded test.
	(bfd_mach_o_archive_p): Check bfd_seek status.
	(bfd_mach_o_core_fetch_environment): Ditto.

	* mach-o-i386.c (bfd_mach_o_i386_mkobject): Don't set filetype.

Index: bfd/mach-o-i386.c
===================================================================
RCS file: /cvs/src/src/bfd/mach-o-i386.c,v
retrieving revision 1.1
diff -c -p -r1.1 mach-o-i386.c
*** bfd/mach-o-i386.c	5 Jun 2009 09:19:44 -0000	1.1
--- bfd/mach-o-i386.c	8 Jun 2009 12:46:26 -0000
*************** bfd_mach_o_i386_mkobject (bfd *abfd)
*** 54,60 ****
     mdata->header.magic = BFD_MACH_O_MH_MAGIC;
     mdata->header.cputype = BFD_MACH_O_CPU_TYPE_I386;
     mdata->header.cpusubtype = BFD_MACH_O_CPU_SUBTYPE_X86_ALL;
-   mdata->header.filetype = BFD_MACH_O_MH_OBJECT;
     mdata->header.byteorder = BFD_ENDIAN_LITTLE;
     mdata->header.version = 1;

--- 54,59 ----
Index: bfd/mach-o.c
===================================================================
RCS file: /cvs/src/src/bfd/mach-o.c,v
retrieving revision 1.34
diff -c -p -r1.34 mach-o.c
*** bfd/mach-o.c	5 Jun 2009 09:19:44 -0000	1.34
--- bfd/mach-o.c	8 Jun 2009 12:46:26 -0000
***************
*** 31,37 ****
   #define bfd_mach_o_core_p bfd_mach_o_gen_core_p
   #define bfd_mach_o_mkobject bfd_false

! static unsigned int
   bfd_mach_o_version (bfd *abfd)
   {
     bfd_mach_o_data_struct *mdata = NULL;
--- 31,37 ----
   #define bfd_mach_o_core_p bfd_mach_o_gen_core_p
   #define bfd_mach_o_mkobject bfd_false

! unsigned int
   bfd_mach_o_version (bfd *abfd)
   {
     bfd_mach_o_data_struct *mdata = NULL;
*************** bfd_mach_o_valid (bfd *abfd)
*** 56,61 ****
--- 56,82 ----
     return TRUE;
   }

+ static INLINE bfd_boolean
+ mach_o_wide_p (bfd_mach_o_header *header)
+ {
+   switch (header->version)
+     {
+     case 1:
+       return FALSE;
+     case 2:
+       return TRUE;
+     default:
+       BFD_FAIL ();
+       return FALSE;
+     }
+ }
+
+ static INLINE bfd_boolean
+ bfd_mach_o_wide_p (bfd *abfd)
+ {
+   return mach_o_wide_p (&abfd->tdata.mach_o_data->header);
+ }
+
   /* Tables to translate well known Mach-O segment/section names to bfd
      names.  Use of canonical names (such as .text or .debug_frame)  
is required
      by gdb.  */
*************** struct mach_o_segment_name_xlat
*** 103,109 ****
     const struct mach_o_section_name_xlat *sections;
   };

! static const struct mach_o_segment_name_xlat segsec_names_xlat[] =
     {
       { "__DWARF", dwarf_section_names_xlat },
       { "__TEXT", text_section_names_xlat },
--- 124,130 ----
     const struct mach_o_section_name_xlat *sections;
   };

! static const struct mach_o_segment_name_xlat segsec_names_xlat[] =
     {
       { "__DWARF", dwarf_section_names_xlat },
       { "__TEXT", text_section_names_xlat },
*************** bfd_mach_o_convert_section_name_to_bfd (
*** 120,125 ****
--- 141,147 ----
     const struct mach_o_segment_name_xlat *seg;
     char *res;
     unsigned int len;
+   const char *pfx = "";

     for (seg = segsec_names_xlat; seg->segname; seg++)
       {
*************** bfd_mach_o_convert_section_name_to_bfd (
*** 133,139 ****
                   {
                     len = strlen (sec->bfd_name);
                     res = bfd_alloc (abfd, len + 1);
!
                     if (res == NULL)
                       return NULL;
                     strcpy (res, sec->bfd_name);
--- 155,161 ----
                   {
                     len = strlen (sec->bfd_name);
                     res = bfd_alloc (abfd, len + 1);
!
                     if (res == NULL)
                       return NULL;
                     strcpy (res, sec->bfd_name);
*************** bfd_mach_o_convert_section_name_to_bfd (
*** 143,160 ****
           }
       }

!   len = sizeof ("LC_SEGMENT") - 1 + 1
!     + strlen (section->segname) + 1
       + strlen (section->sectname) + 1;

     res = bfd_alloc (abfd, len);
     if (res == NULL)
       return NULL;
!   snprintf (res, len, "LC_SEGMENT.%s.%s", section->segname, section- 
 >sectname);
     return res;
   }

! /* Convert a bfd sectio name to a Mach-O segment + section name.  */

   static void
   bfd_mach_o_convert_section_name_to_mach_o (bfd *abfd  
ATTRIBUTE_UNUSED,
--- 165,191 ----
           }
       }

!   len = strlen (section->segname) + 1
       + strlen (section->sectname) + 1;

+   /* Put "LC_SEGMENT." prefix if the segment name is weird (ie  
doesn't start
+      with an underscore.  */
+   if (section->segname[0] != '_')
+     {
+       static const char seg_pfx[] = "LC_SEGMENT.";
+
+       pfx = seg_pfx;
+       len += sizeof (seg_pfx) - 1;
+     }
+
     res = bfd_alloc (abfd, len);
     if (res == NULL)
       return NULL;
!   snprintf (res, len, "%s%s.%s", pfx, section->segname, section- 
 >sectname);
     return res;
   }

! /* Convert a bfd section name to a Mach-O segment + section name.  */

   static void
   bfd_mach_o_convert_section_name_to_mach_o (bfd *abfd  
ATTRIBUTE_UNUSED,
*************** bfd_mach_o_convert_section_name_to_mach_
*** 168,188 ****
     unsigned int seglen;
     unsigned int seclen;

!   /* List of well known names.  */
!   for (seg = segsec_names_xlat; seg->segname; seg++)
!     {
!       const struct mach_o_section_name_xlat *sec;

!       for (sec = seg->sections; sec->mach_o_name; sec++)
!         {
!           if (strcmp (sec->bfd_name, name) == 0)
!             {
!               strcpy (section->segname, seg->segname);
!               strcpy (section->sectname, sec->mach_o_name);
!               return;
!             }
!         }
!     }

     /* Strip LC_SEGMENT. prefix.  */
     if (strncmp (name, "LC_SEGMENT.", 11) == 0)
--- 199,220 ----
     unsigned int seglen;
     unsigned int seclen;

!   /* List of well known names.  They all start with a dot.  */
!   if (name[0] == '.')
!     for (seg = segsec_names_xlat; seg->segname; seg++)
!       {
!         const struct mach_o_section_name_xlat *sec;

!         for (sec = seg->sections; sec->mach_o_name; sec++)
!           {
!             if (strcmp (sec->bfd_name, name) == 0)
!               {
!                 strcpy (section->segname, seg->segname);
!                 strcpy (section->sectname, sec->mach_o_name);
!                 return;
!               }
!           }
!       }

     /* Strip LC_SEGMENT. prefix.  */
     if (strncmp (name, "LC_SEGMENT.", 11) == 0)
*************** bfd_mach_o_convert_architecture (bfd_mac
*** 426,436 ****
       case BFD_MACH_O_CPU_TYPE_ALPHA: *type = bfd_arch_alpha; break;
       case BFD_MACH_O_CPU_TYPE_POWERPC:
         *type = bfd_arch_powerpc;
!       *subtype = bfd_mach_ppc;
         break;
       case BFD_MACH_O_CPU_TYPE_POWERPC_64:
         *type = bfd_arch_powerpc;
!       *subtype = bfd_mach_ppc64;
         break;
       default:
         *type = bfd_arch_unknown;
--- 458,468 ----
       case BFD_MACH_O_CPU_TYPE_ALPHA: *type = bfd_arch_alpha; break;
       case BFD_MACH_O_CPU_TYPE_POWERPC:
         *type = bfd_arch_powerpc;
!       *subtype = bfd_mach_ppc;
         break;
       case BFD_MACH_O_CPU_TYPE_POWERPC_64:
         *type = bfd_arch_powerpc;
!       *subtype = bfd_mach_ppc64;
         break;
       default:
         *type = bfd_arch_unknown;
*************** bfd_mach_o_write_header (bfd *abfd, bfd_
*** 444,450 ****
     unsigned char buf[32];
     unsigned int size;

!   size = (header->version == 2) ?
       BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;

     bfd_h_put_32 (abfd, header->magic, buf + 0);
--- 476,482 ----
     unsigned char buf[32];
     unsigned int size;

!   size = mach_o_wide_p (header) ?
       BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;

     bfd_h_put_32 (abfd, header->magic, buf + 0);
*************** bfd_mach_o_write_header (bfd *abfd, bfd_
*** 455,465 ****
     bfd_h_put_32 (abfd, header->sizeofcmds, buf + 20);
     bfd_h_put_32 (abfd, header->flags, buf + 24);

!   if (header->version == 2)
       bfd_h_put_32 (abfd, header->reserved, buf + 28);

!   bfd_seek (abfd, 0, SEEK_SET);
!   if (bfd_bwrite ((PTR) buf, size, abfd) != size)
       return FALSE;

     return TRUE;
--- 487,497 ----
     bfd_h_put_32 (abfd, header->sizeofcmds, buf + 20);
     bfd_h_put_32 (abfd, header->flags, buf + 24);

!   if (mach_o_wide_p (header))
       bfd_h_put_32 (abfd, header->reserved, buf + 28);

!   if (bfd_seek (abfd, 0, SEEK_SET) != 0
!       || bfd_bwrite ((PTR) buf, size, abfd) != size)
       return FALSE;

     return TRUE;
*************** bfd_mach_o_scan_write_thread (bfd *abfd,
*** 487,494 ****
         bfd_h_put_32 (abfd, cmd->flavours[i].flavour, buf);
         bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), buf + 4);

!       bfd_seek (abfd, command->offset + offset, SEEK_SET);
!       if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
   	return -1;

         offset += cmd->flavours[i].size + 8;
--- 519,526 ----
         bfd_h_put_32 (abfd, cmd->flavours[i].flavour, buf);
         bfd_h_put_32 (abfd, (cmd->flavours[i].size / 4), buf + 4);

!       if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
!           || bfd_bwrite ((PTR) buf, 8, abfd) != 8)
   	return -1;

         offset += cmd->flavours[i].size + 8;
*************** bfd_mach_o_scan_write_section_32 (bfd *a
*** 516,524 ****
     bfd_h_put_32 (abfd, section->reserved1, buf + 60);
     bfd_h_put_32 (abfd, section->reserved2, buf + 64);

!   bfd_seek (abfd, offset, SEEK_SET);
!   if (bfd_bwrite ((PTR) buf, BFD_MACH_O_SECTION_SIZE, abfd)
!       != BFD_MACH_O_SECTION_SIZE)
       return -1;

     return 0;
--- 548,556 ----
     bfd_h_put_32 (abfd, section->reserved1, buf + 60);
     bfd_h_put_32 (abfd, section->reserved2, buf + 64);

!   if (bfd_seek (abfd, offset, SEEK_SET) != 0
!       || (bfd_bwrite ((PTR) buf, BFD_MACH_O_SECTION_SIZE, abfd)
!           != BFD_MACH_O_SECTION_SIZE))
       return -1;

     return 0;
*************** bfd_mach_o_scan_write_section_64 (bfd *a
*** 544,708 ****
     bfd_h_put_32 (abfd, section->reserved2, buf + 72);
     bfd_h_put_32 (abfd, section->reserved3, buf + 76);

!   bfd_seek (abfd, offset, SEEK_SET);
!   if (bfd_bwrite ((PTR) buf, BFD_MACH_O_SECTION_64_SIZE, abfd)
!       != BFD_MACH_O_SECTION_64_SIZE)
       return -1;

     return 0;
   }

   static int
! bfd_mach_o_scan_write_section (bfd *abfd,
! 			       bfd_mach_o_section *section,
! 			       bfd_vma offset,
! 			       unsigned int wide)
! {
!   if (wide)
!     return bfd_mach_o_scan_write_section_64 (abfd, section, offset);
!   else
!     return bfd_mach_o_scan_write_section_32 (abfd, section, offset);
! }
!
! static int
! bfd_mach_o_scan_write_segment (bfd *abfd,
! 			       bfd_mach_o_load_command *command,
! 			       unsigned int wide)
   {
!   unsigned char buf[64];
     bfd_mach_o_segment_command *seg = &command->command.segment;
     unsigned long i;

!   if (wide)
!     {
!       BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
!
!       memcpy (buf, seg->segname, 16);
!
!       bfd_h_put_64 (abfd, seg->vmaddr, buf + 16);
!       bfd_h_put_64 (abfd, seg->vmsize, buf + 24);
!       bfd_h_put_64 (abfd, seg->fileoff, buf + 32);
!       bfd_h_put_64 (abfd, seg->filesize, buf + 40);
!       bfd_h_put_32 (abfd, seg->maxprot, buf + 48);
!       bfd_h_put_32 (abfd, seg->initprot, buf + 52);
!       bfd_h_put_32 (abfd, seg->nsects, buf + 56);
!       bfd_h_put_32 (abfd, seg->flags, buf + 60);

!       bfd_seek (abfd, command->offset + 8, SEEK_SET);
!       if (bfd_bwrite ((PTR) buf, 64, abfd) != 64)
! 	return -1;
!     }
!   else
!     {
!       BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);
!
!       memcpy (buf, seg->segname, 16);

!       bfd_h_put_32 (abfd, seg->vmaddr, buf + 16);
!       bfd_h_put_32 (abfd, seg->vmsize, buf + 20);
!       bfd_h_put_32 (abfd, seg->fileoff, buf + 24);
!       bfd_h_put_32 (abfd, seg->filesize, buf + 28);
!       bfd_h_put_32 (abfd, seg->maxprot, buf + 32);
!       bfd_h_put_32 (abfd, seg->initprot, buf + 36);
!       bfd_h_put_32 (abfd, seg->nsects, buf + 40);
!       bfd_h_put_32 (abfd, seg->flags, buf + 44);
!
!       bfd_seek (abfd, command->offset + 8, SEEK_SET);
!       if (bfd_bwrite ((PTR) buf, 48, abfd) != 48)
! 	return -1;
!     }

     for (i = 0; i < seg->nsects; i++)
       {
!       bfd_vma segoff;
!       if (wide)
! 	segoff = command->offset + BFD_MACH_O_LC_SEGMENT_64_SIZE
!           + (i * BFD_MACH_O_SECTION_64_SIZE);
!       else
! 	segoff = command->offset + BFD_MACH_O_LC_SEGMENT_SIZE
!           + (i * BFD_MACH_O_SECTION_SIZE);

!       if (bfd_mach_o_scan_write_section
! 	  (abfd, &seg->sections[i], segoff, wide) != 0)
   	return -1;
       }
-
     return 0;
   }

   static int
- bfd_mach_o_scan_write_segment_32 (bfd *abfd, bfd_mach_o_load_command  
*command)
- {
-   return bfd_mach_o_scan_write_segment (abfd, command, 0);
- }
-
- static int
   bfd_mach_o_scan_write_segment_64 (bfd *abfd,  
bfd_mach_o_load_command *command)
   {
!   return bfd_mach_o_scan_write_segment (abfd, command, 1);
   }

! static int
! bfd_mach_o_scan_write_symtab_symbols (bfd *abfd,  
bfd_mach_o_load_command *command)
   {
     bfd_mach_o_symtab_command *sym = &command->command.symtab;
!   asymbol *s = NULL;
     unsigned long i;

     for (i = 0; i < sym->nsyms; i++)
       {
!       unsigned char buf[12];
!       bfd_vma symoff = sym->symoff + (i * 12);
!       unsigned char ntype = 0;
!       unsigned char nsect = 0;
!       short ndesc = 0;
!
!       s = &sym->symbols[i];

!       /* Instead just set from the stored values.  */
         ntype = BFD_MACH_O_SYM_NTYPE (s);
         nsect = BFD_MACH_O_SYM_NSECT (s);
         ndesc = BFD_MACH_O_SYM_NDESC (s);

!       bfd_h_put_32 (abfd, s->name - sym->strtab, buf);
         bfd_h_put_8 (abfd, ntype, buf + 4);
         bfd_h_put_8 (abfd, nsect, buf + 5);
         bfd_h_put_16 (abfd, ndesc, buf + 6);
!       bfd_h_put_32 (abfd, s->section->vma + s->value, buf + 8);

!       bfd_seek (abfd, symoff, SEEK_SET);
!       if (bfd_bwrite ((PTR) buf, 12, abfd) != 12)
! 	{
! 	  fprintf (stderr, "bfd_mach_o_scan_write_symtab_symbols: unable to  
write %d bytes at %lu\n",
! 		   12, (unsigned long) symoff);
! 	  return -1;
! 	}
       }

!   return 0;
! }
!
! static int
! bfd_mach_o_scan_write_symtab (bfd *abfd, bfd_mach_o_load_command  
*command)
! {
!   bfd_mach_o_symtab_command *seg = &command->command.symtab;
!   unsigned char buf[16];
!
!   BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);

!   bfd_h_put_32 (abfd, seg->symoff, buf);
!   bfd_h_put_32 (abfd, seg->nsyms, buf + 4);
!   bfd_h_put_32 (abfd, seg->stroff, buf + 8);
!   bfd_h_put_32 (abfd, seg->strsize, buf + 12);
!
!   bfd_seek (abfd, command->offset + 8, SEEK_SET);
!   if (bfd_bwrite ((PTR) buf, 16, abfd) != 16)
!     return -1;

!   if (bfd_mach_o_scan_write_symtab_symbols (abfd, command) != 0)
!     return -1;

!   return 0;
   }

   bfd_boolean
--- 576,769 ----
     bfd_h_put_32 (abfd, section->reserved2, buf + 72);
     bfd_h_put_32 (abfd, section->reserved3, buf + 76);

!   if (bfd_seek (abfd, offset, SEEK_SET) != 0
!       || (bfd_bwrite ((PTR) buf, BFD_MACH_O_SECTION_64_SIZE, abfd)
!           != BFD_MACH_O_SECTION_64_SIZE))
       return -1;

     return 0;
   }

   static int
! bfd_mach_o_scan_write_segment_32 (bfd *abfd, bfd_mach_o_load_command  
*command)
   {
!   unsigned char buf[BFD_MACH_O_LC_SEGMENT_SIZE];
     bfd_mach_o_segment_command *seg = &command->command.segment;
     unsigned long i;

!   BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);

!   memcpy (buf, seg->segname, 16);

!   bfd_h_put_32 (abfd, seg->vmaddr, buf + 16);
!   bfd_h_put_32 (abfd, seg->vmsize, buf + 20);
!   bfd_h_put_32 (abfd, seg->fileoff, buf + 24);
!   bfd_h_put_32 (abfd, seg->filesize, buf + 28);
!   bfd_h_put_32 (abfd, seg->maxprot, buf + 32);
!   bfd_h_put_32 (abfd, seg->initprot, buf + 36);
!   bfd_h_put_32 (abfd, seg->nsects, buf + 40);
!   bfd_h_put_32 (abfd, seg->flags, buf + 44);
!
!   if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
!       || (bfd_bwrite ((PTR) buf, BFD_MACH_O_LC_SEGMENT_SIZE, abfd)
!           != BFD_MACH_O_LC_SEGMENT_SIZE))
!     return -1;

     for (i = 0; i < seg->nsects; i++)
       {
!       bfd_vma segoff = command->offset + BFD_MACH_O_LC_SEGMENT_SIZE
!         + (i * BFD_MACH_O_SECTION_SIZE);

!       if (bfd_mach_o_scan_write_section_32 (abfd, &seg->sections[i],  
segoff))
   	return -1;
       }
     return 0;
   }

   static int
   bfd_mach_o_scan_write_segment_64 (bfd *abfd,  
bfd_mach_o_load_command *command)
   {
!   unsigned char buf[BFD_MACH_O_LC_SEGMENT_64_SIZE];
!   bfd_mach_o_segment_command *seg = &command->command.segment;
!   unsigned long i;
!
!   BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);
!
!   memcpy (buf, seg->segname, 16);
!
!   bfd_h_put_64 (abfd, seg->vmaddr, buf + 16);
!   bfd_h_put_64 (abfd, seg->vmsize, buf + 24);
!   bfd_h_put_64 (abfd, seg->fileoff, buf + 32);
!   bfd_h_put_64 (abfd, seg->filesize, buf + 40);
!   bfd_h_put_32 (abfd, seg->maxprot, buf + 48);
!   bfd_h_put_32 (abfd, seg->initprot, buf + 52);
!   bfd_h_put_32 (abfd, seg->nsects, buf + 56);
!   bfd_h_put_32 (abfd, seg->flags, buf + 60);
!
!   if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
!       || (bfd_bwrite ((PTR) buf, BFD_MACH_O_LC_SEGMENT_64_SIZE, abfd)
!           != BFD_MACH_O_LC_SEGMENT_64_SIZE))
!     return -1;
!
!   for (i = 0; i < seg->nsects; i++)
!     {
!       bfd_vma segoff = command->offset + BFD_MACH_O_LC_SEGMENT_64_SIZE
!         + (i * BFD_MACH_O_SECTION_64_SIZE);
!
!       if (bfd_mach_o_scan_write_section_64 (abfd, &seg->sections[i],  
segoff))
! 	return -1;
!     }
!   return 0;
   }

! static bfd_boolean
! bfd_mach_o_scan_write_symtab (bfd *abfd, bfd_mach_o_load_command  
*command)
   {
     bfd_mach_o_symtab_command *sym = &command->command.symtab;
!   unsigned char buf[16];
     unsigned long i;
+   unsigned int wide = bfd_mach_o_wide_p (abfd);
+   unsigned int symlen = wide ? 16 : 12;
+   struct bfd_strtab_hash *strtab;
+   asymbol **symbols = bfd_get_outsymbols (abfd);
+
+   BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);
+
+   /* Write the symbols first.  */
+   if (bfd_seek (abfd, sym->symoff, SEEK_SET) != 0)
+     return FALSE;
+
+   sym->nsyms = bfd_get_symcount (abfd);
+
+   strtab = _bfd_stringtab_init ();
+   if (strtab == NULL)
+     return FALSE;

     for (i = 0; i < sym->nsyms; i++)
       {
!       unsigned char buf[16];
!       unsigned char ntype;
!       unsigned char nsect;
!       short ndesc;
!       bfd_size_type index;
!       asymbol *s = symbols[i];
!
!       /* Compute index.  */
!       /* An index of 0 always means the empty string.  */
!       if (s->name == 0 || s->name[0] == '\0')
!         index = 0;
!       else
!         {
!           index = _bfd_stringtab_add (strtab, s->name, TRUE, FALSE);
!           if (index == (bfd_size_type) -1)
!             goto err;
!         }

!       /* Get back-end specific values.  */
         ntype = BFD_MACH_O_SYM_NTYPE (s);
         nsect = BFD_MACH_O_SYM_NSECT (s);
         ndesc = BFD_MACH_O_SYM_NDESC (s);

!       if (ntype == BFD_MACH_O_N_UNDF && !(s->flags & BSF_DEBUGGING))
!         {
!           /* As genuine Mach-O symbols type shouldn't be N_UNDF  
(undefined
!              symbols should be N_UNDEF | N_EXT), we suppose the back- 
end
!              values haven't been set.  */
!           if (s->flags & (BSF_LOCAL | BSF_GLOBAL))
!             {
!               if (s->section == bfd_abs_section_ptr)
!                 ntype = BFD_MACH_O_N_ABS;
!               else if (s->section == bfd_und_section_ptr)
!                 ntype = BFD_MACH_O_N_UNDF;
!               else if (s->section == bfd_com_section_ptr)
!                 ntype = BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT;
!               else
!                 ntype = BFD_MACH_O_N_SECT;
!
!               ntype |= (s->flags & BSF_GLOBAL) ? BFD_MACH_O_N_EXT : 0;
!             }
!         }
!
!       /* Compute section index.  */
!       if (s->section != bfd_abs_section_ptr
!           && s->section != bfd_und_section_ptr
!           && s->section != bfd_com_section_ptr)
!         nsect = s->section->target_index;
!
!       bfd_h_put_32 (abfd, index, buf);
         bfd_h_put_8 (abfd, ntype, buf + 4);
         bfd_h_put_8 (abfd, nsect, buf + 5);
         bfd_h_put_16 (abfd, ndesc, buf + 6);
!       if (wide)
!         bfd_h_put_64 (abfd, s->section->vma + s->value, buf + 8);
!       else
!         bfd_h_put_32 (abfd, s->section->vma + s->value, buf + 8);

!       if (bfd_bwrite ((PTR) buf, symlen, abfd) != symlen)
!         goto err;
       }
+   sym->strsize = _bfd_stringtab_size (strtab);
+   sym->stroff = sym->symoff + sym->nsyms * symlen;

!   if (_bfd_stringtab_emit (abfd, strtab) != TRUE)
!     goto err;
!   _bfd_stringtab_free (strtab);
!
!   /* The command.  */
!   bfd_h_put_32 (abfd, sym->symoff, buf);
!   bfd_h_put_32 (abfd, sym->nsyms, buf + 4);
!   bfd_h_put_32 (abfd, sym->stroff, buf + 8);
!   bfd_h_put_32 (abfd, sym->strsize, buf + 12);

!   if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
!       || bfd_bwrite ((PTR) buf, 16, abfd) != 16)
!     return FALSE;

!   return TRUE;

!  err:
!   _bfd_stringtab_free (strtab);
!   return FALSE;
   }

   bfd_boolean
*************** bfd_mach_o_write_contents (bfd *abfd)
*** 712,717 ****
--- 773,787 ----
     bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;

     /* Now write header information.  */
+   if (mdata->header.filetype == 0)
+     {
+       if (abfd->flags & EXEC_P)
+         mdata->header.filetype = BFD_MACH_O_MH_EXECUTE;
+       else if (abfd->flags & DYNAMIC)
+         mdata->header.filetype = BFD_MACH_O_MH_DYLIB;
+       else
+         mdata->header.filetype = BFD_MACH_O_MH_OBJECT;
+     }
     if (!bfd_mach_o_write_header (abfd, &mdata->header))
       return FALSE;

*************** bfd_mach_o_write_contents (bfd *abfd)
*** 726,733 ****
         bfd_h_put_32 (abfd, typeflag, buf);
         bfd_h_put_32 (abfd, cur->len, buf + 4);

!       bfd_seek (abfd, cur->offset, SEEK_SET);
!       if (bfd_bwrite ((PTR) buf, 8, abfd) != 8)
   	return FALSE;

         switch (cur->type)
--- 796,803 ----
         bfd_h_put_32 (abfd, typeflag, buf);
         bfd_h_put_32 (abfd, cur->len, buf + 4);

!       if (bfd_seek (abfd, cur->offset, SEEK_SET) != 0
!           || bfd_bwrite ((PTR) buf, 8, abfd) != 8)
   	return FALSE;

         switch (cur->type)
*************** bfd_mach_o_write_contents (bfd *abfd)
*** 741,747 ****
   	    return FALSE;
   	  break;
   	case BFD_MACH_O_LC_SYMTAB:
! 	  if (bfd_mach_o_scan_write_symtab (abfd, cur) != 0)
   	    return FALSE;
   	  break;
   	case BFD_MACH_O_LC_SYMSEG:
--- 811,817 ----
   	    return FALSE;
   	  break;
   	case BFD_MACH_O_LC_SYMTAB:
! 	  if (!bfd_mach_o_scan_write_symtab (abfd, cur))
   	    return FALSE;
   	  break;
   	case BFD_MACH_O_LC_SYMSEG:
*************** bfd_boolean
*** 783,805 ****
   bfd_mach_o_build_commands (bfd *abfd)
   {
     bfd_mach_o_data_struct *mdata = bfd_get_mach_o_data (abfd);
!   unsigned int wide = (mdata->header.version == 2);
     bfd_mach_o_segment_command *seg;
     bfd_mach_o_section *sections;
     asection *sec;
     file_ptr filepos;

     /* Return now if commands are already built.  */
     if (mdata->header.ncmds)
       return FALSE;

     /* Very simple version: 1 command (segment) containing all  
sections.  */
!   mdata->header.ncmds = 1;
!   mdata->commands = bfd_alloc (abfd, mdata->header.ncmds
                                  * sizeof (bfd_mach_o_load_command));
     if (mdata->commands == NULL)
       return FALSE;
!   seg = &mdata->commands[0].command.segment;
     seg->nsects = bfd_count_sections (abfd);
     sections = bfd_alloc (abfd, seg->nsects * sizeof  
(bfd_mach_o_section));
     if (sections == NULL)
--- 853,880 ----
   bfd_mach_o_build_commands (bfd *abfd)
   {
     bfd_mach_o_data_struct *mdata = bfd_get_mach_o_data (abfd);
!   unsigned int wide = mach_o_wide_p (&mdata->header);
     bfd_mach_o_segment_command *seg;
     bfd_mach_o_section *sections;
     asection *sec;
     file_ptr filepos;
+   bfd_mach_o_load_command *cmd;
+   bfd_mach_o_load_command *symtab_cmd;
+   int target_index;

     /* Return now if commands are already built.  */
     if (mdata->header.ncmds)
       return FALSE;

     /* Very simple version: 1 command (segment) containing all  
sections.  */
!   mdata->header.ncmds = 2;
!   mdata->commands = bfd_alloc (abfd, mdata->header.ncmds
                                  * sizeof (bfd_mach_o_load_command));
     if (mdata->commands == NULL)
       return FALSE;
!   cmd = &mdata->commands[0];
!   seg = &cmd->command.segment;
!
     seg->nsects = bfd_count_sections (abfd);
     sections = bfd_alloc (abfd, seg->nsects * sizeof  
(bfd_mach_o_section));
     if (sections == NULL)
*************** bfd_mach_o_build_commands (bfd *abfd)
*** 809,831 ****
     /* Set segment command.  */
     if (wide)
       {
!       mdata->commands[0].type = BFD_MACH_O_LC_SEGMENT_64;
!       mdata->commands[0].offset = BFD_MACH_O_HEADER_64_SIZE;
!       mdata->commands[0].len = BFD_MACH_O_LC_SEGMENT_64_SIZE
           + BFD_MACH_O_SECTION_64_SIZE * seg->nsects;
       }
     else
       {
!       mdata->commands[0].type = BFD_MACH_O_LC_SEGMENT;
!       mdata->commands[0].offset = BFD_MACH_O_HEADER_SIZE;
!       mdata->commands[0].len = BFD_MACH_O_LC_SEGMENT_SIZE
           + BFD_MACH_O_SECTION_SIZE * seg->nsects;
       }
!   mdata->commands[0].type_required = FALSE;
!   mdata->header.sizeofcmds = mdata->commands[0].len;
!
!   filepos = mdata->commands[0].offset + mdata->commands[0].len;

     memset (seg->segname, 0, sizeof (seg->segname));
     seg->vmaddr = 0;
     seg->fileoff = filepos;
--- 884,917 ----
     /* Set segment command.  */
     if (wide)
       {
!       cmd->type = BFD_MACH_O_LC_SEGMENT_64;
!       cmd->offset = BFD_MACH_O_HEADER_64_SIZE;
!       cmd->len = BFD_MACH_O_LC_SEGMENT_64_SIZE
           + BFD_MACH_O_SECTION_64_SIZE * seg->nsects;
       }
     else
       {
!       cmd->type = BFD_MACH_O_LC_SEGMENT;
!       cmd->offset = BFD_MACH_O_HEADER_SIZE;
!       cmd->len = BFD_MACH_O_LC_SEGMENT_SIZE
           + BFD_MACH_O_SECTION_SIZE * seg->nsects;
       }
!   cmd->type_required = FALSE;
!   mdata->header.sizeofcmds = cmd->len;
!   filepos = cmd->offset + cmd->len;

+   /* Set symtab command.  */
+   symtab_cmd = &mdata->commands[1];
+
+   symtab_cmd->type = BFD_MACH_O_LC_SYMTAB;
+   symtab_cmd->offset = cmd->offset + cmd->len;
+   symtab_cmd->len = 6 * 4;
+   symtab_cmd->type_required = FALSE;
+
+   mdata->header.sizeofcmds += symtab_cmd->len;
+   filepos += symtab_cmd->len;
+
+   /* Fill segment command.  */
     memset (seg->segname, 0, sizeof (seg->segname));
     seg->vmaddr = 0;
     seg->fileoff = filepos;
*************** bfd_mach_o_build_commands (bfd *abfd)
*** 836,841 ****
--- 922,928 ----
     seg->flags = 0;

     /* Create Mach-O sections.  */
+   target_index = 0;
     for (sec = abfd->sections; sec; sec = sec->next)
       {
         sections->bfdsection = sec;
*************** bfd_mach_o_build_commands (bfd *abfd)
*** 843,849 ****
         sections->addr = bfd_get_section_vma (abfd, sec);
         sections->size = bfd_get_section_size (sec);
         sections->align = bfd_get_section_alignment (abfd, sec);
!
         filepos = (filepos + ((file_ptr) 1 << sections->align) - 1)
           & ((file_ptr) -1 << sections->align);
         sections->offset = filepos;
--- 930,936 ----
         sections->addr = bfd_get_section_vma (abfd, sec);
         sections->size = bfd_get_section_size (sec);
         sections->align = bfd_get_section_alignment (abfd, sec);
!
         filepos = (filepos + ((file_ptr) 1 << sections->align) - 1)
           & ((file_ptr) -1 << sections->align);
         sections->offset = filepos;
*************** bfd_mach_o_build_commands (bfd *abfd)
*** 854,859 ****
--- 941,947 ----
         sections->reserved3 = 0;

         sec->filepos = filepos;
+       sec->target_index = ++target_index;

         filepos += sections->size;
         sections++;
*************** bfd_mach_o_build_commands (bfd *abfd)
*** 861,866 ****
--- 949,960 ----
     seg->filesize = filepos - seg->fileoff;
     seg->vmsize = seg->filesize;

+   /* Fill symtab command.
+      Note: we don't know the number of symbols.
+      Also, symtab is at the end since the length of the symbol table  
(and
+      string table) is not known.  */
+   symtab_cmd->command.symtab.symoff = filepos;
+
     return TRUE;
   }

*************** bfd_mach_o_read_header (bfd *abfd, bfd_m
*** 921,928 ****
     bfd_vma (*get32) (const void *) = NULL;

     /* Just read the magic number.  */
!   bfd_seek (abfd, 0, SEEK_SET);
!   if (bfd_bread ((PTR) buf, 4, abfd) != 4)
       return FALSE;

     if (bfd_getb32 (buf) == BFD_MACH_O_MH_MAGIC)
--- 1015,1022 ----
     bfd_vma (*get32) (const void *) = NULL;

     /* Just read the magic number.  */
!   if (bfd_seek (abfd, 0, SEEK_SET) != 0
!       || bfd_bread ((PTR) buf, 4, abfd) != 4)
       return FALSE;

     if (bfd_getb32 (buf) == BFD_MACH_O_MH_MAGIC)
*************** bfd_mach_o_read_header (bfd *abfd, bfd_m
*** 960,970 ****
       }

     /* Once the size of the header is known, read the full header.  */
!   size = (header->version == 2) ?
       BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;

!   bfd_seek (abfd, 0, SEEK_SET);
!   if (bfd_bread ((PTR) buf, size, abfd) != size)
       return FALSE;

     header->cputype = (*get32) (buf + 4);
--- 1054,1064 ----
       }

     /* Once the size of the header is known, read the full header.  */
!   size = mach_o_wide_p (header) ?
       BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;

!   if (bfd_seek (abfd, 0, SEEK_SET) != 0
!       || bfd_bread ((PTR) buf, size, abfd) != size)
       return FALSE;

     header->cputype = (*get32) (buf + 4);
*************** bfd_mach_o_read_header (bfd *abfd, bfd_m
*** 974,980 ****
     header->sizeofcmds = (*get32) (buf + 20);
     header->flags = (*get32) (buf + 24);

!   if (header->version == 2)
       header->reserved = (*get32) (buf + 28);

     return TRUE;
--- 1068,1074 ----
     header->sizeofcmds = (*get32) (buf + 20);
     header->flags = (*get32) (buf + 24);

!   if (mach_o_wide_p (header))
       header->reserved = (*get32) (buf + 28);

     return TRUE;
*************** bfd_mach_o_scan_read_section_32 (bfd *ab
*** 1031,1039 ****
   {
     unsigned char buf[BFD_MACH_O_SECTION_SIZE];

!   bfd_seek (abfd, offset, SEEK_SET);
!   if (bfd_bread ((PTR) buf, BFD_MACH_O_SECTION_SIZE, abfd)
!       != BFD_MACH_O_SECTION_SIZE)
       return -1;

     memcpy (section->sectname, buf, 16);
--- 1125,1133 ----
   {
     unsigned char buf[BFD_MACH_O_SECTION_SIZE];

!   if (bfd_seek (abfd, offset, SEEK_SET) != 0
!       || (bfd_bread ((PTR) buf, BFD_MACH_O_SECTION_SIZE, abfd)
!           != BFD_MACH_O_SECTION_SIZE))
       return -1;

     memcpy (section->sectname, buf, 16);
*************** bfd_mach_o_scan_read_section_64 (bfd *ab
*** 1066,1074 ****
   {
     unsigned char buf[BFD_MACH_O_SECTION_64_SIZE];

!   bfd_seek (abfd, offset, SEEK_SET);
!   if (bfd_bread ((PTR) buf, BFD_MACH_O_SECTION_64_SIZE, abfd)
!       != BFD_MACH_O_SECTION_64_SIZE)
       return -1;

     memcpy (section->sectname, buf, 16);
--- 1160,1168 ----
   {
     unsigned char buf[BFD_MACH_O_SECTION_64_SIZE];

!   if (bfd_seek (abfd, offset, SEEK_SET) != 0
!       || (bfd_bread ((PTR) buf, BFD_MACH_O_SECTION_64_SIZE, abfd)
!           != BFD_MACH_O_SECTION_64_SIZE))
       return -1;

     memcpy (section->sectname, buf, 16);
*************** bfd_mach_o_scan_read_symtab_symbol (bfd
*** 1113,1119 ****
   				    unsigned long i)
   {
     bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
!   unsigned int wide = (mdata->header.version == 2);
     unsigned int symwidth = wide ? 16 : 12;
     bfd_vma symoff = sym->symoff + (i * symwidth);
     unsigned char buf[16];
--- 1207,1213 ----
   				    unsigned long i)
   {
     bfd_mach_o_data_struct *mdata = abfd->tdata.mach_o_data;
!   unsigned int wide = mach_o_wide_p (&mdata->header);
     unsigned int symwidth = wide ? 16 : 12;
     bfd_vma symoff = sym->symoff + (i * symwidth);
     unsigned char buf[16];
*************** bfd_mach_o_scan_read_symtab_symbol (bfd
*** 1126,1133 ****

     BFD_ASSERT (sym->strtab != NULL);

!   bfd_seek (abfd, symoff, SEEK_SET);
!   if (bfd_bread ((PTR) buf, symwidth, abfd) != symwidth)
       {
         fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: unable  
to read %d bytes at %lu\n",
   	       symwidth, (unsigned long) symoff);
--- 1220,1227 ----

     BFD_ASSERT (sym->strtab != NULL);

!   if (bfd_seek (abfd, symoff, SEEK_SET) != 0
!       || bfd_bread ((PTR) buf, symwidth, abfd) != symwidth)
       {
         fprintf (stderr, "bfd_mach_o_scan_read_symtab_symbol: unable  
to read %d bytes at %lu\n",
   	       symwidth, (unsigned long) symoff);
*************** bfd_mach_o_scan_read_symtab_symbol (bfd
*** 1136,1142 ****

     stroff = bfd_h_get_32 (abfd, buf);
     type = bfd_h_get_8 (abfd, buf + 4);
!   symtype = (type & 0x0e);
     section = bfd_h_get_8 (abfd, buf + 5);
     desc = bfd_h_get_16 (abfd, buf + 6);
     if (wide)
--- 1230,1236 ----

     stroff = bfd_h_get_32 (abfd, buf);
     type = bfd_h_get_8 (abfd, buf + 4);
!   symtype = type & BFD_MACH_O_N_TYPE;
     section = bfd_h_get_8 (abfd, buf + 5);
     desc = bfd_h_get_16 (abfd, buf + 6);
     if (wide)
*************** bfd_mach_o_scan_read_symtab_symbol (bfd
*** 1184,1190 ****
       {
         if (type & BFD_MACH_O_N_PEXT)
   	s->flags |= BSF_GLOBAL;
!
         if (type & BFD_MACH_O_N_EXT)
   	s->flags |= BSF_GLOBAL;

--- 1278,1284 ----
       {
         if (type & BFD_MACH_O_N_PEXT)
   	s->flags |= BSF_GLOBAL;
!
         if (type & BFD_MACH_O_N_EXT)
   	s->flags |= BSF_GLOBAL;

*************** bfd_mach_o_scan_read_symtab_symbol (bfd
*** 1194,1200 ****
         switch (symtype)
   	{
   	case BFD_MACH_O_N_UNDF:
! 	  s->section = bfd_und_section_ptr;
   	  break;
   	case BFD_MACH_O_N_PBUD:
   	  s->section = bfd_und_section_ptr;
--- 1288,1302 ----
         switch (symtype)
   	{
   	case BFD_MACH_O_N_UNDF:
!           if (type == (BFD_MACH_O_N_UNDF | BFD_MACH_O_N_EXT)
!               && s->value != 0)
!             {
!               /* A common symbol.  */
!               s->section = bfd_com_section_ptr;
!               s->flags = BSF_NO_FLAGS;
!             }
!           else
!             s->section = bfd_und_section_ptr;
   	  break;
   	case BFD_MACH_O_N_PBUD:
   	  s->section = bfd_und_section_ptr;
*************** bfd_mach_o_scan_read_symtab_strtab (bfd
*** 1263,1270 ****
     if (sym->strtab == NULL)
       return -1;

!   bfd_seek (abfd, sym->stroff, SEEK_SET);
!   if (bfd_bread ((PTR) sym->strtab, sym->strsize, abfd) != sym- 
 >strsize)
       {
         fprintf (stderr, "bfd_mach_o_scan_read_symtab_strtab: unable  
to read %lu bytes at %lu\n",
   	       sym->strsize, sym->stroff);
--- 1365,1372 ----
     if (sym->strtab == NULL)
       return -1;

!   if (bfd_seek (abfd, sym->stroff, SEEK_SET) != 0
!       || bfd_bread ((PTR) sym->strtab, sym->strsize, abfd) != sym- 
 >strsize)
       {
         fprintf (stderr, "bfd_mach_o_scan_read_symtab_strtab: unable  
to read %lu bytes at %lu\n",
   	       sym->strsize, sym->stroff);
*************** bfd_mach_o_scan_read_dysymtab_symbol (bf
*** 1317,1324 ****

     BFD_ASSERT (i < dysym->nindirectsyms);

!   bfd_seek (abfd, isymoff, SEEK_SET);
!   if (bfd_bread ((PTR) buf, 4, abfd) != 4)
       {
         fprintf (stderr, "bfd_mach_o_scan_read_dysymtab_symbol:  
unable to read %lu bytes at %lu\n",
   	       (unsigned long) 4, isymoff);
--- 1419,1426 ----

     BFD_ASSERT (i < dysym->nindirectsyms);

!   if (bfd_seek (abfd, isymoff, SEEK_SET) != 0
!       || bfd_bread ((PTR) buf, 4, abfd) != 4)
       {
         fprintf (stderr, "bfd_mach_o_scan_read_dysymtab_symbol:  
unable to read %lu bytes at %lu\n",
   	       (unsigned long) 4, isymoff);
*************** bfd_mach_o_scan_read_dylinker (bfd *abfd
*** 1378,1385 ****
     BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
   	      || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));

!   bfd_seek (abfd, command->offset + 8, SEEK_SET);
!   if (bfd_bread ((PTR) buf, 4, abfd) != 4)
       return -1;

     nameoff = bfd_h_get_32 (abfd, buf + 0);
--- 1480,1487 ----
     BFD_ASSERT ((command->type == BFD_MACH_O_LC_ID_DYLINKER)
   	      || (command->type == BFD_MACH_O_LC_LOAD_DYLINKER));

!   if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
!       || bfd_bread ((PTR) buf, 4, abfd) != 4)
       return -1;

     nameoff = bfd_h_get_32 (abfd, buf + 0);
*************** bfd_mach_o_scan_read_dylib (bfd *abfd, b
*** 1428,1435 ****
   	      || (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
   	      || (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB));

!   bfd_seek (abfd, command->offset + 8, SEEK_SET);
!   if (bfd_bread ((PTR) buf, 16, abfd) != 16)
       return -1;

     nameoff = bfd_h_get_32 (abfd, buf + 0);
--- 1530,1537 ----
   	      || (command->type == BFD_MACH_O_LC_LOAD_DYLIB)
   	      || (command->type == BFD_MACH_O_LC_LOAD_WEAK_DYLIB));

!   if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
!       || bfd_bread ((PTR) buf, 16, abfd) != 16)
       return -1;

     nameoff = bfd_h_get_32 (abfd, buf + 0);
*************** bfd_mach_o_scan_read_thread (bfd *abfd,
*** 1502,1510 ****
         if (offset >= command->len)
   	return -1;

!       bfd_seek (abfd, command->offset + offset, SEEK_SET);
!
!       if (bfd_bread ((PTR) buf, 8, abfd) != 8)
   	return -1;

         offset += 8 + bfd_h_get_32 (abfd, buf + 4) * 4;
--- 1604,1611 ----
         if (offset >= command->len)
   	return -1;

!       if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
!           || bfd_bread ((PTR) buf, 8, abfd) != 8)
   	return -1;

         offset += 8 + bfd_h_get_32 (abfd, buf + 4) * 4;
*************** bfd_mach_o_scan_read_thread (bfd *abfd,
*** 1526,1534 ****
         if (nflavours >= cmd->nflavours)
   	return -1;

!       bfd_seek (abfd, command->offset + offset, SEEK_SET);
!
!       if (bfd_bread ((PTR) buf, 8, abfd) != 8)
   	return -1;

         cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, buf);
--- 1627,1634 ----
         if (nflavours >= cmd->nflavours)
   	return -1;

!       if (bfd_seek (abfd, command->offset + offset, SEEK_SET) != 0
!           || bfd_bread ((PTR) buf, 8, abfd) != 8)
   	return -1;

         cmd->flavours[nflavours].flavour = bfd_h_get_32 (abfd, buf);
*************** bfd_mach_o_scan_read_dysymtab (bfd *abfd
*** 1597,1604 ****

     BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);

!   bfd_seek (abfd, command->offset + 8, SEEK_SET);
!   if (bfd_bread ((PTR) buf, 72, abfd) != 72)
       return -1;

     seg->ilocalsym = bfd_h_get_32 (abfd, buf + 0);
--- 1697,1704 ----

     BFD_ASSERT (command->type == BFD_MACH_O_LC_DYSYMTAB);

!   if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
!       || bfd_bread ((PTR) buf, 72, abfd) != 72)
       return -1;

     seg->ilocalsym = bfd_h_get_32 (abfd, buf + 0);
*************** bfd_mach_o_scan_read_symtab (bfd *abfd,
*** 1628,1642 ****
   {
     bfd_mach_o_symtab_command *seg = &command->command.symtab;
     unsigned char buf[16];
-   asection *bfdsec;
-   char *sname;
-   const char *prefix = "LC_SYMTAB.stabs";
-   int nlist_size = (bfd_mach_o_version (abfd) > 1) ? 16 : 12;

     BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);

!   bfd_seek (abfd, command->offset + 8, SEEK_SET);
!   if (bfd_bread ((PTR) buf, 16, abfd) != 16)
       return -1;

     seg->symoff = bfd_h_get_32 (abfd, buf);
--- 1728,1738 ----
   {
     bfd_mach_o_symtab_command *seg = &command->command.symtab;
     unsigned char buf[16];

     BFD_ASSERT (command->type == BFD_MACH_O_LC_SYMTAB);

!   if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
!       || bfd_bread ((PTR) buf, 16, abfd) != 16)
       return -1;

     seg->symoff = bfd_h_get_32 (abfd, buf);
*************** bfd_mach_o_scan_read_symtab (bfd *abfd,
*** 1646,1689 ****
     seg->symbols = NULL;
     seg->strtab = NULL;

-   sname = bfd_alloc (abfd, strlen (prefix) + 1);
-   if (sname == NULL)
-     return -1;
-   strcpy (sname, prefix);
-
-   bfdsec = bfd_make_section_anyway_with_flags (abfd, sname,  
SEC_HAS_CONTENTS);
-   if (bfdsec == NULL)
-     return -1;
-
-   bfdsec->vma = 0;
-   bfdsec->lma = 0;
-   bfdsec->size = seg->nsyms * nlist_size;
-   bfdsec->filepos = seg->symoff;
-   bfdsec->alignment_power = 0;
-
-   seg->stabs_segment = bfdsec;
-
     if (seg->nsyms != 0)
       abfd->flags |= HAS_SYMS;

-   prefix = "LC_SYMTAB.stabstr";
-   sname = bfd_alloc (abfd, strlen (prefix) + 1);
-   if (sname == NULL)
-     return -1;
-   strcpy (sname, prefix);
-
-   bfdsec = bfd_make_section_anyway_with_flags (abfd, sname,  
SEC_HAS_CONTENTS);
-   if (bfdsec == NULL)
-     return -1;
-
-   bfdsec->vma = 0;
-   bfdsec->lma = 0;
-   bfdsec->size = seg->strsize;
-   bfdsec->filepos = seg->stroff;
-   bfdsec->alignment_power = 0;
-
-   seg->stabstr_segment = bfdsec;
-
     return 0;
   }

--- 1742,1750 ----
*************** bfd_mach_o_scan_read_uuid (bfd *abfd, bf
*** 1697,1704 ****

     BFD_ASSERT (command->type == BFD_MACH_O_LC_UUID);

!   bfd_seek (abfd, command->offset + 8, SEEK_SET);
!   if (bfd_bread ((PTR) cmd->uuid, 16, abfd) != 16)
       return -1;

     sname = bfd_alloc (abfd, strlen (prefix) + 1);
--- 1758,1765 ----

     BFD_ASSERT (command->type == BFD_MACH_O_LC_UUID);

!   if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
!       || bfd_bread ((PTR) cmd->uuid, 16, abfd) != 16)
       return -1;

     sname = bfd_alloc (abfd, strlen (prefix) + 1);
*************** bfd_mach_o_scan_read_segment (bfd *abfd,
*** 1734,1741 ****
       {
         BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);

!       bfd_seek (abfd, command->offset + 8, SEEK_SET);
!       if (bfd_bread ((PTR) buf, 64, abfd) != 64)
   	return -1;

         memcpy (seg->segname, buf, 16);
--- 1795,1802 ----
       {
         BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT_64);

!       if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
!           || bfd_bread ((PTR) buf, 64, abfd) != 64)
   	return -1;

         memcpy (seg->segname, buf, 16);
*************** bfd_mach_o_scan_read_segment (bfd *abfd,
*** 1754,1761 ****
       {
         BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);

!       bfd_seek (abfd, command->offset + 8, SEEK_SET);
!       if (bfd_bread ((PTR) buf, 48, abfd) != 48)
   	return -1;

         memcpy (seg->segname, buf, 16);
--- 1815,1822 ----
       {
         BFD_ASSERT (command->type == BFD_MACH_O_LC_SEGMENT);

!       if (bfd_seek (abfd, command->offset + 8, SEEK_SET) != 0
!           || bfd_bread ((PTR) buf, 48, abfd) != 48)
   	return -1;

         memcpy (seg->segname, buf, 16);
*************** bfd_mach_o_scan_read_segment (bfd *abfd,
*** 1782,1791 ****
   	{
   	  bfd_vma segoff;
             if (wide)
!             segoff = command->offset + BFD_MACH_O_LC_SEGMENT_64_SIZE
                 + (i * BFD_MACH_O_SECTION_64_SIZE);
             else
!             segoff = command->offset + BFD_MACH_O_LC_SEGMENT_SIZE
                 + (i * BFD_MACH_O_SECTION_SIZE);

   	  if (bfd_mach_o_scan_read_section
--- 1843,1852 ----
   	{
   	  bfd_vma segoff;
             if (wide)
!             segoff = command->offset + BFD_MACH_O_LC_SEGMENT_64_SIZE
                 + (i * BFD_MACH_O_SECTION_64_SIZE);
             else
!             segoff = command->offset + BFD_MACH_O_LC_SEGMENT_SIZE
                 + (i * BFD_MACH_O_SECTION_SIZE);

   	  if (bfd_mach_o_scan_read_section
*************** bfd_mach_o_scan_read_command (bfd *abfd,
*** 1814,1821 ****
   {
     unsigned char buf[8];

!   bfd_seek (abfd, command->offset, SEEK_SET);
!   if (bfd_bread ((PTR) buf, 8, abfd) != 8)
       return -1;

     command->type = bfd_h_get_32 (abfd, buf) & ~BFD_MACH_O_LC_REQ_DYLD;
--- 1875,1882 ----
   {
     unsigned char buf[8];

!   if (bfd_seek (abfd, command->offset, SEEK_SET) != 0
!       || bfd_bread ((PTR) buf, 8, abfd) != 8)
       return -1;

     command->type = bfd_h_get_32 (abfd, buf) & ~BFD_MACH_O_LC_REQ_DYLD;
*************** bfd_mach_o_scan_start_address (bfd *abfd
*** 1969,1977 ****
   	{
   	  unsigned char buf[4];

! 	  bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET);
!
! 	  if (bfd_bread (buf, 4, abfd) != 4)
   	    return -1;

   	  abfd->start_address = bfd_h_get_32 (abfd, buf);
--- 2030,2037 ----
   	{
   	  unsigned char buf[4];

! 	  if (bfd_seek (abfd, cmd->flavours[i].offset + 40, SEEK_SET) != 0
!               || bfd_bread (buf, 4, abfd) != 4)
   	    return -1;

   	  abfd->start_address = bfd_h_get_32 (abfd, buf);
*************** bfd_mach_o_scan_start_address (bfd *abfd
*** 1981,1989 ****
   	{
   	  unsigned char buf[4];

! 	  bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET);
!
! 	  if (bfd_bread (buf, 4, abfd) != 4)
   	    return -1;

   	  abfd->start_address = bfd_h_get_32 (abfd, buf);
--- 2041,2048 ----
   	{
   	  unsigned char buf[4];

! 	  if (bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET) != 0
!               || bfd_bread (buf, 4, abfd) != 4)
   	    return -1;

   	  abfd->start_address = bfd_h_get_32 (abfd, buf);
*************** bfd_mach_o_scan_start_address (bfd *abfd
*** 1993,2001 ****
           {
             unsigned char buf[8];

!           bfd_seek (abfd, cmd->flavours[i].offset + 0, SEEK_SET);
!
!           if (bfd_bread (buf, 8, abfd) != 8)
               return -1;

             abfd->start_address = bfd_h_get_64 (abfd, buf);
--- 2052,2059 ----
           {
             unsigned char buf[8];

!           if (bfd_seek (abfd, cmd->flavours[i].offset + 0,  
SEEK_SET) != 0
!               || bfd_bread (buf, 8, abfd) != 8)
               return -1;

             abfd->start_address = bfd_h_get_64 (abfd, buf);
*************** bfd_mach_o_scan_start_address (bfd *abfd
*** 2005,2013 ****
           {
             unsigned char buf[8];

!           bfd_seek (abfd, cmd->flavours[i].offset + (16 * 8),  
SEEK_SET);
!
!           if (bfd_bread (buf, 8, abfd) != 8)
               return -1;

             abfd->start_address = bfd_h_get_64 (abfd, buf);
--- 2063,2070 ----
           {
             unsigned char buf[8];

!           if (bfd_seek (abfd, cmd->flavours[i].offset + (16 * 8),  
SEEK_SET) != 0
!               || bfd_bread (buf, 8, abfd) != 8)
               return -1;

             abfd->start_address = bfd_h_get_64 (abfd, buf);
*************** bfd_mach_o_scan (bfd *abfd,
*** 2027,2033 ****
     unsigned long cpusubtype;
     unsigned int hdrsize;

!   hdrsize = (header->version == 2) ?
       BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;

     mdata->header = *header;
--- 2084,2090 ----
     unsigned long cpusubtype;
     unsigned int hdrsize;

!   hdrsize = mach_o_wide_p (header) ?
       BFD_MACH_O_HEADER_64_SIZE : BFD_MACH_O_HEADER_SIZE;

     mdata->header = *header;
*************** bfd_mach_o_archive_p (bfd *abfd)
*** 2239,2246 ****
     unsigned char buf[20];
     unsigned long i;

!   bfd_seek (abfd, 0, SEEK_SET);
!   if (bfd_bread ((PTR) buf, 8, abfd) != 8)
       goto error;

     adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
--- 2296,2303 ----
     unsigned char buf[20];
     unsigned long i;

!   if (bfd_seek (abfd, 0, SEEK_SET) != 0
!       || bfd_bread ((PTR) buf, 8, abfd) != 8)
       goto error;

     adata = bfd_alloc (abfd, sizeof (mach_o_fat_data_struct));
*************** bfd_mach_o_archive_p (bfd *abfd)
*** 2257,2272 ****
     if (adata->nfat_arch > 30)
       goto error;

!   adata->archentries =
       bfd_alloc (abfd, adata->nfat_arch * sizeof  
(mach_o_fat_archentry));
     if (adata->archentries == NULL)
       goto error;

     for (i = 0; i < adata->nfat_arch; i++)
       {
!       bfd_seek (abfd, 8 + 20 * i, SEEK_SET);
!
!       if (bfd_bread ((PTR) buf, 20, abfd) != 20)
   	goto error;
         adata->archentries[i].cputype = bfd_getb32 (buf);
         adata->archentries[i].cpusubtype = bfd_getb32 (buf + 4);
--- 2314,2328 ----
     if (adata->nfat_arch > 30)
       goto error;

!   adata->archentries =
       bfd_alloc (abfd, adata->nfat_arch * sizeof  
(mach_o_fat_archentry));
     if (adata->archentries == NULL)
       goto error;

     for (i = 0; i < adata->nfat_arch; i++)
       {
!       if (bfd_seek (abfd, 8 + 20 * i, SEEK_SET) != 0
!           || bfd_bread ((PTR) buf, 20, abfd) != 20)
   	goto error;
         adata->archentries[i].cputype = bfd_getb32 (buf);
         adata->archentries[i].cpusubtype = bfd_getb32 (buf + 4);
*************** bfd_mach_o_fat_extract (bfd *abfd,
*** 2378,2384 ****
     if (!bfd_check_format (abfd, bfd_archive)
         || abfd->xvec != &mach_o_fat_vec)
       return NULL;
!
     /* This is a Mach-O fat image.  */
     adata = (mach_o_fat_data_struct *) abfd->tdata.mach_o_fat_data;
     BFD_ASSERT (adata != NULL);
--- 2434,2440 ----
     if (!bfd_check_format (abfd, bfd_archive)
         || abfd->xvec != &mach_o_fat_vec)
       return NULL;
!
     /* This is a Mach-O fat image.  */
     adata = (mach_o_fat_data_struct *) abfd->tdata.mach_o_fat_data;
     BFD_ASSERT (adata != NULL);
*************** bfd_mach_o_core_fetch_environment (bfd *
*** 2701,2708 ****
   	      buf = bfd_realloc_or_free (buf, size);
   	      if (buf == NULL)
   		return -1;
! 	
! 	      bfd_seek (abfd, end - size, SEEK_SET);
   	      nread = bfd_bread (buf, size, abfd);

   	      if (nread != size)
--- 2757,2769 ----
   	      buf = bfd_realloc_or_free (buf, size);
   	      if (buf == NULL)
   		return -1;
!
! 	      if (bfd_seek (abfd, end - size, SEEK_SET) != 0)
!                 {
!                   free (buf);
!                   return -1;
!                 }
!
   	      nread = bfd_bread (buf, size, abfd);

   	      if (nread != size)
Index: bfd/mach-o.h
===================================================================
RCS file: /cvs/src/src/bfd/mach-o.h,v
retrieving revision 1.13
diff -c -p -r1.13 mach-o.h
*** bfd/mach-o.h	5 Jun 2009 09:19:44 -0000	1.13
--- bfd/mach-o.h	8 Jun 2009 12:46:26 -0000
*************** typedef struct bfd_mach_o_symtab_command
*** 310,317 ****
     unsigned long strsize;
     asymbol *symbols;
     char *strtab;
-   asection *stabs_segment;
-   asection *stabstr_segment;
   }
   bfd_mach_o_symtab_command;

--- 310,315 ----
*************** const bfd_target *bfd_mach_o_header_p (b
*** 598,606 ****
   bfd_boolean bfd_mach_o_build_commands (bfd *abfd);
   bfd_boolean bfd_mach_o_set_section_contents (bfd *, asection *,  
const void *,
                                                file_ptr,  
bfd_size_type);

- extern const bfd_target mach_o_be_vec;
- extern const bfd_target mach_o_le_vec;
   extern const bfd_target mach_o_fat_vec;

   #endif /* _BFD_MACH_O_H_ */
--- 596,603 ----
   bfd_boolean bfd_mach_o_build_commands (bfd *abfd);
   bfd_boolean bfd_mach_o_set_section_contents (bfd *, asection *,  
const void *,
                                                file_ptr,  
bfd_size_type);
+ unsigned int bfd_mach_o_version (bfd *);

   extern const bfd_target mach_o_fat_vec;

   #endif /* _BFD_MACH_O_H_ */



More information about the Binutils mailing list