From patchwork Tue Mar 14 02:04:38 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Alan Modra X-Patchwork-Id: 69244 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp1514985wrd; Mon, 13 Mar 2023 19:05:06 -0700 (PDT) X-Google-Smtp-Source: AK7set8cL/A57u44r9nwTfZS4/02hIgMwN7WZv4yc55Kn6N1EMQ7n1X1PhEDtJOB7NGZd3t++kUQ X-Received: by 2002:a50:e616:0:b0:4fb:eda4:c093 with SMTP id y22-20020a50e616000000b004fbeda4c093mr6813492edm.13.1678759506875; Mon, 13 Mar 2023 19:05:06 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678759506; cv=none; d=google.com; s=arc-20160816; b=syvtUfGs+hqxq8mOJdRdAmZS5QfqiJPQSJtW46HUcMqr1cZJf/NHkXtcI/kSJXKuTh FmJuWXXoYfwGViCTmfIN8iNeFJiCiVuWDarVHJpUULBkfTbDU64uDaLDOeoAkGznyWzJ kTIFPrDEJv1oRkxGWmyD9Zl1hoC3x+1H8V9SJ7W/nhmA6qMyjR6uc1Xd7+B5ARnG/nXe waQj8FSx8vjoyHHBuO85DCQrmneMOVgDZeb/oQsls75EXgeWsw2E8JscT1BOGawpNOMq +J3Sl7g3Xy6Vn0yEmssxB2aG/zclkuRmXeQhMKxJ7bw5PfzHy02RQOZJehUtCxVJ8SqD uPaQ== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-disposition:mime-version:message-id:subject:to:date :dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=Tzsd98qc5x+6E/qAJTphRXwxebekGIMS0oREgrJZHO0=; b=EjR7hdey0qGG0Uk5QXi95faDEHX5mwsQp7ScxDA9HD/1IjyNJ4Up3Qhvc2pt24b/vo 8W8R2k+8mhS08C63mmdxSAKyr8nGcn/jNAoiqGRwbR+96gzsuRKYO+8Er1tWwzp2Hxcn UtzR6MZEVU5X3Zt1+90ALM3RaqxxpgR/pcFJlpW+8JUGdJ2cm8yRVoKsWxsSn9Es98wp jAlD7nXocDrRAFHgwAPha75c+rJu7BCfM4/b616bqvLfSjJAWg8Ij4VYqc1I6EyH+r00 CLgDnG0aIDn4GLL0s91IWyzCfWca1ONGuP0ZHzww5Qd9/SBZukut5JwUFCdR1vDqfWpP KMlQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=naGti34n; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from sourceware.org (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id a22-20020a50ff16000000b004faccfa320esi1080856edu.520.2023.03.13.19.05.06 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Mar 2023 19:05:06 -0700 (PDT) Received-SPF: pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@sourceware.org header.s=default header.b=naGti34n; spf=pass (google.com: domain of binutils-bounces+ouuuleilei=gmail.com@sourceware.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="binutils-bounces+ouuuleilei=gmail.com@sourceware.org"; dmarc=pass (p=NONE sp=NONE dis=NONE) header.from=sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 12D52385B52E for ; Tue, 14 Mar 2023 02:04:54 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 12D52385B52E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=sourceware.org; s=default; t=1678759494; bh=Tzsd98qc5x+6E/qAJTphRXwxebekGIMS0oREgrJZHO0=; h=Date:To:Subject:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=naGti34n9t2CN175Ur9ugpD6zH2juHpZ3Lj4iDjxg5bb19s+9/iQc0r/7SwfRVwpp zxHZGW052T5a6EqrzpUnjMe7bKB9jt3d3mywMaCtkNbkigO3zV6ty0A39Ymg8baz6e 8bS9LvVeE0RH9Gjos914UkijK7pQtiYhUwKWM9Vk= X-Original-To: binutils@sourceware.org Delivered-To: binutils@sourceware.org Received: from mail-pj1-x1033.google.com (mail-pj1-x1033.google.com [IPv6:2607:f8b0:4864:20::1033]) by sourceware.org (Postfix) with ESMTPS id 1CC7A385B526 for ; Tue, 14 Mar 2023 02:04:42 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 1CC7A385B526 Received: by mail-pj1-x1033.google.com with SMTP id cn6so1316304pjb.2 for ; Mon, 13 Mar 2023 19:04:42 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1678759481; h=content-disposition:mime-version:message-id:subject:to:from:date :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=Tzsd98qc5x+6E/qAJTphRXwxebekGIMS0oREgrJZHO0=; b=P30Z0LiqrVDDtVRIrvwZFOA503Qfcm7auTDZ4OcFs4Ys6dJJCjrAmK3N8pte644gT6 u5smlCKG3SOwpvnTvIRmq/EGmvYhcGjnooZGyDxPzhQMmZwwSKnbJelhLG+jN8BF9/gn 7oiWbNA7ppmSqpv+KdnbRkC2PJ+WfDZCPRxrhmZgT2XY9XvbRrR4Q6Wdz744O/Ru5vwT 1NCU4V/+9jgjoycFfTzXp08lnkhJ1xYLGMcRU+/J5HQLbC3tyCZuQezesvepOUKrSZtY Gyf4l738ognKhzV+daMIPW0575wFUctNDAGJuhJNaUPzQhHMtWVGElB1wj+26xQQX8b2 qRdg== X-Gm-Message-State: AO0yUKVPku1169l7HYNaJnRZ7wlWzKavP+e9Hl0fMk0tdVynzt2xN7D2 dNfRYuu9VHg65doBXik2ZO+xBdbnNyM= X-Received: by 2002:a17:903:11c4:b0:19e:f315:98d with SMTP id q4-20020a17090311c400b0019ef315098dmr22420150plh.43.1678759480694; Mon, 13 Mar 2023 19:04:40 -0700 (PDT) Received: from squeak.grove.modra.org (158.106.96.58.static.exetel.com.au. [58.96.106.158]) by smtp.gmail.com with ESMTPSA id ke7-20020a170903340700b0019906c4c9dcsm490285plb.98.2023.03.13.19.04.40 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 13 Mar 2023 19:04:40 -0700 (PDT) Received: by squeak.grove.modra.org (Postfix, from userid 1000) id 19A6A1142C71; Tue, 14 Mar 2023 12:34:38 +1030 (ACDT) Date: Tue, 14 Mar 2023 12:34:38 +1030 To: binutils@sourceware.org Subject: gas .include and .incbin Message-ID: MIME-Version: 1.0 Content-Disposition: inline X-Spam-Status: No, score=-3035.3 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org X-BeenThere: binutils@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Binutils mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Alan Modra via Binutils From: Alan Modra Reply-To: Alan Modra Errors-To: binutils-bounces+ouuuleilei=gmail.com@sourceware.org Sender: "Binutils" X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1760306928518886407?= X-GMAIL-MSGID: =?utf-8?q?1760306928518886407?= This fixes a bug in .include and .incbin where given an absolute path the -I dirs would be searched for the path. * read.c (include_dir_count, include_dir_maxlen): Make them size_t. (search_and_open): New function. (s_incbin, s_include): Use search_and_open. (init_include_dir): New function. (add_include_dir): Don't set initial "." dir here. * read.h (include_dir_count, include_dir_maxlen): Update. (init_include_dir, search_and_open): Declare. * as.c (gas_early_init): Call init_include_dir. * config/tc-rx.c (rx_include): Avoid warning by using size_t. * config/tc-tic54x.c (tic54x_set_default_include): Simplify and use notes for include path. (tic54x_mlib): Use search_and_open. diff --git a/gas/as.c b/gas/as.c index a18438c2542..593eaec7f14 100644 --- a/gas/as.c +++ b/gas/as.c @@ -1308,6 +1308,8 @@ gas_early_init (int *argcp, char ***argvp) expandargv (argcp, argvp); + init_include_dir (); + #ifdef HOST_SPECIAL_INIT HOST_SPECIAL_INIT (*argcp, *argvp); #endif diff --git a/gas/config/tc-rx.c b/gas/config/tc-rx.c index 36b7f0bf9c6..c011ec11b4b 100644 --- a/gas/config/tc-rx.c +++ b/gas/config/tc-rx.c @@ -380,9 +380,7 @@ rx_include (int ignore) if (try == NULL) { - int i; - - for (i = 0; i < include_dir_count; i++) + for (size_t i = 0; i < include_dir_count; i++) { sprintf (path, "%s/%s", include_dirs[i], f); if ((try = fopen (path, FOPEN_RT)) != NULL) diff --git a/gas/config/tc-tic54x.c b/gas/config/tc-tic54x.c index f687dfe5abb..4dc1dbf28d7 100644 --- a/gas/config/tc-tic54x.c +++ b/gas/config/tc-tic54x.c @@ -1909,35 +1909,22 @@ tic54x_clink (int ignored ATTRIBUTE_UNUSED) } /* Change the default include directory to be the current source file's - directory, instead of the current working directory. If DOT is non-zero, - set to "." instead. */ + directory. */ static void tic54x_set_default_include (void) { - char *dir, *tmp = NULL; - const char *curfile; unsigned lineno; - - curfile = as_where (&lineno); - dir = xstrdup (curfile); - tmp = strrchr (dir, '/'); + const char *curfile = as_where (&lineno); + const char *tmp = strrchr (curfile, '/'); if (tmp != NULL) { - int len; - - *tmp = '\0'; - len = strlen (dir); - if (include_dir_count == 0) - { - include_dirs = XNEWVEC (const char *, 1); - include_dir_count = 1; - } - include_dirs[0] = dir; + size_t len = tmp - curfile; if (len > include_dir_maxlen) include_dir_maxlen = len; + include_dirs[0] = notes_memdup (curfile, len, len + 1); } - else if (include_dirs != NULL) + else include_dirs[0] = "."; } @@ -2325,7 +2312,7 @@ tic54x_mlib (int ignore ATTRIBUTE_UNUSED) { char *filename; char *path; - int len, i; + int len; bfd *abfd, *mbfd; ILLEGAL_WITHIN_STRUCT (); @@ -2353,31 +2340,11 @@ tic54x_mlib (int ignore ATTRIBUTE_UNUSED) demand_empty_rest_of_line (); tic54x_set_default_include (); - path = XNEWVEC (char, (unsigned long) len + include_dir_maxlen + 5); - - for (i = 0; i < include_dir_count; i++) - { - FILE *try; - - strcpy (path, include_dirs[i]); - strcat (path, "/"); - strcat (path, filename); - if ((try = fopen (path, "r")) != NULL) - { - fclose (try); - break; - } - } - - if (i >= include_dir_count) - { - free (path); - path = filename; - } + path = notes_alloc (len + include_dir_maxlen + 2); + FILE *try = search_and_open (filename, path); + if (try) + fclose (try); - /* FIXME: if path is found, malloc'd storage is not freed. Of course, this - happens all over the place, and since the assembler doesn't usually keep - running for a very long time, it really doesn't matter. */ register_dependency (path); /* Expand all archive entries to temporary files and include them. */ diff --git a/gas/read.c b/gas/read.c index cff44623541..08312ff6d2f 100644 --- a/gas/read.c +++ b/gas/read.c @@ -40,6 +40,7 @@ #include "dw2gencfi.h" #include "codeview.h" #include "wchar.h" +#include "filenames.h" #include @@ -173,10 +174,10 @@ int target_big_endian = TARGET_BYTES_BIG_ENDIAN; const char **include_dirs; /* How many are in the table. */ -int include_dir_count; +size_t include_dir_count; /* Length of longest in table. */ -int include_dir_maxlen = 1; +size_t include_dir_maxlen; #ifndef WORKING_DOT_WORD struct broken_word *broken_words; @@ -5746,6 +5747,30 @@ equals (char *sym_name, int reassign) } } +/* Open FILENAME, first trying the unadorned file name, then if that + fails and the file name is not an absolute path, attempt to open + the file in current -I include paths. PATH is a preallocated + buffer which will be set to the file opened, or FILENAME if no file + is found. */ + +FILE * +search_and_open (const char *filename, char *path) +{ + FILE *f = fopen (filename, FOPEN_RB); + if (f == NULL && !IS_ABSOLUTE_PATH (filename)) + { + for (size_t i = 0; i < include_dir_count; i++) + { + sprintf (path, "%s/%s", include_dirs[i], filename); + f = fopen (path, FOPEN_RB); + if (f != NULL) + return f; + } + } + strcpy (path, filename); + return f; +} + /* .incbin -- include a file verbatim at the current location. */ void @@ -5797,30 +5822,12 @@ s_incbin (int x ATTRIBUTE_UNUSED) demand_empty_rest_of_line (); - /* Try opening absolute path first, then try include dirs. */ - binfile = fopen (filename, FOPEN_RB); - if (binfile == NULL) - { - int i; - - path = XNEWVEC (char, (unsigned long) len + include_dir_maxlen + 5); - - for (i = 0; i < include_dir_count; i++) - { - sprintf (path, "%s/%s", include_dirs[i], filename); - - binfile = fopen (path, FOPEN_RB); - if (binfile != NULL) - break; - } + path = XNEWVEC (char, len + include_dir_maxlen + 2); + binfile = search_and_open (filename, path); - if (binfile == NULL) - as_bad (_("file not found: %s"), filename); - } + if (binfile == NULL) + as_bad (_("file not found: %s"), filename); else - path = xstrdup (filename); - - if (binfile) { long file_len; struct stat filestat; @@ -5914,48 +5921,33 @@ s_include (int arg ATTRIBUTE_UNUSED) } demand_empty_rest_of_line (); - path = notes_alloc ((size_t) i + include_dir_maxlen + 5); - for (i = 0; i < include_dir_count; i++) - { - strcpy (path, include_dirs[i]); - strcat (path, "/"); - strcat (path, filename); - if (0 != (try_file = fopen (path, FOPEN_RT))) - { - fclose (try_file); - goto gotit; - } - } + path = notes_alloc (i + include_dir_maxlen + 2); + try_file = search_and_open (filename, path); + if (try_file) + fclose (try_file); - notes_free (path); - path = filename; - gotit: register_dependency (path); input_scrub_insert_file (path); } void -add_include_dir (char *path) +init_include_dir (void) { - int i; - - if (include_dir_count == 0) - { - include_dirs = XNEWVEC (const char *, 2); - include_dirs[0] = "."; /* Current dir. */ - include_dir_count = 2; - } - else - { - include_dir_count++; - include_dirs = XRESIZEVEC (const char *, include_dirs, - include_dir_count); - } + include_dirs = XNEWVEC (const char *, 1); + include_dirs[0] = "."; /* Current dir. */ + include_dir_count = 1; + include_dir_maxlen = 1; +} +void +add_include_dir (char *path) +{ + include_dir_count++; + include_dirs = XRESIZEVEC (const char *, include_dirs, include_dir_count); include_dirs[include_dir_count - 1] = path; /* New one. */ - i = strlen (path); + size_t i = strlen (path); if (i > include_dir_maxlen) include_dir_maxlen = i; } diff --git a/gas/read.h b/gas/read.h index 406b026d76f..42efce9e79d 100644 --- a/gas/read.h +++ b/gas/read.h @@ -79,8 +79,8 @@ extern const char line_separator_chars[]; /* Table of -I directories. */ extern const char **include_dirs; -extern int include_dir_count; -extern int include_dir_maxlen; +extern size_t include_dir_count; +extern size_t include_dir_maxlen; /* The offset in the absolute section. */ extern addressT abs_section_offset; @@ -124,7 +124,9 @@ extern unsigned int next_char_of_string (void); extern void s_mri_sect (char *); extern char *mri_comment_field (char *); extern void mri_comment_end (char *, int); -extern void add_include_dir (char *path); +extern void init_include_dir (void); +extern void add_include_dir (char *); +extern FILE *search_and_open (const char *, char *); extern void cons (int nbytes); extern void demand_empty_rest_of_line (void); extern void emit_expr (expressionS *exp, unsigned int nbytes);