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

    <tr>
        <th>Summary</th>
        <td>
            [m68k] Add support for thread local storage
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:m68k
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          0x59616e
      </td>
    </tr>
</table>

<pre>
    This is a place for discussion regarding the support of TLS on m68k.

This issue blocks #59161.

cc @glaubitz 

## Reference for TLS.

- [this draft](https://lists.debian.org/debian-68k/2007/11/msg00071.html) gives a detailed description of the TLS ABI of m68k.
- [this paper](https://akkadia.org/drepper/tls.pdf) introduces the ELF handling for TLS.

In short, there are four access model for TLS

1. general dynamic
2. local dynamic
3. inital exec
4. local exec

In short, the first one is the most generic one and is able to handle all possible cases. 2, 3 and 4 are optimization under certain circumstances.

## Current status of gcc

It is weird that gcc seems to only support the two more-restricted access model: inital exec and local exec.

The other two, general dynamic and local dynamic, are unsupported, which may causes problem with dynamically loaded object.

You can attest this claim with the following code:

```
$ echo '
extern __thread int myvar;


int *get_addr(void) {
  return &myvar;
}
' > test.c
$ m68k-linux-gnu-gcc -ftls-model=global-dynamic -S -o - test.c
#NO_APP
 .file   "test.c"
        .text
        .globl __m68k_read_tp
        .align  2
        .globl  get_addr
 .type   get_addr, @function
get_addr:
        link.w %fp,#0
 move.l %a5,-(%sp)
        lea (%pc, _GLOBAL_OFFSET_TABLE_@GOTPC), %a5
 jsr __m68k_read_tp
        move.l %a0,%d0
        add.l myvar@TLSIE(%a5),%d0
        move.l %d0,%d1
        move.l %d1,%a0
 move.l -4(%fp),%a5
        unlk %fp
        rts
        .size get_addr, .-get_addr
        .ident  "GCC: (Ubuntu 11.3.0-1ubuntu1~22.04) 11.3.0"
        .section .note.GNU-stack,"",@progbits
```

It still uses the initial-exec model instead of general-dynamic model specified in the command line.

I have no idea why gcc fails to support the general-dynamic model. I guess the runtime may have not ready yet.

## What should we do now ?

We need to know the status of the runtime (glibc and linux) so that we can decided to what extent we need to support.





</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx8Vl1v8yoS_jXkZmTLxvm8yEWaNlWl6vTVtkdHexVhGNucYLAAN817sb99BXbSJKe7keXIw8zwzAcPw5yTtUZck9kDmT1OWO8bY9fZ12w1z-c4KY04rT8a6UA6YNApxhEqY0FIx3vnpNFgsWZWSF2DbxBc33XGejAVfLy-g9HQzpeHlGSPJNsM79Gf6xFKZfjBAaHFbJXP8xs1zoFMs1qxvpT-N1wvEVoQWsC_sEKLeoT08fp-Y58AmT34sJewrPJk9kjosvG-c6TYELojdKek8y4VWEqmU2NrQnfDRzJfHgjd0SxbELrLc0J3rauzLFvkaeNbRegKavmJISkCPZMKBQh03MrOh6SYKmYjpGDz8BI-r9LwjaxjHdqfkLHDgQnJzqAsdkGR7rxyaSeqsL_U3hrRc3Rxq6fXHTRMCxUq8VM-XjS4xlhP6DYYWARmQ-Z6C4xzdA5aI1Cdba9N8xRq1GiZAnHSrJV8kNMUlOH30iIFqaVnCvALR9n0rPkt-hEVVNI6D0Zj6LggaI3zw-6SRznTInZjqRC8GWJGYEpBZ5yTQcyZQ5cCDU6LaDCNwZrOy1b-ZrFEvRZogaP1TGrg0vK-dZ5pji79odm2vbWoPTjPfO9CRWt-G4kPsI4orQDfMB_WwSG2LsA0Wp0uhyPE5Y8GWmMxsei8ldyjuKkDKTbXaYxRfKfw7kAhmFDS4DTEfFesK9tzoeg2JqTXIyQUQXRsJG-gZSfgrHfooLOmVNjCUfrmbMuUOoEyTKAAU_6N3N-A-bfpgTMNzHt0IVTpgCsmRyexxkYpcwx9yo3A0PPX6Z5n4zNmfwrIGwOELgYJfnm0GvZ731hkIpwDaE-fzJLi4cZRfIdVQjc1-j0TwhK6_DRShPNDFqM6gEXfWw2Ezu8cLR7PKBZAiicIIaX8G1k41ImSuv9Kat0noe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