mach-o: layout executables

bfd/
	* mach-o.h (bfd_mach_o_dyld_info_command): Add rebase_content,
	bind_content, weak_bind_content, lazy_bind_content,
	export_content.
	(bfd_mach_o_load_command): Add comments, add next field.
	(mach_o_data_struct): Replace commands field by first_command
	and last_command.
	* mach-o.c (bfd_mach_o_append_command): New function.
	(bfd_mach_o_bfd_copy_private_symbol_data): Add blank lines.
	(bfd_mach_o_bfd_copy_private_section_data): Check flavour,
	copy fields.
	(bfd_mach_o_bfd_copy_private_header_data): Copy load commands.
	(bfd_mach_o_pad4, bfd_mach_o_pad_command): New functions.
	(bfd_mach_o_write_thread): Use macro instead of literal.
	(bfd_mach_o_write_dylinker, bfd_mach_o_write_dylib)
	(bfd_mach_o_write_main, bfd_mach_o_write_dyld_info): New
	functions.
	(bfd_mach_o_write_symtab_content): New function (extracted
	from bfd_mach_o_write_symtab).
	(bfd_mach_o_write_symtab): Split.
	(bfd_mach_o_count_indirect_symbols): Move
	(bfd_mach_o_build_dysymtab): Remove layout code.
	(bfd_mach_o_write_contents): Rewritten to build commands in order.
	(bfd_mach_o_count_sections_for_seg): Remove.
	(bfd_mach_o_build_obj_seg_command): New function (extracted from
	bfd_mach_o_build_seg_command).
	(bfd_mach_o_build_exec_seg_command): New function.
	(bfd_mach_o_build_dysymtab_command): Remove.
	(bfd_mach_o_layout_commands): New function.
	(bfd_mach_o_init_segment): New function.
	(bfd_mach_o_build_commands): Major rework to handle non-object
	files.
	(bfd_mach_o_alloc_and_read, bfd_mach_o_read_dyld_content): New
	function.
	(bfd_mach_o_read_dyld_info): Clear content fields.
	(bfd_mach_o_read_segment): Adjust call.
	(bfd_mach_o_flatten_sections): Adjust as now load commands are
	chained.
	(bfd_mach_o_scan_start_address, bfd_mach_o_scan)
	(bfd_mach_o_mkobject_init, bfd_mach_o_get_base_address)
	(bfd_mach_o_lookup_command, bfd_mach_o_core_fetch_environment):
	Likewise.

binutils/
	* od-macho.c (dump_section_map): Adjust as load commands
	are now chained.
	(dump_load_command, dump_section_content): Likewise.
This commit is contained in:
Tristan Gingold 2014-04-03 14:45:31 +02:00
parent 967b2c539a
commit c9ffd2eaf8
5 changed files with 1170 additions and 467 deletions

View File

@ -1,3 +1,47 @@
2014-04-18 Tristan Gingold <gingold@adacore.com>
* mach-o.h (bfd_mach_o_dyld_info_command): Add rebase_content,
bind_content, weak_bind_content, lazy_bind_content,
export_content.
(bfd_mach_o_load_command): Add comments, add next field.
(mach_o_data_struct): Replace commands field by first_command
and last_command.
* mach-o.c (bfd_mach_o_append_command): New function.
(bfd_mach_o_bfd_copy_private_symbol_data): Add blank lines.
(bfd_mach_o_bfd_copy_private_section_data): Check flavour,
copy fields.
(bfd_mach_o_bfd_copy_private_header_data): Copy load commands.
(bfd_mach_o_pad4, bfd_mach_o_pad_command): New functions.
(bfd_mach_o_write_thread): Use macro instead of literal.
(bfd_mach_o_write_dylinker, bfd_mach_o_write_dylib)
(bfd_mach_o_write_main, bfd_mach_o_write_dyld_info): New
functions.
(bfd_mach_o_write_symtab_content): New function (extracted
from bfd_mach_o_write_symtab).
(bfd_mach_o_write_symtab): Split.
(bfd_mach_o_count_indirect_symbols): Move
(bfd_mach_o_build_dysymtab): Remove layout code.
(bfd_mach_o_write_contents): Rewritten to build commands in order.
(bfd_mach_o_count_sections_for_seg): Remove.
(bfd_mach_o_build_obj_seg_command): New function (extracted from
bfd_mach_o_build_seg_command).
(bfd_mach_o_build_exec_seg_command): New function.
(bfd_mach_o_build_dysymtab_command): Remove.
(bfd_mach_o_layout_commands): New function.
(bfd_mach_o_init_segment): New function.
(bfd_mach_o_build_commands): Major rework to handle non-object
files.
(bfd_mach_o_alloc_and_read, bfd_mach_o_read_dyld_content): New
function.
(bfd_mach_o_read_dyld_info): Clear content fields.
(bfd_mach_o_read_segment): Adjust call.
(bfd_mach_o_flatten_sections): Adjust as now load commands are
chained.
(bfd_mach_o_scan_start_address, bfd_mach_o_scan)
(bfd_mach_o_mkobject_init, bfd_mach_o_get_base_address)
(bfd_mach_o_lookup_command, bfd_mach_o_core_fetch_environment):
Likewise.
2014-04-18 Tristan Gingold <gingold@adacore.com>
* mach-o-target.c (bfd_mach_o_bfd_copy_private_header_data):

