<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" "http://www.w3.org/TR/REC-html40/loose.dtd">
<html lang="en" style="--code-editor-font: var(--default-mono-font, "GitLab Mono"), JetBrains Mono, Menlo, DejaVu Sans Mono, Liberation Mono, Consolas, Ubuntu Mono, Courier New, andale mono, lucida console, monospace;">
<head>
<meta content="text/html; charset=UTF-8" http-equiv="Content-Type">
<title>
GitLab
</title>

<style data-premailer="ignore" type="text/css">
a { color: #1068bf; }
</style>

<style>img {
max-width: 100%; height: auto;
}
body {
font-size: .875rem;
}
body {
-webkit-text-shadow: rgba(255,255,255,.01) 0 0 1px;
}
body {
font-family: "GitLab Sans",-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Noto Sans",Ubuntu,Cantarell,"Helvetica Neue",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji"; font-size: inherit;
}
</style>
</head>
<body style="font-size: inherit; -webkit-text-shadow: rgba(255,255,255,.01) 0 0 1px; font-family: "GitLab Sans",-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,"Noto Sans",Ubuntu,Cantarell,"Helvetica Neue",sans-serif,"Apple Color Emoji","Segoe UI Emoji","Segoe UI Symbol","Noto Color Emoji";">
<div class="content">

<p style="color: #777777;">
<a href="https://gitlab.rtems.org/hongran86">Ran Hong</a>
<a href="https://gitlab.rtems.org/rtems/pkg/rtems-libbsd/-/issues/75#note_144011">commented</a>:
</p>
<div class="md" style="position: relative; z-index: 1; color: #3a383f; word-wrap: break-word;">
<p dir="auto" style="color: #3a383f; margin: 0 0 1rem;" align="initial">I fixed this issue by adding -G0 to the ABI_FLAGS in rtems.
This is how ABI_FLAGS looks in my config.ini in the rtems source directory (not libbsd).</p>
<p dir="auto" style="color: #3a383f; margin: 0 0 1rem;" align="initial">I learned this from Gemini AI. Let me paste what AI said regarding this issue. See below:</p>
<p dir="auto" style="color: #3a383f; margin: 0 0 1rem;" align="initial">This error is a specific consequence of how the PowerPC architecture handles "Small Data Areas" (SDA) and how modern compilers optimize for memory layout using per-variable sections.</p>
<p dir="auto" style="color: #3a383f; margin: 0 0 1rem;" align="initial">To understand why this is happening, we have to look at how the PowerPC ABI (Application Binary Interface) tries to save CPU cycles and how the linker organizes your binary.</p>
<ol dir="auto" style="text-align: initial; margin: 0 0 1rem; padding: 0;">
<li style="margin-top: 0; line-height: 1.6em; margin-left: 25px; padding-left: 3px;">The PowerPC SDA (Small Data Area) Optimization</li>
</ol>
<p dir="auto" style="color: #3a383f; margin: 0 0 1rem;" align="initial">On PowerPC, accessing a standard global variable usually requires two instructions because addresses are 32-bit, but a single instruction can only carry a 16-bit immediate value.</p>
<p dir="auto" style="color: #3a383f; margin: 0 0 1rem;" align="initial">To optimize this, the PowerPC ABI uses a Small Data Area. The linker groups "small" global variables (typically those 8 bytes or smaller) into special sections called .sdata or .sbss.</p>
<div class="gl-relative markdown-code-block js-markdown-code">
<pre class="code highlight js-syntax-highlight language-plaintext" v-pre="true" style="display: block; font-size: 14px; color: #3a383f; line-height: 1.6em; overflow-x: auto; border-radius: .25rem; position: relative; font-family: "GitLab Mono", "JetBrains Mono", "Menlo", "DejaVu Sans Mono", "Liberation Mono", "Consolas", "Ubuntu Mono", "Courier New", "andale mono", "lucida console", monospace; font-variant-ligatures: none; word-break: break-all; word-wrap: break-word; background-color: #fbfafd; margin: 0 0 1rem; padding: 12px; border: 1px solid #dcdcde;"><code style="font-size: inherit; color: inherit; word-wrap: normal; word-break: keep-all; background-color: inherit; border-radius: .25rem; white-space: pre; margin-top: 0; font-family: "GitLab Mono", "JetBrains Mono", "Menlo", "DejaVu Sans Mono", "Liberation Mono", "Consolas", "Ubuntu Mono", "Courier New", "andale mono", "lucida console", monospace; font-variant-ligatures: none; overflow-wrap: normal; padding: unset;"><span id="LC1" class="line" lang="plaintext" style="margin-top: 0;">A register (usually r13 or r2) is initialized to point to the base of this area.</span>
<span id="LC2" class="line" lang="plaintext"></span>
<span id="LC3" class="line" lang="plaintext">The compiler then uses a R_PPC_SDAREL16 relocation, which allows the CPU to access the variable with a single instruction using a 16-bit offset from that register.</span></code></pre>
<copy-code></copy-code><insert-code-snippet></insert-code-snippet>
</div>
<ol start="2" dir="auto" style="text-align: initial; margin: 0 0 1rem; padding: 0;">
<li style="margin-top: 0; line-height: 1.6em; margin-left: 25px; padding-left: 3px;">The Conflict: _bsd_vfs_smr in the Wrong Section</li>
</ol>
<p dir="auto" style="color: #3a383f; margin: 0 0 1rem;" align="initial">The error message tells you exactly what went wrong:</p>
<div class="gl-relative markdown-code-block js-markdown-code">
<pre class="code highlight js-syntax-highlight language-plaintext" v-pre="true" style="display: block; font-size: 14px; color: #3a383f; line-height: 1.6em; overflow-x: auto; border-radius: .25rem; position: relative; font-family: "GitLab Mono", "JetBrains Mono", "Menlo", "DejaVu Sans Mono", "Liberation Mono", "Consolas", "Ubuntu Mono", "Courier New", "andale mono", "lucida console", monospace; font-variant-ligatures: none; word-break: break-all; word-wrap: break-word; background-color: #fbfafd; margin: 0 0 1rem; padding: 12px; border: 1px solid #dcdcde;"><code style="font-size: inherit; color: inherit; word-wrap: normal; word-break: keep-all; background-color: inherit; border-radius: .25rem; white-space: pre; margin-top: 0; font-family: "GitLab Mono", "JetBrains Mono", "Menlo", "DejaVu Sans Mono", "Liberation Mono", "Consolas", "Ubuntu Mono", "Courier New", "andale mono", "lucida console", monospace; font-variant-ligatures: none; overflow-wrap: normal; padding: unset;"><span id="LC1" class="line" lang="plaintext" style="margin-top: 0;">the target (_bsd_vfs_smr) ... is in the wrong output section (.data)</span></code></pre>
<copy-code></copy-code><insert-code-snippet></insert-code-snippet>
</div>
<p dir="auto" style="color: #3a383f; margin: 0 0 1rem;" align="initial">The compiler generated code for kern_descrip.c assuming that the variable _bsd_vfs_smr would be placed in the .sdata section (the Small Data Area). However, during the final link, that variable ended up in the standard .data section.</p>
<p dir="auto" style="color: #3a383f; margin: 0 0 1rem;" align="initial">Because .data is outside the 16-bit reach of the SDA base register, the R_PPC_SDAREL16 relocation "breaks." The linker realizes it cannot calculate a 16-bit offset to a variable that is sitting far away in the general data section.
3. Why "Per-Variable Sections" Cause This</p>
<p dir="auto" style="color: #3a383f; margin: 0 0 1rem;" align="initial">The expert mentioned: "We use per variable/function data/text sections." When you compile with flags like -fdata-sections and -ffunction-sections (standard in RTEMS to allow the linker to garbage collect unused code), every single variable gets its own unique section (e.g., .data._bsd_vfs_smr).</p>
<p dir="auto" style="color: #3a383f; margin: 0 0 1rem;" align="initial">If the linker script or the compiler’s "small data threshold" logic isn't perfectly synchronized:</p>
<div class="gl-relative markdown-code-block js-markdown-code">
<pre class="code highlight js-syntax-highlight language-plaintext" v-pre="true" style="display: block; font-size: 14px; color: #3a383f; line-height: 1.6em; overflow-x: auto; border-radius: .25rem; position: relative; font-family: "GitLab Mono", "JetBrains Mono", "Menlo", "DejaVu Sans Mono", "Liberation Mono", "Consolas", "Ubuntu Mono", "Courier New", "andale mono", "lucida console", monospace; font-variant-ligatures: none; word-break: break-all; word-wrap: break-word; background-color: #fbfafd; margin: 0 0 1rem; padding: 12px; border: 1px solid #dcdcde;"><code style="font-size: inherit; color: inherit; word-wrap: normal; word-break: keep-all; background-color: inherit; border-radius: .25rem; white-space: pre; margin-top: 0; font-family: "GitLab Mono", "JetBrains Mono", "Menlo", "DejaVu Sans Mono", "Liberation Mono", "Consolas", "Ubuntu Mono", "Courier New", "andale mono", "lucida console", monospace; font-variant-ligatures: none; overflow-wrap: normal; padding: unset;"><span id="LC1" class="line" lang="plaintext" style="margin-top: 0;">The Compiler's Guess: While compiling kern_descrip.c, the compiler sees _bsd_vfs_smr and thinks, "This is a small variable, I'll use an SDA relocation for it."</span>
<span id="LC2" class="line" lang="plaintext"></span>
<span id="LC3" class="line" lang="plaintext">The Linker's Reality: At link time, because the variable is in its own section (.data._bsd_vfs_smr), the linker might not match it into the .sdata output group. It falls back into the general .data group.</span></code></pre>
<copy-code></copy-code><insert-code-snippet></insert-code-snippet>
</div>
<ol start="4" dir="auto" style="text-align: initial; margin: 0 0 1rem; padding: 0;">
<li style="margin-top: 0; line-height: 1.6em; margin-left: 25px; padding-left: 3px;">Why This Happens specifically in RTEMS/libbsd</li>
</ol>
<p dir="auto" style="color: #3a383f; margin: 0 0 1rem;" align="initial">This often occurs when:</p>
<div class="gl-relative markdown-code-block js-markdown-code">
<pre class="code highlight js-syntax-highlight language-plaintext" v-pre="true" style="display: block; font-size: 14px; color: #3a383f; line-height: 1.6em; overflow-x: auto; border-radius: .25rem; position: relative; font-family: "GitLab Mono", "JetBrains Mono", "Menlo", "DejaVu Sans Mono", "Liberation Mono", "Consolas", "Ubuntu Mono", "Courier New", "andale mono", "lucida console", monospace; font-variant-ligatures: none; word-break: break-all; word-wrap: break-word; background-color: #fbfafd; margin: 0 0 1rem; padding: 12px; border: 1px solid #dcdcde;"><code style="font-size: inherit; color: inherit; word-wrap: normal; word-break: keep-all; background-color: inherit; border-radius: .25rem; white-space: pre; margin-top: 0; font-family: "GitLab Mono", "JetBrains Mono", "Menlo", "DejaVu Sans Mono", "Liberation Mono", "Consolas", "Ubuntu Mono", "Courier New", "andale mono", "lucida console", monospace; font-variant-ligatures: none; overflow-wrap: normal; padding: unset;"><span id="LC1" class="line" lang="plaintext" style="margin-top: 0;">Threshold Mismatch: One part of the library was compiled with a different -G (Small Data Threshold) value than another. If libbsd was compiled with -G 8, it assumes anything 8 bytes or smaller is in SDA. If the final application or a different object file uses -G 0, they won't agree on where that variable lives.</span>
<span id="LC2" class="line" lang="plaintext"></span>
<span id="LC3" class="line" lang="plaintext">External Declarations: _bsd_vfs_smr might be declared in a way that the compiler thinks it's small, but the actual definition of the variable is forced into a different section by a header or a macro in the FreeBSD kernel code ported to RTEMS.</span></code></pre>
<copy-code></copy-code><insert-code-snippet></insert-code-snippet>
</div>
<p dir="auto" style="color: #3a383f; margin: 0 0 1rem;" align="initial">How to Fix It</p>
<p dir="auto" style="color: #3a383f; margin: 0 0 1rem;" align="initial">There are generally three ways to resolve this in an RTEMS environment:</p>
<div class="gl-relative markdown-code-block js-markdown-code">
<pre class="code highlight js-syntax-highlight language-plaintext" v-pre="true" style="display: block; font-size: 14px; color: #3a383f; line-height: 1.6em; overflow-x: auto; border-radius: .25rem; position: relative; font-family: "GitLab Mono", "JetBrains Mono", "Menlo", "DejaVu Sans Mono", "Liberation Mono", "Consolas", "Ubuntu Mono", "Courier New", "andale mono", "lucida console", monospace; font-variant-ligatures: none; word-break: break-all; word-wrap: break-word; background-color: #fbfafd; margin: 0 0 1rem; padding: 12px; border: 1px solid #dcdcde;"><code style="font-size: inherit; color: inherit; word-wrap: normal; word-break: keep-all; background-color: inherit; border-radius: .25rem; white-space: pre; margin-top: 0; font-family: "GitLab Mono", "JetBrains Mono", "Menlo", "DejaVu Sans Mono", "Liberation Mono", "Consolas", "Ubuntu Mono", "Courier New", "andale mono", "lucida console", monospace; font-variant-ligatures: none; overflow-wrap: normal; padding: unset;"><span id="LC1" class="line" lang="plaintext" style="margin-top: 0;">Uniform Small Data Threshold: Ensure that your application and all linked libraries (like libbsd) are compiled with the same -G value. Setting -G 0 globally is a common (though slightly less performant) way to "turn off" SDA and avoid these relocation errors entirely.</span>
<span id="LC2" class="line" lang="plaintext"></span>
<span id="LC3" class="line" lang="plaintext">Adjust Linker Scripts: Ensure the linker script explicitly handles the per-variable sections by pulling them into the correct SDA output section:</span>
<span id="LC4" class="line" lang="plaintext">Code snippet</span></code></pre>
<copy-code></copy-code><insert-code-snippet></insert-code-snippet>
</div>
<p dir="auto" style="color: #3a383f; margin: 0 0 1rem;" align="initial">.sdata : {
<em style="margin-top: 0;">(.sdata .sdata.</em> .gnu.linkonce.s.*)
}</p>
<p dir="auto" style="color: #3a383f; margin: 0;" align="initial">Compiler Attributes: If it's just one or two variables, you can use <strong style="font-weight: 600; margin-top: 0;">attribute</strong>((section(".data"))) to force the compiler to stop assuming it's in the Small Data Area, though this is difficult in a large ported library like libbsd.</p>
</div>


</div>
<div class="footer" style="margin-top: 10px;">
<p style="font-size: small; color: #626168;">

<br>
<a href="https://gitlab.rtems.org/rtems/pkg/rtems-libbsd/-/issues/75#note_144011">View it on GitLab</a>.
<br>
You're receiving this email because of your account on <a target="_blank" rel="noopener noreferrer" href="https://gitlab.rtems.org">gitlab.rtems.org</a>. <a href="https://gitlab.rtems.org/-/sent_notifications/2-97ciy08pie1yge86d99sxikm4/unsubscribe" target="_blank" rel="noopener noreferrer">Unsubscribe</a> from this thread · <a href="https://gitlab.rtems.org/-/profile/notifications" target="_blank" rel="noopener noreferrer" class="mng-notif-link">Manage all notifications</a> · <a href="https://gitlab.rtems.org/help" target="_blank" rel="noopener noreferrer" class="help-link">Help</a>
<span style="color: transparent; font-size: 0; display: none; overflow: hidden; opacity: 0; width: 0; height: 0; max-width: 0; max-height: 0;">
Notification message regarding https://gitlab.rtems.org/rtems/pkg/rtems-libbsd/-/issues/75#note_144011 at 1772473935
</span>
<script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","action":{"@type":"ViewAction","name":"View Issue","url":"https://gitlab.rtems.org/rtems/pkg/rtems-libbsd/-/issues/75#note_144011"}}</script>


</p>
</div>
</body>
</html>