This is an archive of the discontinued LLVM Phabricator instance.

[Driver][AVR] Don't emit default '-Tdata' when '-T' is specified
AbandonedPublic

Authored by benshi001 on Jan 25 2023, 6:16 PM.

Diff Detail

Event Timeline

benshi001 created this revision.Jan 25 2023, 6:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 25 2023, 6:16 PM
benshi001 requested review of this revision.Jan 25 2023, 6:16 PM
Herald added a project: Restricted Project. · View Herald TranscriptJan 25 2023, 6:16 PM
This revision now requires changes to proceed.Jan 28 2023, 5:34 PM

I agree with your comment in https://github.com/llvm/llvm-project/issues/60203#issuecomment-1407532083, But I still think my patch is worth to be applied.

Since you mentioned It's not a problem to have multiple -Tdata=. The last wins. But what will happen if both a linker script and a -Wl,-Tdata are specified? ASFAIK, the command line option will win.

But the default "-Tdata" is added according to each specific AVR device SRAM layout (https://github.com/llvm/llvm-project/blob/main/clang/lib/Driver/ToolChains/AVR.cpp#L35).

My opinion is:

  1. We should add a default SRAM address to the linker, if user does not specify any link options. This is current behaviour of clang's main branch.
  2. If the user explictly specifies a linker scrpit, we should follow the user's willing, and do not decide the SRAM address for him.
benshi001 updated this revision to Diff 493053.Jan 28 2023, 7:10 PM
benshi001 edited the summary of this revision. (Show Details)
benshi001 added inline comments.
clang/lib/Driver/ToolChains/AVR.cpp
499–500

I have added comment about why we need this change, it has nothing to do with https://github.com/llvm/llvm-project/issues/60203.

benshi001 edited the summary of this revision. (Show Details)Jan 28 2023, 7:38 PM

OK. If this doesn't add -Tdata= driver options, I'm fine with it.
But why is default -Tdata added in the first place?

Most linker scripts are added as -Wl,-T,a.lds (-Wl, values are opaque to the driver), so the driver cannot really know whether a linker script is used.

benshi001 added a comment.EditedJan 29 2023, 5:42 PM

OK. If this doesn't add -Tdata= driver options, I'm fine with it.
But why is default -Tdata added in the first place?

Most linker scripts are added as -Wl,-T,a.lds (-Wl, values are opaque to the driver), so the driver cannot really know whether a linker script is used.

So how about change to

  1. move the default -Tdata to a later position.
  2. clang driver just checks -T but omits -Wl, this can not 100% fix the issue, but at least improve it. Current most users are from avr-gcc, who have get used to -T.

A good solution is using default linker script provided by the avr-libc, which will be overwritten by user's explicit specification.

I will do that solution in a different patch, it needs more extra work. Currently we just guarantee avr-gcc users's -T option will not be broken.

benshi001 updated this revision to Diff 493179.Jan 29 2023, 7:08 PM
benshi001 retitled this revision from [Driver][AVR] Don't emit default '-Tdata' when a linker script is specified to [Driver][AVR] Don't emit default '-Tdata' when '-T' is specified.
benshi001 edited the summary of this revision. (Show Details)
benshi001 removed a reviewer: Miss_Grape.
benshi001 added inline comments.Jan 29 2023, 7:10 PM
clang/lib/Driver/ToolChains/AVR.cpp
544

Just moving the default -T from previous position to here, without any change.

MaskRay requested changes to this revision.Jan 30 2023, 10:04 AM

OK. If this doesn't add -Tdata= driver options, I'm fine with it.
But why is default -Tdata added in the first place?

Most linker scripts are added as -Wl,-T,a.lds (-Wl, values are opaque to the driver), so the driver cannot really know whether a linker script is used.

So how about change to

  1. move the default -Tdata to a later position.
  2. clang driver just checks -T but omits -Wl, this can not 100% fix the issue, but at least improve it. Current most users are from avr-gcc, who have get used to -T.

A good solution is using default linker script provided by the avr-libc, which will be overwritten by user's explicit specification.

I will do that solution in a different patch, it needs more extra work. Currently we just guarantee avr-gcc users's -T option will not be broken.

I think checking whether there is a linker script in any way is not a right solution.
GNU ld supports -dT which lld doesn't support. A linker script can be used without a -T/-dT option as well (it gets appended).
Any chance to remove -Tdata= and require the user to specify a -Tdata=?

This revision now requires changes to proceed.Jan 30 2023, 10:04 AM
benshi001 added a comment.EditedJan 30 2023, 6:43 PM

OK. If this doesn't add -Tdata= driver options, I'm fine with it.
But why is default -Tdata added in the first place?

Most linker scripts are added as -Wl,-T,a.lds (-Wl, values are opaque to the driver), so the driver cannot really know whether a linker script is used.

So how about change to

  1. move the default -Tdata to a later position.
  2. clang driver just checks -T but omits -Wl, this can not 100% fix the issue, but at least improve it. Current most users are from avr-gcc, who have get used to -T.

A good solution is using default linker script provided by the avr-libc, which will be overwritten by user's explicit specification.

I will do that solution in a different patch, it needs more extra work. Currently we just guarantee avr-gcc users's -T option will not be broken.

I think checking whether there is a linker script in any way is not a right solution.
GNU ld supports -dT which lld doesn't support. A linker script can be used without a -T/-dT option as well (it gets appended).
Any chance to remove -Tdata= and require the user to specify a -Tdata=?

@MaskRay

Newer avr-gcc's solution is adding a default linker script (provided by binutils, locates at $binutils-gdb/ld/scripttempl/avr.sc). And user can specifies his own one, which will win against the default one.

I will remove the default -Tdata, and append a default -Tavr.sc along with several --defsym (required by avr.sc).

How do you think about this solution ?

How does avr-gcc know whether the user provides a linker script?

I really don't like adding a default -Tdata= when it can interfere with common uses of specifying a linker script. In these situations drivers not adding default options is usually better than guessing the user intention.
If the use case is somewhat strange, we can document it.

How does avr-gcc know whether the user provides a linker script?

I really don't like adding a default -Tdata= when it can interfere with common uses of specifying a linker script. In these situations drivers not adding default options is usually better than guessing the user intention.
If the use case is somewhat strange, we can document it.

My option,

  1. we remove default -Tdata
  2. we add a default -T avr.sc (provided by bintuils as default) at an early position of avr-ld's options.
  3. If user specified -T xx.lds, append user's at an late position, then user's will win, the default will lose.
benshi001 added a comment.EditedJan 30 2023, 7:22 PM

How does avr-gcc know whether the user provides a linker script?

I really don't like adding a default -Tdata= when it can interfere with common uses of specifying a linker script. In these situations drivers not adding default options is usually better than guessing the user intention.
If the use case is somewhat strange, we can document it.

I have a question. linux-x86 has its default linker script, which can also be specified by user. How does clang driver implement that? Could you please show me where clang handle this ?

I hope avr also follow this logic. User's willing go first, then default linker script.

benshi001 abandoned this revision.Feb 8 2023, 11:14 PM