File diff suppressed because it is too large Load Diff

View File

@ -488,22 +488,27 @@ typedef struct bfd_mach_o_dyld_info_command
/* File offset and size to rebase info. */
unsigned int rebase_off;
unsigned int rebase_size;
unsigned char *rebase_content;
/* File offset and size of binding info. */
unsigned int bind_off;
unsigned int bind_size;
unsigned char *bind_content;
/* File offset and size of weak binding info. */
unsigned int weak_bind_off;
unsigned int weak_bind_size;
unsigned char *weak_bind_content;
/* File offset and size of lazy binding info. */
unsigned int lazy_bind_off;
unsigned int lazy_bind_size;
unsigned char *lazy_bind_content;
/* File offset and size of export info. */
unsigned int export_off;
unsigned int export_size;
unsigned char *export_content;
}
bfd_mach_o_dyld_info_command;
@ -543,10 +548,17 @@ bfd_mach_o_source_version_command;
typedef struct bfd_mach_o_load_command
{
/* Next command in the single linked list. */
struct bfd_mach_o_load_command *next;
/* Type and required flag. */
bfd_mach_o_load_command_type type;
bfd_boolean type_required;
/* Offset and length in the file. */
unsigned int offset;
unsigned int len;
union
{
bfd_mach_o_segment_command segment;
@ -567,8 +579,7 @@ typedef struct bfd_mach_o_load_command
bfd_mach_o_fvmlib_command fvmlib;
bfd_mach_o_main_command main;
bfd_mach_o_source_version_command source_version;
}
command;
} command;
}
bfd_mach_o_load_command;
@ -577,7 +588,8 @@ typedef struct mach_o_data_struct
/* Mach-O header. */
bfd_mach_o_header header;
/* Array of load commands (length is given by header.ncmds). */
bfd_mach_o_load_command *commands;
bfd_mach_o_load_command *first_command;
bfd_mach_o_load_command *last_command;
/* Flatten array of sections. The array is 0-based. */
unsigned long nsects;

View File

@ -1,3 +1,9 @@
2014-04-18 Tristan Gingold <gingold@adacore.com>
* od-macho.c (dump_section_map): Adjust as load commands
are now chained.
(dump_load_command, dump_section_content): Likewise.
2014-04-16 Tristan Gingold <gingold@adacore.com>
* od-macho.c (OPT_DYLD_INFO): New macro.

View File

@ -317,22 +317,22 @@ static void
dump_section_map (bfd *abfd)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
unsigned int i;
bfd_mach_o_load_command *cmd;
unsigned int sec_nbr = 0;
fputs (_("Segments and Sections:\n"), stdout);
fputs (_(" #: Segment name Section name Address\n"), stdout);
for (i = 0; i < mdata->header.ncmds; i++)
for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
{
bfd_mach_o_segment_command *seg;
bfd_mach_o_section *sec;
if (mdata->commands[i].type != BFD_MACH_O_LC_SEGMENT
&& mdata->commands[i].type != BFD_MACH_O_LC_SEGMENT_64)
if (cmd->type != BFD_MACH_O_LC_SEGMENT
&& cmd->type != BFD_MACH_O_LC_SEGMENT_64)
continue;
seg = &mdata->commands[i].command.segment;
seg = &cmd->command.segment;
printf ("[Segment %-16s ", seg->segname);
printf_vma (seg->vmaddr);
@ -1644,12 +1644,11 @@ static void
dump_load_commands (bfd *abfd, unsigned int cmd32, unsigned int cmd64)
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
bfd_mach_o_load_command *cmd;
unsigned int i;
for (i = 0; i < mdata->header.ncmds; i++)
for (cmd = mdata->first_command, i = 0; cmd != NULL; cmd = cmd->next, i++)
{
bfd_mach_o_load_command *cmd = &mdata->commands[i];
if (cmd32 == 0)
dump_load_command (abfd, cmd, i, FALSE);
else if (cmd->type == cmd32 || cmd->type == cmd64)
@ -2070,11 +2069,10 @@ dump_section_content (bfd *abfd,
void (*dump)(bfd*, const unsigned char*, bfd_size_type))
{
bfd_mach_o_data_struct *mdata = bfd_mach_o_get_data (abfd);
unsigned int i;
bfd_mach_o_load_command *cmd;
for (i = 0; i < mdata->header.ncmds; i++)
for (cmd = mdata->first_command; cmd != NULL; cmd = cmd->next)
{
bfd_mach_o_load_command *cmd = &mdata->commands[i];
if (cmd->type == BFD_MACH_O_LC_SEGMENT
|| cmd->type == BFD_MACH_O_LC_SEGMENT_64)
{