Skip to content

Commit

Permalink
More comments and error messages.
Browse files Browse the repository at this point in the history
  • Loading branch information
culler committed Feb 8, 2021
1 parent 3850d85 commit bd6a71b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 18 deletions.
20 changes: 14 additions & 6 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -17,19 +17,28 @@ what is available without this option.

Here is a list of the available commands:

help
$ macher help

Prints usage information.

version
$ macher version

Prints the version number.

segments
$ macher [-options] segments <Mach-O file path>
$ macher [-options] segments <Mach-O file path>

Prints information about each segment in the Mach-O file. In verbose mode,
the sections within each segment are listed, along with byte ranges showing
the location of the segment within the file.
Prints information about each segment in the Mach-O file. In verbose mode,
the sections within each segment are listed.

commands
$ macher [-options] commands <Mach-O file path>

Prints information about each load command in the Mach-O file. This
includes the load commands which define segments. The verbose mode provides
additional details about the command. This is similar to otool -l but
additional details about the commands. This is similar to otool -l but
generates output which is more readable and amenable to being parsed by a
script.

Expand Down Expand Up @@ -87,4 +96,3 @@ set_id
LC_ID_DYLIB load command exists only for dylib files. When another
executable is linked with the dylib, the linker copies the id into an
LC_DYLIB command for the executable.

30 changes: 18 additions & 12 deletions macher.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,8 @@ static void init_mach_o(mach_o_obj *mach_o, char *path, char *mode)
if (mach_o->verbose) {
struct stat st;
stat(mach_o->path, &st);
printf("Mach-O magic number is 0x%x.\n", magic);
printf("Mach-O file size is %llu.\n", st.st_size);
printf("The Mach-O magic number is 0x%x.\n", magic);
printf("The Mach-O file size is %llu.\n", st.st_size);
}
/*
* Read the Mach-O header.
Expand All @@ -137,7 +137,7 @@ static void init_mach_o(mach_o_obj *mach_o, char *path, char *mode)
mach_o->command_block_size = header->sizeofcmds;
if (mach_o->verbose) {
printf("The Mach-O header occupies %d bytes\n", mach_o->header_size);
printf("%u bytes are being used to store %u load commands.\n",
printf("Currently %u bytes are being used to store %u load commands.\n",
header->sizeofcmds, header->ncmds);
}
} else {
Expand Down Expand Up @@ -349,7 +349,7 @@ static void compute_command_space(mach_o_obj *mach_o)
if (command_space >= 0) {
mach_o->command_space = command_space;
if (mach_o->verbose) {
printf("%lu bytes are available for storing load commands.\n\n",
printf("A total of %lu bytes are available for storing load commands.\n\n",
mach_o->command_space);
}
} else {
Expand Down Expand Up @@ -708,19 +708,21 @@ static int set_id(mach_o_obj *mach_o, mach_o_command *command, char *idpath)
static void usage()
{
printf("Usage: \n");
printf(" mach_o [-v] segments <mach-O file>\n");
printf(" mach_o [-v] commands <mach-O file>\n");
printf(" mach_o [-v] append <datafile> <mach-O file>\n");
printf(" mach_o [-v] add_rpath <library search path> <Mach-O file path>\n");
printf(" mach_o [-v] remove_rpath <library search path> <Mach-O file path>\n");
printf(" mach_o [-v] set_libpath <library path> <Mach-O file path>\n");
printf(" mach_o [-v] set_id <library id path> <Mach-O file path>\n");
printf(" mach_o [-v|--verbose] help\n");
printf(" mach_o [-v|--verbose] version\n");
printf(" mach_o [-v|--verbose] segments <mach-O file>\n");
printf(" mach_o [-v|--verbose] commands <mach-O file>\n");
printf(" mach_o [-v|--verbose] append <datafile> <mach-O file>\n");
printf(" mach_o [-v|--verbose] add_rpath <library dir> <Mach-O file path>\n");
printf(" mach_o [-v|--verbose] remove_rpath <library dir> <Mach-O file path>\n");
printf(" mach_o [-v|--verbose] set_libpath <library path> <Mach-O file path>\n");
printf(" mach_o [-v|--verbose] set_id <library path> <Mach-O file path>\n");
exit(1);
}

typedef int (*action_op)(mach_o_obj *mach_o, mach_o_command *command, char *arg);

typedef enum {HELP=1, SEGMENTS, COMMANDS, APPEND, ADD_RPATH, REMOVE_RPATH,
typedef enum {HELP=1, VERSION, SEGMENTS, COMMANDS, APPEND, ADD_RPATH, REMOVE_RPATH,
EDIT_LIBPATH, SET_ID} action_id;

typedef struct {
Expand All @@ -731,6 +733,7 @@ typedef struct {

static mach_o_action actions[] = {
{.id = HELP, .name = "help", .op = NULL},
{.id = VERSION, .name = "version", .op = NULL},
{.id = COMMANDS, .name = "commands", .op = print_command},
{.id = SEGMENTS, .name = "segments", .op = print_segment},
{.id = APPEND, .name = "append", .op = append_data},
Expand Down Expand Up @@ -792,6 +795,9 @@ int main(int argc, char **argv)
case HELP:
usage();
break;
case VERSION:
printf("This is version 1.0 of macher.\n");
exit(0);
case APPEND:
case ADD_RPATH:
case REMOVE_RPATH:
Expand Down

0 comments on commit bd6a71b

Please sign in to comment.