<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/97124>97124</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [LLD] Adding a modern target-selection flag to the drivers
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            enhancement
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Ericson2314
      </td>
    </tr>
</table>

<pre>
    LLD's frontends are currently very faithful to the linkers they are based on. But that means the target-selection mechanisms they have are rather underpowered. I think would be good to have modern flags that would allow us to set the `EM_*` choice, `ELFOSABI_*` choice, and `ELFKind` independently (for valid combinations)  

A good use for this would be better handling of the "OSABI" field. For example:

## Problems

### FreeBSD existing hacks

https://github.com/llvm/llvm-project/blob/9572388a849c494d45df334f9facd8ee6663953f/lld/ELF/Driver.cpp#L174-L177 
is an ad-hoc hack for FreeBSD. The corresponding code in Clang to use it is even uglier:

https://github.com/llvm/llvm-project/blob/9572388a849c494d45df334f9facd8ee6663953f/clang/lib/Driver/ToolChains/FreeBSD.cpp#L174-L218

If Clang could use "regular" code to transform the CPU into a `-m` flag, and then separately tell LLD that the `ELFOSABI_*` is `ELFOSABI_FREEBSD`, that would be much cleaner.


### OpenBSD has similar needs

As discussed in https://github.com/llvm/llvm-project/pull/92675, we ought to have CI for OpenBSD, but OpenBSD has some outstanding downstream changes that need to be upstreamed before upstream-tool-produced binaries will work, and many of those changes today assume OpenBSD->OpenBSD native compilation and so are unfit to upstream as is 

https://github.com/llvm/llvm-project/pull/97122 is the first such patch I've rebased. This is a somewhat borderline case, as there is already blanket handling to `.openbsd.random` regardless of the `ELFOSABI_*` in use. Still, "stealing names" from all ELF usages of LLD doesn't seem very elegant, even if the `.openbsd.random` case is grandfathered in --- I much rather start requiring `ELFOSABI_OPENBSD` with the `.openbsd.random` case deprecated with a warning. That said, if doing this would require a `_obsd` hack like FreeBSD's `_fbsd`, I can't help but think the medicine is as almost as bad is the disease.

## Solutions

### Proposal A: `--target` flag

Add a flag for "regular" LLVM triples --- like Clang's `--target` or LLVM's `-mtriple` --- so we have more expressive power. Those triples should be mostly possible to map to the choices above, and `-m` flags could still be used to fill in the gaps.

Advantages:

- "no new syntax"
- tools like Clang can just forward their `--target` argument as-is and hope for the best

Disadvantages:

LLVM triples can both say to much and too little

### Proposal B: New greenfield flags

Add multiple new flags for specifying the these parts independently.  Certainly `ELFOSABI_` needs one. `-m` does the `EM_` and `ELFKind` residual alright, perhaps, or perhaps they get fresh new flags too.

Advantages: No syntax vs semantics mismatch / friction / corner cases.

Disadvantage: Greenfield new flags for other tooling to have to learn about.

----

CC @brad0 because OpenBSD

C @mstorsjo because I am curious if Windows stuff has similar needs / not sure what to do about `lld-link` (`clang-cl` takes `--target` and many other GNU-style flags, but `lld-link` only takes `/flag` MS-style-flags)
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8V0tv4zgS_jXypSDDpp85-JCXB8F6uhubmd3joCSWJHYoUsui7Pa_XxRlx3bSg8FggT10xxLJYj2--uoTMpvaEW2yxUO2eBphHxsfNs_BlOydmk3no8Lr42a3e8rUiqEK3kVymgEDQdmHQC7aI-wpHKFCE5uqtxA9xIbAGvdGgeX3Me0vkEmDd2N46CPEBiO0hC7tgIihppgzWSqj8Q5aKht0htuThQb3lMwEjA0F6J2m0PkDBdJjeIHYGPcGB99bDQVB7b0WT9Kx1msKDiqLNQ8XD_vQWn-AnmUjU0yOZMvJ869_ZOo-W06gbLwpKVOP6fVu-_X1_uHl8yI6fdrwD-O0rBmnqSOnh_xkal35AHu0RkPp28I4lCA5U3cA2eQpm9wP_98PjvdMICdiY_gSU0ExUoAGnbbG1eCrwWOlkl-ZUlAZsnoMWx-AfmDbWcpm99cXZGqWqRl8C76w1PLnJVndBqKH1yegH4aj3NRg-Xazt4mxYzGttpna1iY2fTEufZuprbX785-8C_47lTFT28L6IlPbu8VKzdZrXM_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