From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mx2.suse.de (mx2.suse.de [195.135.220.15]) by sourceware.org (Postfix) with ESMTPS id 042E33861936 for ; Tue, 9 Mar 2021 11:36:05 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 042E33861936 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=suse.de Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=tdevries@suse.de X-Virus-Scanned: by amavisd-new at test-mx.suse.de Received: from relay2.suse.de (unknown [195.135.221.27]) by mx2.suse.de (Postfix) with ESMTP id 0D92DAB8C; Tue, 9 Mar 2021 11:36:04 +0000 (UTC) Date: Tue, 9 Mar 2021 12:36:02 +0100 From: Tom de Vries To: dwz@sourceware.org, jakub@redhat.com, mark@klomp.org Subject: [PATCH] Break up main Message-ID: <20210309113600.GA28218@delia> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.10.1 (2018-07-13) X-Spam-Status: No, score=-12.3 required=5.0 tests=BAYES_00, GIT_PATCH_0, KAM_DMARC_STATUS, RCVD_IN_MSPIKE_H3, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: dwz@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Dwz mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 09 Mar 2021 11:36:08 -0000 Hi, Factor functions parse_args, dwz_one_file and dwz_files out of main. Any comments? Thanks, - Tom Break up main 2021-03-09 Tom de Vries * dwz.c (dwz): Make files parameter a const char**. (parse_args, dwz_one_file, dwz_files): New function, factored out of ... (main): ... here. --- dwz.c | 304 ++++++++++++++++++++++++++++++++++++++---------------------------- 1 file changed, 176 insertions(+), 128 deletions(-) diff --git a/dwz.c b/dwz.c index 9a8e237..5975cc7 100644 --- a/dwz.c +++ b/dwz.c @@ -15325,7 +15325,7 @@ struct file_result over FILE. */ static int dwz (const char *file, const char *outfile, struct file_result *res, - struct file_result *resa, char **files) + struct file_result *resa, const char **files) { DSO *dso; int ret = 0, fd; @@ -16648,25 +16648,18 @@ version (void) exit (0); } -int -main (int argc, char *argv[]) +/* Parse command line arguments in ARGV. */ +static void +parse_args (int argc, char *argv[], bool *hardlink, const char **outfile) { - const char *outfile = NULL; - int ret = 0; - int i; unsigned long l; char *end; - struct file_result res; - bool hardlink = false; - const char *file; - - if (elf_version (EV_CURRENT) == EV_NONE) - error (1, 0, "library out of date"); while (1) { int option_index = -1; - int c = getopt_long (argc, argv, "m:o:qhl:L:M:r?v5", dwz_options, &option_index); + int c = getopt_long (argc, argv, "m:o:qhl:L:M:r?v5", dwz_options, + &option_index); if (c == -1) break; switch (c) @@ -16734,7 +16727,7 @@ main (int argc, char *argv[]) break; case 'o': - outfile = optarg; + *outfile = optarg; break; case 'm': @@ -16746,7 +16739,7 @@ main (int argc, char *argv[]) break; case 'h': - hardlink = true; + *hardlink = true; break; case 'M': @@ -16803,138 +16796,193 @@ main (int argc, char *argv[]) if (multifile_relative && multifile_name) error (1, 0, "-M and -r options can't be specified together"); +} - if (optind == argc || optind + 1 == argc) +/* Dwarf-compress FILE. If OUTFILE, write to result to OUTFILE, otherwise + modify FILE. */ +static int +dwz_one_file (const char *file, const char *outfile) +{ + int ret = 0; + struct file_result res; + + if (stats_p) + init_stats (file); + + res.die_count = 0; + + ret = (low_mem_die_limit == 0 + ? 2 + : dwz (file, outfile, &res, NULL, NULL)); + + if (ret == 2) { - file = optind == argc ? "a.out" : argv[optind]; - if (multifile != NULL) - { - error (0, 0, "Too few files for multifile optimization"); + multifile_mode = MULTIFILE_MODE_LOW_MEM; + ret = dwz (file, outfile, &res, NULL, NULL); + } + + return ret; +} + +/* Dwarf-compress FILES. If HARDLINK, detect if some files are hardlinks and + if so, dwarf-compress just one, and relink the others. */ +static int +dwz_files (int nr_files, const char *files[], bool hardlink) +{ + int ret = 0; + int i; + const char *file; + struct file_result *resa; + bool hardlinks = false; + int successcount = 0; + + resa = (struct file_result *) malloc ((nr_files) * sizeof (*resa)); + if (resa == NULL) + error (1, ENOMEM, "failed to allocate result array"); + for (i = 0; i < nr_files; ++i) + resa[i].die_count = 0; + + if (multifile) + { + multi_info_fd = make_temp_file ("dwz.debug_info"); + multi_abbrev_fd = make_temp_file ("dwz.debug_abbrev"); + multi_line_fd = make_temp_file ("dwz.debug_line"); + multi_str_fd = make_temp_file ("dwz.debug_str"); + multi_macro_fd = make_temp_file ("dwz.debug_macro"); + if (multi_info_fd == -1 + || multi_abbrev_fd == -1 + || multi_line_fd == -1 + || multi_str_fd == -1 + || multi_macro_fd == -1) + { + error (0, 0, "Could not create multifile temporary files"); multifile = NULL; } + } + + for (i = 0; i < nr_files; i++) + { + int thisret; + file = files[i]; if (stats_p) init_stats (file); - res.die_count = 0; - ret = (low_mem_die_limit == 0 - ? 2 - : dwz (file, outfile, &res, NULL, NULL)); - if (ret == 2) + thisret = (low_mem_die_limit == 0 + ? 2 + : dwz (file, NULL, &resa[i], + hardlinks ? resa : NULL, files)); + if (thisret == 2) { multifile_mode = MULTIFILE_MODE_LOW_MEM; - ret = dwz (file, outfile, &res, NULL, NULL); + thisret = dwz (file, NULL, &resa[i], + hardlinks ? resa : NULL, files); } + else if (thisret == 1) + ret = 1; + else if (resa[i].res >= 0) + successcount++; + if (hardlink + && resa[i].res >= 0 + && resa[i].nlink > 1) + hardlinks = true; } - else + + if (multifile && successcount < 2) { - int nr_files = argc - optind; - struct file_result *resa - = (struct file_result *) malloc ((nr_files) * sizeof (*resa)); - bool hardlinks = false; - int successcount = 0; - - for (i = 0; i < nr_files; ++i) - resa[i].die_count = 0; - if (resa == NULL) - error (1, ENOMEM, "failed to allocate result array"); - if (outfile != NULL) - error (1, 0, "-o option not allowed for multiple files"); - if (multifile) - { - multi_info_fd = make_temp_file ("dwz.debug_info"); - multi_abbrev_fd = make_temp_file ("dwz.debug_abbrev"); - multi_line_fd = make_temp_file ("dwz.debug_line"); - multi_str_fd = make_temp_file ("dwz.debug_str"); - multi_macro_fd = make_temp_file ("dwz.debug_macro"); - if (multi_info_fd == -1 - || multi_abbrev_fd == -1 - || multi_line_fd == -1 - || multi_str_fd == -1 - || multi_macro_fd == -1) - { - error (0, 0, "Could not create multifile temporary files"); - multifile = NULL; - } - } - for (i = optind; i < argc; i++) + error (0, 0, "Too few files for multifile optimization"); + multifile = NULL; + } + + if (multifile + && multi_info_off == 0 && multi_str_off == 0 && multi_macro_off == 0) + { + if (!quiet) + error (0, 0, "No suitable DWARF found for multifile optimization"); + multifile = NULL; + } + + if (multifile) + { + unsigned int multifile_die_count = 0; + int multi_fd = optimize_multifile (&multifile_die_count); + DSO *dso; + if (multi_fd == -1) + return 1; + dso = read_multifile (multi_fd, multifile_die_count); + if (dso == NULL) + ret = 1; + else { - int thisret; - file = argv[i]; - if (stats_p) - init_stats (file); - thisret = (low_mem_die_limit == 0 - ? 2 - : dwz (file, NULL, &resa[i - optind], - hardlinks ? resa : NULL, &argv[optind])); - if (thisret == 2) + for (i = 0; i < nr_files; i++) { - multifile_mode = MULTIFILE_MODE_LOW_MEM; - thisret = dwz (file, NULL, &resa[i - optind], - hardlinks ? resa : NULL, &argv[optind]); + dw_cu_ref cu; + file = files[i]; + if (stats_p) + init_stats (file); + multifile_mode = MULTIFILE_MODE_FI; + /* Don't process again files that couldn't + be processed successfully. */ + if (resa[i].res == -1) + continue; + for (cu = alt_first_cu; cu; cu = cu->cu_next) + alt_clear_dups (cu->cu_die); + ret |= dwz (file, NULL, &resa[i], + hardlinks ? resa : NULL, files); } - else if (thisret == 1) - ret = 1; - else if (resa[i - optind].res >= 0) - successcount++; - if (hardlink - && resa[i - optind].res >= 0 - && resa[i - optind].nlink > 1) - hardlinks = true; + elf_end (dso->elf); + close (multi_fd); + free (dso); } - if (multifile && successcount < 2) + cleanup (); + strp_htab = alt_strp_htab; + off_htab = alt_off_htab; + dup_htab = alt_dup_htab; + macro_htab = alt_macro_htab; + pool = alt_pool; + ob = alt_ob; + ob2 = alt_ob2; + cleanup (); + } + + free (resa); + + return ret; +} + +int +main (int argc, char *argv[]) +{ + int ret; + const char *outfile; + bool hardlink; + + if (elf_version (EV_CURRENT) == EV_NONE) + error (1, 0, "library out of date"); + + outfile = NULL; + hardlink = false; + parse_args (argc, argv, &hardlink, &outfile); + + if (optind == argc || optind + 1 == argc) + { + const char *file = optind == argc ? "a.out" : argv[optind]; + + if (multifile != NULL) { error (0, 0, "Too few files for multifile optimization"); multifile = NULL; } - if (multifile - && multi_info_off == 0 && multi_str_off == 0 && multi_macro_off == 0) - { - if (!quiet) - error (0, 0, "No suitable DWARF found for multifile optimization"); - multifile = NULL; - } - if (multifile) - { - unsigned int multifile_die_count = 0; - int multi_fd = optimize_multifile (&multifile_die_count); - DSO *dso; - if (multi_fd == -1) - return 1; - dso = read_multifile (multi_fd, multifile_die_count); - if (dso == NULL) - ret = 1; - else - { - for (i = optind; i < argc; i++) - { - dw_cu_ref cu; - file = argv[i]; - if (stats_p) - init_stats (file); - multifile_mode = MULTIFILE_MODE_FI; - /* Don't process again files that couldn't - be processed successfully. */ - if (resa[i - optind].res == -1) - continue; - for (cu = alt_first_cu; cu; cu = cu->cu_next) - alt_clear_dups (cu->cu_die); - ret |= dwz (file, NULL, &resa[i - optind], - hardlinks ? resa : NULL, &argv[optind]); - } - elf_end (dso->elf); - close (multi_fd); - free (dso); - } - cleanup (); - strp_htab = alt_strp_htab; - off_htab = alt_off_htab; - dup_htab = alt_dup_htab; - macro_htab = alt_macro_htab; - pool = alt_pool; - ob = alt_ob; - ob2 = alt_ob2; - cleanup (); - } - free (resa); + + ret = dwz_one_file (file, outfile); + } + else + { + int nr_files = argc - optind; + const char **files = (const char **)&argv[optind]; + + if (outfile != NULL) + error (1, 0, "-o option not allowed for multiple files"); + + ret = dwz_files (nr_files, files, hardlink); } if (stats_p)