Skip to content

Commit

Permalink
ld: Support archives appearing as input files
Browse files Browse the repository at this point in the history
Handle them equally to '-l' (but without resolving via '-L').
(But beware of (pre-existing) non-standard Unix 'ld' semantics;
see <#41>
"ld: non-standard handling of options which refer to files".)
  • Loading branch information
tschwinge committed Jun 23, 2023
1 parent 606848b commit 4500e5c
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 2 deletions.
25 changes: 24 additions & 1 deletion nvptx-ld.cc
Original file line number Diff line number Diff line change
Expand Up @@ -173,13 +173,22 @@ class archive
delete[] contents;
contents = NULL;
}
bool init (FILE *file)

static bool is_archive (FILE *file)
{
char magic[SARMAG];
if (fread (magic, 1, SARMAG, file) != SARMAG)
return false;
if (memcmp (magic, ARMAG, SARMAG) != 0)
return false;
return true;
}

bool init (FILE *file)
{
if (!is_archive (file))
return false;

f = file;
fseek (f, 0, SEEK_END);
flen = ftell (f);
Expand Down Expand Up @@ -492,6 +501,20 @@ This program has absolutely no warranty.\n",
cerr << "error opening " << name << "\n";
goto error_out;
}

/* Archives appearing here are not resolved via 'libpaths'. */
if (archive::is_archive (f))
{
/* (Pre-existing problem of) non-standard Unix 'ld' semantics; see
<https://github.com/MentorEmbedded/nvptx-tools/issues/41>
"ld: non-standard handling of options which refer to files". */
libraries.push_back (name);

fclose (f);
f = NULL;
continue;
}

fseek (f, 0, SEEK_END);
off_t len = ftell (f);
fseek (f, 0, SEEK_SET);
Expand Down
12 changes: 12 additions & 0 deletions test/ld/empty-2.test
Original file line number Diff line number Diff line change
Expand Up @@ -42,3 +42,15 @@ RUN: %target_ld_cmd -o %t.nvptx %t/o/GLOBAL_FUNCTION_DECL_f.o -L%t/a -lempty -lG
RUN: cmp %t.nvptx.golden %t.nvptx
RUN: ! test -s %t.stdout
RUN: ! test -s %t.stderr

RUN: rm -f %t.nvptx
RUN: %target_ld_cmd -o %t.nvptx %t/o/GLOBAL_FUNCTION_DECL_f.o -L%t/a %t/a/libempty.a -lGLOBAL_FUNCTION_f %t/a/libempty.a > %t.stdout 2> %t.stderr
RUN: cmp %t.nvptx.golden %t.nvptx
RUN: ! test -s %t.stdout
RUN: ! test -s %t.stderr

RUN: rm -f %t.nvptx
RUN: %target_ld_cmd -o %t.nvptx %t/o/GLOBAL_FUNCTION_DECL_f.o -L%t/a -lempty -lGLOBAL_FUNCTION_f %t/a/libempty.a > %t.stdout 2> %t.stderr
RUN: cmp %t.nvptx.golden %t.nvptx
RUN: ! test -s %t.stdout
RUN: ! test -s %t.stderr
6 changes: 5 additions & 1 deletion test/ld/search-1.test
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,11 @@ RUN: cmp %t.nvptx.golden %t.nvptx
RUN: ! test -s %t.stdout
RUN: ! test -s %t.stderr

TODO Not testing with linker input file '%t/a/libGLOBAL_FUNCTION_f.a', as we don't support that yet.
RUN: rm -f %t.nvptx
RUN: %target_ld_cmd -o %t.nvptx %t/o/GLOBAL_FUNCTION_DECL_f.o %t/a/libGLOBAL_FUNCTION_f.a > %t.stdout 2> %t.stderr
RUN: cmp %t.nvptx.golden %t.nvptx
RUN: ! test -s %t.stdout
RUN: ! test -s %t.stderr

RUN: rm -f %t.nvptx
RUN: %target_ld_cmd -o %t.nvptx -L%t/o ./GLOBAL_FUNCTION_DECL_f.o ./GLOBAL_FUNCTION_DEF_f.o > %t.stdout 2> %t.stderr; r=$?; [ x"$r" = x1 ]
Expand Down

0 comments on commit 4500e5c

Please sign in to comment.