<?xml version="1.0"?>
<feed xmlns="http://www.w3.org/2005/Atom" xml:lang="en">
	<id>https://www.emmtrix.com/w139/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Tobias.columbus</id>
	<title>emmtrix Wiki - User contributions [en]</title>
	<link rel="self" type="application/atom+xml" href="https://www.emmtrix.com/w139/api.php?action=feedcontributions&amp;feedformat=atom&amp;user=Tobias.columbus"/>
	<link rel="alternate" type="text/html" href="https://www.emmtrix.com/wiki/Special:Contributions/Tobias.columbus"/>
	<updated>2026-04-18T23:55:22Z</updated>
	<subtitle>User contributions</subtitle>
	<generator>MediaWiki 1.39.10</generator>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Remove_Includes_Transformation&amp;diff=2783</id>
		<title>Remove Includes Transformation</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Remove_Includes_Transformation&amp;diff=2783"/>
		<updated>2025-08-06T06:57:15Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== RemoveIncludes Transformation ==&lt;br /&gt;
The &#039;&#039;&#039;RemoveIncludes Transformation&#039;&#039;&#039; removes all non-standard include directives from a C source file and replaces them with the corresponding declarations directly within the file. The result is a self-contained &amp;lt;code&amp;gt;.c&amp;lt;/code&amp;gt; file that can be compiled independently of external headers.&lt;br /&gt;
&lt;br /&gt;
This transformation effectively performs a header-flattening step that normally occurs during preprocessing in a compiler’s build pipeline. However, instead of producing a preprocessed file, it inserts only the &#039;&#039;&#039;relevant declarations&#039;&#039;&#039; (e.g., function prototypes, structs, macros) in a clean and readable format.&lt;br /&gt;
&lt;br /&gt;
=== RemoveIncludes Transformation in emmtrix Studio ===&lt;br /&gt;
The &#039;&#039;&#039;RemoveIncludes&#039;&#039;&#039; transformation in emmtrix Studio resolves and eliminates all non-standard includes from a source file. It can be invoked via&lt;br /&gt;
&amp;lt;code&amp;gt;#pragma EMX_TRANSFORMATION_GLOBAL RemoveIncludes&amp;lt;/code&amp;gt;&lt;br /&gt;
or through the graphical user interface.&lt;br /&gt;
&lt;br /&gt;
The transformation extracts all declarations from the included header files and inserts them directly into the C source file, replacing the corresponding &amp;lt;code&amp;gt;#include&amp;lt;/code&amp;gt; directives. System includes such as &amp;lt;code&amp;gt;&amp;amp;lt;stdio.h&amp;amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;&amp;amp;lt;stdlib.h&amp;amp;gt;&amp;lt;/code&amp;gt; are left untouched.&lt;br /&gt;
&lt;br /&gt;
After applying this transformation, the resulting &amp;lt;code&amp;gt;.c&amp;lt;/code&amp;gt; file is fully self-contained and can be compiled without any external include files.&lt;br /&gt;
&lt;br /&gt;
=== Purpose ===&lt;br /&gt;
This transformation is useful when:&lt;br /&gt;
* creating &#039;&#039;&#039;standalone compilable files&#039;&#039;&#039;, e.g., for testing, simulation, or external distribution&lt;br /&gt;
* analyzing or debugging code where &#039;&#039;&#039;include chains introduce complexity&#039;&#039;&#039;&lt;br /&gt;
* preparing code for &#039;&#039;&#039;environments without access to full header hierarchies&#039;&#039;&#039;&lt;br /&gt;
* exporting generated code for &#039;&#039;&#039;toolchain-agnostic or embedded workflows&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;my_driver.h&amp;quot;&lt;br /&gt;
#include &amp;quot;board_cfg.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
    init_driver();&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* auto-generated by RemoveIncludes Transformation */&lt;br /&gt;
void init_driver(void);&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
    init_driver();&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Parameters ===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Parameter !! Default !! Description&lt;br /&gt;
|-&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | Currently, no parameters &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Limitations ===&lt;br /&gt;
* The transformation &#039;&#039;&#039;does not take system includes&#039;&#039;&#039; (e.g., &amp;lt;code&amp;gt;&amp;amp;lt;stdio.h&amp;amp;gt;&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;&amp;amp;lt;math.h&amp;amp;gt;&amp;lt;/code&amp;gt;) into account, although that might be changed in the future.&lt;br /&gt;
* &#039;&#039;&#039;Static functions or internal declarations&#039;&#039;&#039; that are not exposed in headers will not be visible&lt;br /&gt;
* &#039;&#039;&#039;Header guards, conditional compilation logic or complex macro definitions&#039;&#039;&#039; are not always fully preserved&lt;br /&gt;
&lt;br /&gt;
[[Category:Code Transformation]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Remove_Includes_Transformation&amp;diff=2782</id>
		<title>Remove Includes Transformation</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Remove_Includes_Transformation&amp;diff=2782"/>
		<updated>2025-08-06T06:56:34Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: Created page with &amp;quot;== RemoveIncludes Transformation == The &amp;#039;&amp;#039;&amp;#039;RemoveIncludes Transformation&amp;#039;&amp;#039;&amp;#039; removes all non-standard include directives from a C source file and replaces them with the corresponding declarations directly within the file. The result is a self-contained &amp;lt;code&amp;gt;.c&amp;lt;/code&amp;gt; file that can be compiled independently of external headers.  This transformation effectively performs a header-flattening step that normally occurs during preprocessing in a compiler’s build pipeline. How...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;== RemoveIncludes Transformation ==&lt;br /&gt;
The &#039;&#039;&#039;RemoveIncludes Transformation&#039;&#039;&#039; removes all non-standard include directives from a C source file and replaces them with the corresponding declarations directly within the file. The result is a self-contained &amp;lt;code&amp;gt;.c&amp;lt;/code&amp;gt; file that can be compiled independently of external headers.&lt;br /&gt;
&lt;br /&gt;
This transformation effectively performs a header-flattening step that normally occurs during preprocessing in a compiler’s build pipeline. However, instead of producing a preprocessed file, it inserts only the &#039;&#039;&#039;relevant declarations&#039;&#039;&#039; (e.g., function prototypes, structs, macros) in a clean and readable format.&lt;br /&gt;
&lt;br /&gt;
=== RemoveIncludes Transformation in emmtrix Studio ===&lt;br /&gt;
The &#039;&#039;&#039;RemoveIncludes&#039;&#039;&#039; transformation in emmtrix Studio resolves and eliminates all non-standard includes from a source file. It can be invoked via&lt;br /&gt;
&amp;lt;code&amp;gt;#pragma EMX_TRANSFORMATION RemoveIncludes&amp;lt;/code&amp;gt;&lt;br /&gt;
or through the graphical user interface.&lt;br /&gt;
&lt;br /&gt;
The transformation extracts all declarations from the included header files and inserts them directly into the C source file, replacing the corresponding &amp;lt;code&amp;gt;#include&amp;lt;/code&amp;gt; directives. System includes such as &amp;lt;code&amp;gt;&amp;amp;lt;stdio.h&amp;amp;gt;&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;&amp;amp;lt;stdlib.h&amp;amp;gt;&amp;lt;/code&amp;gt; are left untouched.&lt;br /&gt;
&lt;br /&gt;
After applying this transformation, the resulting &amp;lt;code&amp;gt;.c&amp;lt;/code&amp;gt; file is fully self-contained and can be compiled without any external include files.&lt;br /&gt;
&lt;br /&gt;
=== Purpose ===&lt;br /&gt;
This transformation is useful when:&lt;br /&gt;
* creating &#039;&#039;&#039;standalone compilable files&#039;&#039;&#039;, e.g., for testing, simulation, or external distribution&lt;br /&gt;
* analyzing or debugging code where &#039;&#039;&#039;include chains introduce complexity&#039;&#039;&#039;&lt;br /&gt;
* preparing code for &#039;&#039;&#039;environments without access to full header hierarchies&#039;&#039;&#039;&lt;br /&gt;
* exporting generated code for &#039;&#039;&#039;toolchain-agnostic or embedded workflows&#039;&#039;&#039;&lt;br /&gt;
&lt;br /&gt;
=== Example ===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
#include &amp;quot;my_driver.h&amp;quot;&lt;br /&gt;
#include &amp;quot;board_cfg.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
    init_driver();&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* auto-generated by RemoveIncludes Transformation */&lt;br /&gt;
void init_driver(void);&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
    init_driver();&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Parameters ===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Parameter !! Default !! Description&lt;br /&gt;
|-&lt;br /&gt;
| colspan=&amp;quot;4&amp;quot; | Currently, no parameters &lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Limitations ===&lt;br /&gt;
* The transformation &#039;&#039;&#039;does not take system includes&#039;&#039;&#039; (e.g., &amp;lt;code&amp;gt;&amp;amp;lt;stdio.h&amp;amp;gt;&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;&amp;amp;lt;math.h&amp;amp;gt;&amp;lt;/code&amp;gt;) into account, although that might be changed in the future.&lt;br /&gt;
* &#039;&#039;&#039;Static functions or internal declarations&#039;&#039;&#039; that are not exposed in headers will not be visible&lt;br /&gt;
* &#039;&#039;&#039;Header guards, conditional compilation logic or complex macro definitions&#039;&#039;&#039; are not always fully preserved&lt;br /&gt;
&lt;br /&gt;
[[Category:Code Transformation]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Infineon_AURIX_TC3xx&amp;diff=2759</id>
		<title>Infineon AURIX TC3xx</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Infineon_AURIX_TC3xx&amp;diff=2759"/>
		<updated>2025-07-04T07:17:50Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: /* GCC for AURIX™ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:AURIX TC399 LFBGA-516 plain.jpg|thumb|Infineon AURIX™ TC3xx second-generation microcontroller (TC399 BGA package). The AURIX™ TC3xx family integrates multiple TriCore™ CPUs and on-chip safety/security modules for automotive applications.|alt=TC399 BGA package]]&lt;br /&gt;
The &#039;&#039;&#039;Infineon AURIX™ TC3xx&#039;&#039;&#039; (Automotive Realtime Integrated neXt-generation architecture) is a family of 32-bit automotive microcontrollers introduced as the second generation of Infineon’s AURIX™ platform. Launched in 2016 as a successor to the earlier [[Infineon AURIX TC2xx|AURIX™ TC2xx series]], the TC3xx devices emphasize high performance, functional safety, and embedded security for advanced automotive and industrial applications. AURIX™ TC3xx microcontrollers feature a hexa-core [[TriCore Instruction Set Architecture|&#039;&#039;TriCore&#039;&#039; processor architecture]] with enhanced safety measures, making them well-suited for demanding tasks such as autonomous driving domain controllers, powertrain control, and sensor data fusion in advanced driver-assistance systems (ADAS). The combination of multiple CPU cores, extensive on-chip memory, and a rich peripheral set allows TC3xx chips to deliver real-time responsiveness and reliability in safety-critical environments while meeting stringent automotive standards (such as ISO 26262 for functional safety).&amp;lt;ref name=&amp;quot;ref1951907555&amp;quot;&amp;gt;Safety Microcontrollers: Texas Instruments Hercules vs Infineon AURIX https://www.linkedin.com/pulse/safety-microcontrollers-texas-instruments-hercules-vs-cook-meng-miet&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref1428109010&amp;quot;&amp;gt;AURIX™ microcontroller TC3xx family of Infineon fuels automated driving and electromobility - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2016/INFATV201610-005.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref1948477695&amp;quot;&amp;gt;32-bit TriCore™ AURIX™– TC3xx - Infineon Technologies https://www.infineon.com/cms/en/product/microcontroller/32-bit-tricore-microcontroller/32-bit-tricore-aurix-tc3xx/&amp;lt;/ref&amp;gt;&lt;br /&gt;
==Applications==&lt;br /&gt;
AURIX™ TC3xx microcontrollers are used in a variety of safety-critical and high-performance applications across the automotive and industrial sectors. Key application domains include:&lt;br /&gt;
*&#039;&#039;&#039;Automotive Systems:&#039;&#039;&#039; The TC3xx family was primarily designed for automotive use and finds broad application in vehicles’ electronic control units (ECUs). Notably, it is used in &#039;&#039;powertrain and drivetrain control&#039;&#039; (engine management, transmission control, hybrid/electric vehicle inverter control, battery management) where its real-time processing and resilience support precise actuator control. It is also deployed in &#039;&#039;chassis and safety systems&#039;&#039; such as anti-lock braking systems (ABS), electronic stability control, airbag controllers, and electric power steering, leveraging its redundancy and fast response for fail-safe operation. With the growth of ADAS and autonomous driving, AURIX™ TC3xx devices serve as domain controllers for &#039;&#039;ADAS sensor fusion&#039;&#039;, handling data from radar, LiDAR, camera, and ultrasonic sensors and performing environment modeling and decision-making algorithms. Their combination of multiple cores and a built-in radar signal processing sub-system makes them an attractive choice for tasks like front radar processing, sensor fusion hubs, and &#039;&#039;autonomous driving domain controllers&#039;&#039; that must meet ASIL-D safety levels.&amp;lt;ref name=&amp;quot;ref14281090102&amp;quot;&amp;gt;AURIX™ microcontroller TC3xx family of Infineon fuels automated driving and electromobility - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2016/INFATV201610-005.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref572781451&amp;quot;&amp;gt;Safety Joins Performance: Infineon Introduces Automotive Multicore 32-bit Microcontroller Family AURIX™ to Meet Safety and Powertrain Requirements of Upcoming Vehicle Generations - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2012/INFATV201205-040.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
*&#039;&#039;&#039;Industrial Automation and Robotics:&#039;&#039;&#039; Infineon has also positioned the AURIX™ family for use in industrial applications that demand high reliability. The TC3xx MCUs, with their hardware self-test mechanisms and safety documentation, can be found in &#039;&#039;industrial motor drives, robotics controllers, and PLCs (programmable logic controllers)&#039;&#039;, where they help achieve functional safety certifications up to SIL-3 (according to IEC 61508). The powerful TriCore™ cores and rich peripheral set enable complex motor control algorithms (e.g., field-oriented control for servo motors) and real-time sensor processing in factory automation. Additionally, the support for automotive-grade networking (CAN/CAN-FD, Ethernet) and timing peripherals makes them suitable for synchronous control in robotics and machinery. Infineon’s provision of AUTOSAR-compatible software for AURIX™ has even facilitated its use in non-automotive settings such as medical devices and elevator controllers, where proven safety and security are required.&amp;lt;ref name=&amp;quot;ref496771205&amp;quot;&amp;gt;Infineon&#039;s AURIX™ &amp;amp; TRAVEO™ microcontroller families extend their support for IEC 61508 hardware and software metrics enabling industrial safety up to SIL-3 - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2023/INFATV202303-078.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
*&#039;&#039;&#039;Aerospace and Defense:&#039;&#039;&#039; The high fault-tolerance and deterministic performance of the AURIX™ TC3xx architecture also attract interest for aerospace and other mission-critical uses. In avionics systems or autonomous drones, for instance, the lockstep core configuration and robust error checking of AURIX™ can provide an added layer of reliability for flight control computers or navigation systems. While primarily an automotive chip, its ability to operate in harsh conditions (−40°C to 150°C range) and to detect internal failures in real-time makes it viable for certain aerospace electronic systems. Some defense and aerospace projects leverage COTS (commercial off-the-shelf) automotive components like AURIX™ for applications such as unmanned vehicle control and high-reliability communication links, given its safety pedigree. (In these domains, additional analysis and possibly radiation testing would be performed if the devices are used in high-altitude or space environments.)&lt;br /&gt;
== Architecture Overview ==&lt;br /&gt;
[[File:Infineon TC3xx Architecture.webp|545x545px|thumb|alt=TC3xx Architecture Overview|TC3xx Architecture Overview]]&lt;br /&gt;
=== Processor Cores and Architecture===&lt;br /&gt;
The AURIX™ TC3xx family implements Infineon’s TriCore™ architecture, which uniquely combines aspects of RISC microcontrollers, DSP (digital signal processor) capabilities, and conventional microprocessor features into a single core design. Each TC3xx device can include up to six independent TriCore™ 32-bit CPU cores (TriCore™ version 1.6.2), all capable of running at up to 300 MHz clock frequency. Four of these cores can operate in dual-core lockstep pairs (with one core acting as a redundant checker for its partner) to provide fault detection for safety-critical functions. The TriCore™ cores use a superscalar Harvard architecture with 32-bit instruction words (and support for mixed 16/32-bit instruction encoding for code density). Each core includes a dedicated Floating Point Unit (FPU) for single-precision arithmetic and supports DSP-oriented instructions (e.g. single-cycle 16×16 MAC operations, SIMD for 16-bit/8-bit data) to accelerate signal processing tasks. For fast real-time performance, the architecture offers low interrupt latency (with automatic context save on-chip) and features like zero-overhead loops and a programmable peripheral timer unit for scheduling tasks. In total, a high-end TC3xx (such as the TC39x) provides six 300 MHz cores plus four checker cores, yielding a theoretical processing throughput of up to ~2400 DMIPS (Dhrystone MIPS) when all cores are utilized. This represents roughly a threefold increase in computational power over the previous AURIX™ generation (which achieved ~740 DMIPS with three cores).&amp;lt;ref name=&amp;quot;ref19484776952&amp;quot;&amp;gt;32-bit TriCore™ AURIX™– TC3xx - Infineon Technologies https://www.infineon.com/cms/en/product/microcontroller/32-bit-tricore-microcontroller/32-bit-tricore-aurix-tc3xx/&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref2022357806&amp;quot;&amp;gt;UDE Debug, Trace and Test solutions for Infineon TriCore™ AURIX™ TC32, TC33, TC35, TC36, TC37, TC38, TC39, TC3x Microcontrollers       https://www.pls-mc.com/products/infineon-tricore-aurix-tc32-tc33-tc35-tc36-tc37-tc38-tc39-microcontrollers/&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref14281090103&amp;quot;&amp;gt;AURIX™ microcontroller TC3xx family of Infineon fuels automated driving and electromobility - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2016/INFATV201610-005.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Memory Subsystem and Cache===&lt;br /&gt;
AURIX™ TC3xx microcontrollers integrate a large on-chip memory system to meet real-time and safety requirements. Program flash memory sizes range up to 16 MB on the largest models, with over 6 MB of on-chip SRAM available for data and scratchpad usage. Each TriCore™ CPU has its own tightly-coupled local memories as well as caches: for example, each core includes a 32 KB instruction cache and a 16 KB data cache. In addition, every core has separate local RAM blocks (Harvard architecture) for instructions and data (often termed PSPR and DSPR – Program Scratch-Pad RAM and Data Scratch-Pad RAM) that allow deterministic access for time-critical routines. Shared global memory is provided via a Local Memory Unit (LMU) which can be up to several hundred kilobytes (for instance, 768 KB in the TC39x) accessible by all cores. The memory system is connected by a high-bandwidth internal bus fabric (the SRI crossbar and peripheral buses) that arbitrates access between cores, DMA engines, and peripherals. This architecture ensures that each core can quickly fetch instructions and data either from its local cache/RAM or the central memory, enabling efficient parallel processing. Memory protection hardware is also built-in to isolate tasks of different criticality, an important feature for safety and security.&amp;lt;ref name=&amp;quot;ref19484776952&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref5727814512&amp;quot;&amp;gt;Safety Joins Performance: Infineon Introduces Automotive Multicore 32-bit Microcontroller Family AURIX™ to Meet Safety and Powertrain Requirements of Upcoming Vehicle Generations - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2012/INFATV201205-040.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Peripherals and Interfaces===&lt;br /&gt;
One of the strengths of the AURIX™ TC3xx architecture is its extensive set of on-chip peripherals and interface controllers, designed to handle the complex networking and I/O needs of modern vehicles and industrial systems. Key peripheral features and interfaces include:&amp;lt;ref name=&amp;quot;ref19484776952&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref2022357806&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref14281090103&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref3635926&amp;quot;&amp;gt;AURIX™ Training System Architecture https://www.infineon.com/dgdl/Infineon-AURIX_TC3xx_System_Architecture-Training-v01_00-EN.pdf?fileId=5546d46272e49d2a0172eb476d56739e&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Networking Interfaces:&#039;&#039;&#039; Multiple CAN FD controllers (supporting up to 12 CAN-FD channels in high-end variants) for in-vehicle networking, with legacy CAN and LIN interfaces (up to 24 LIN channels) for lower-speed device communication. Many TC3xx devices also integrate up to two FlexRay controllers for time-triggered automotive networks (maintaining compatibility with earlier automotive communication standards). For high-bandwidth data, a Gigabit Ethernet MAC is included, enabling uses like automotive Ethernet (e.g., audio-video bridging and IP-based connectivity).&lt;br /&gt;
* &#039;&#039;&#039;Analog and Timers:&#039;&#039;&#039; A suite of analog peripherals including multiple ADC units for sensor interfacing and motor control, and DACs in some models. Sophisticated timer units are on-chip, notably the Generic Timer Module (GTM) and Capture/Compare Units (CCU6), which can generate complex PWM waveforms and handle events for engine control, power inverters, or robotics with minimal CPU intervention. These timers enable precise control of actuators (e.g. fuel injection timing, electric motor commutation) in real time.&lt;br /&gt;
*&#039;&#039;&#039;Serial Interfaces:&#039;&#039;&#039; SPI, I²C, UART/LIN and other serial interfaces for connecting to external sensors, actuator controllers, and memory. The controllers often support multiple instances (several SPI channels, etc.) to accommodate numerous devices. AURIX™ TC3xx also provides an external bus interface (EBU) for parallel memory or FPGA connectivity, and an SD/MMC interface (eMMC) to connect external flash memory for data logging or over-the-air update storage.&lt;br /&gt;
*&#039;&#039;&#039;Dedicated Processing Units:&#039;&#039;&#039; Some AURIX™ TC3xx models include special accelerators, such as a hardware Fast Fourier Transform unit (HW-FFT) and up to two Signal Processing Units (SPUs) optimized for radar signal processing. These enable the microcontroller to handle radar front-end data or other intensive DSP tasks (like Fourier transforms for radar or audio signals) on-chip, which is crucial for advanced driver assistance sensors. There is also a &#039;&#039;Security Module&#039;&#039; (covered in a later section) and a Safety Management Unit that supervise system integrity.&lt;br /&gt;
*&#039;&#039;&#039;Other On-chip Modules:&#039;&#039;&#039; Watchdog timers, error-correction coders for memories (ECC on flash and RAM for reliability), a programmable interrupt system, multiple DMA channels (up to 128 DMA channels) for efficient data movement, and debug interfaces (JTAG and a 2/3-pin DAP) for development. The on-chip debug system supports tracing of program execution (via a built-in Emulation Memory, MCDS) to aid in complex multicore software development.&lt;br /&gt;
&lt;br /&gt;
Overall, the AURIX™TC3xx provides a highly integrated SoC-like platform, allowing a single chip to manage tasks ranging from powertrain control and chassis control to infotainment gateways. This high level of integration reduces the need for separate companion chips and helps to lower system complexity and cost.&lt;br /&gt;
===Performance and Benchmarking===&lt;br /&gt;
Fabricated in a 40 nm embedded flash process&amp;lt;ref name=&amp;quot;ref2022357806&amp;quot; /&amp;gt;, AURIX™ TC3xx processors achieve significant performance gains over prior generations while maintaining automotive-grade power efficiency. With six 32-bit cores at 300 MHz (four of them paired in lockstep), a flagship TC3xx device (e.g., TC39x) can deliver up to approximately 2,400 DMIPS of aggregate computing throughput&amp;lt;ref name=&amp;quot;ref2022357806&amp;quot; /&amp;gt;. In terms of per-core performance, the TriCore™ architecture yields around 1.3–2.0 DMIPS/MHz depending on the instruction mix, meaning each 300 MHz core can approach ~400–600 DMIPS in optimized scenarios&amp;lt;ref name=&amp;quot;ref14281090103&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref1222155140&amp;quot;&amp;gt;Infineon AURIX - Wikipedia https://en.wikipedia.org/wiki/Infineon_AURIX&amp;lt;/ref&amp;gt;. This is on par with or exceeding many contemporary 32-bit automotive MCUs. The integrated DSP extensions and optional accelerators (like the FFT unit) also enable high-speed signal processing, with Infineon citing up to ~1.8 GFLOPS of DSP throughput for certain operations&amp;lt;ref name=&amp;quot;ref1222155140&amp;quot; /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Real-world performance improvements of the TC3xx over its predecessor are notable: for example, compared to the first-generation AURIX™ (which offered up to 740 DMIPS), the TC3xx’s expanded core count and clock speed provide roughly three times the computational power on the same type of workload&amp;lt;ref name=&amp;quot;ref14281090103&amp;quot; /&amp;gt;. This extra headroom allows more functions (such as running multiple control algorithms or sensor fusion tasks in parallel) to be consolidated onto one microcontroller without compromising real-time deadlines&amp;lt;ref name=&amp;quot;ref14281090103&amp;quot; /&amp;gt;. Despite the increase in speed and core count, the TC3xx family was designed with power and thermal constraints in mind, achieving these gains within similar power consumption envelopes as the previous generation by leveraging the smaller 40 nm process and power-saving features like clock gating&amp;lt;ref name=&amp;quot;ref5727814512&amp;quot; /&amp;gt;. The deterministic real-time performance (with features like lockstep and tightly-coupled memory) makes the AURIX™ TC3xx suitable for the most demanding automotive applications, where consistent low latency and high throughput are required simultaneously.&lt;br /&gt;
&lt;br /&gt;
== On-Chip System Connectivity and Bridges ==&lt;br /&gt;
[[File:TC3xx On-Chip System Connectivity.png|thumb|The TC3xx has two &#039;&#039;&#039;SRI Domains&#039;&#039;&#039; (SRI0 and SRI1). CPU0 to CPU3 are connected to SRI0, CPU4/5 are connected to SRI1.|alt=TC3xx On-Chip System Connectivity|423x423px]]&lt;br /&gt;
The AURIX™ TC3xx Platform has three independent on-chip connectivity resources: &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System Resource Interconnect Fabric (SRI Fabric)&#039;&#039;&#039; connects the TriCore™ CPUs, the DMA module, and other high bandwidth requestors to high bandwidth memories and other resources for instruction fetches and data accesses. A key component of the fabric is the SRI crossbar, which connects all the agents in one SRI domain. The SRI crossbar carries the transactions between the SRI Masters and SRI Slaves of the domain. The SRI crossbar supports parallel transactions between different SRI Master and SRI Slave agents. In addition to the parallelism of concurrent requests, it also supports pipelined requests from an SRI Master to a SRI Slave.&lt;br /&gt;
* &#039;&#039;&#039;System Peripheral Bus (SPB)&#039;&#039;&#039; connects the TriCore™ CPUs, the DMA module, and other SPB masters to the medium and low bandwidth peripherals. SPB masters do not directly connect to the SRI Fabric, and will access SRI attached resources via a SFI_F2S Bridge.&lt;br /&gt;
* &#039;&#039;&#039;Back Bone Bus (BBB)&#039;&#039;&#039; connects the TriCore™ CPUs, the DMA module, and SPB masters with ADAS resources. SRI Masters do not directly connect to the BBB, but access BBB attached resources via a SFI_S2F Bridge. SPB masters also do not directly connect to the BBB, but access BBB attached resources via bridging over the SRI Fabric.&lt;br /&gt;
&lt;br /&gt;
=== CPU Resource Access Times ===&lt;br /&gt;
These tables describe the CPU access times to various resources in CPU clock cycles for the AURIX™ TC3xx Platform. In the case of load or fetch accesses, the access times are the minimum number of CPU stall cycles to complete the access. If there is a conflict for the resource accessed, there may be additional stall cycles till the conflicting access completes. &lt;br /&gt;
&lt;br /&gt;
For write access, the access times are the maximum for a sequence of such access (non-conflicting). In many cases for a singleton access, or a short sequence, write buffering reduces the stall effect seen by a CPU, sometimes to 0. However, as with loads and fetches, if there is a conflict for the resource accessed, there may be additional stall cycles till the conflicting access completes.&lt;br /&gt;
&lt;br /&gt;
==== Access latency for global resources ====&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+&lt;br /&gt;
! CPU Access Type !! CPU stall cycles&lt;br /&gt;
!Notes&lt;br /&gt;
|-&lt;br /&gt;
| Data read from System Peripheral Bus (SPB) || &amp;lt;math&amp;gt;\frac{f_\text{CPU}}{f_\text{SPB}} \cdot (4 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |The final number of stall cycles will depend&amp;lt;br&amp;gt;on the real number of WS generated by the target resource.&lt;br /&gt;
|-&lt;br /&gt;
| Data write to System Peripheral Bus (SPB) || &amp;lt;math&amp;gt;\frac{f_\text{CPU}}{f_\text{SPB}} \cdot (4 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Data read from Back Bone Bus (BBB) (TC39x, TC37xED) || &amp;lt;math&amp;gt;9 + \frac{f_\text{CPU}}{f_\text{BBB}} \cdot (5 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |When SFI_S2F is connected to XBar2 (TC39x and TC37xED)&amp;lt;br&amp;gt;there is an additional latency due to access going through an S2S.&lt;br /&gt;
|-&lt;br /&gt;
| Data write to Back Bone Bus (BBB) (TC39x, TC37xED) || &amp;lt;math&amp;gt;5 + \frac{f_\text{CPU}}{f_\text{BBB}} \cdot (4 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Data read from Back Bone Bus (BBB) (TC35x, TC33xED) || &amp;lt;math&amp;gt;6 + \frac{f_\text{CPU}}{f_\text{BBB}} \cdot (5 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
|-&lt;br /&gt;
| Data write to Back Bone Bus (BBB) (TC35x, TC33xED) || &amp;lt;math&amp;gt;3 + \frac{f_\text{CPU}}{f_\text{BBB}} \cdot (4 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;small&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;Module Wait State&#039;&#039;&#039;: The number of wait states for read and for write accesses is &amp;gt;= 1 and depends on the accessed module and its configuration.&lt;br /&gt;
&amp;lt;/small&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== CPU Accesses: Stall cycles for local and SRI resources ====&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+&lt;br /&gt;
! CPU Access Type !! Local CPU !! Local SRI !! Remote SRI Domain&lt;br /&gt;
|-&lt;br /&gt;
| Data read from DSPR || 0 || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data write to DSPR || 0 || 5, {{rev|3|with Pipelining}}|| 5, {{rev|4|with Pipelining}}&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from DSPR || See local SRI column || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data read from DLMU || 0 || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data write to DLMU || 2 || 5, {{rev|3|with Pipelining}}|| 5, 4&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from DLMU || See local SRI column || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data read from PSPR || See local SRI column || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data write to PSPR || See local SRI column || 5, {{rev|3|with Pipelining}}|| 5, {{rev|4|with Pipelining}}&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from PSPR || 0 || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data read from PFlash || 5 + PWS || 10 + PWS || 13 + PWS&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from PFlash (buffer miss) || 2 + PWS || 9 + PWS || 12 + PWS&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from PFlash (buffer hit) || 3 || 6 || 9&lt;br /&gt;
|-&lt;br /&gt;
| Data read from LMU || n.a. || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data write to LMU || n.a. || 5, {{rev|3|with Pipelining}}|| 5, {{rev|4|with Pipelining}}&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from LMU || n.a. || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data read from DFlash || n.a. || 5 + 3*(3 + DCWS) || 8 + 3*(3 + DCWS)&lt;br /&gt;
|-&lt;br /&gt;
| Data read access from EMEM (TC39x, TC37xED) || n.a. || n.a. || 14, {{rev|15|f&amp;lt;sub&amp;gt;BBB&amp;lt;/sub&amp;gt; &amp;lt; f&amp;lt;sub&amp;gt;SRI&amp;lt;/sub&amp;gt;}}&lt;br /&gt;
|-&lt;br /&gt;
| Data write access to EMEM (TC39x, TC37xED) || n.a. || n.a. || 9&lt;br /&gt;
|-&lt;br /&gt;
| Data read access from EMEM (TC35x, TC33xED) || n.a. || 11, {{rev|12|f&amp;lt;sub&amp;gt;BBB&amp;lt;/sub&amp;gt; &amp;lt; f&amp;lt;sub&amp;gt;SRI&amp;lt;/sub&amp;gt;}}|| n.a.&lt;br /&gt;
|-&lt;br /&gt;
| Data write access to EMEM (TC35x, TC33xED) || n.a. || 9 || n.a.&lt;br /&gt;
|-&lt;br /&gt;
| Data read access from DAM || n.a. || 10 || 13&lt;br /&gt;
|-&lt;br /&gt;
| Data write access to DAM || n.a. || 7 || 7&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;small&amp;gt;&#039;&#039;&#039;Remote SRI Domain&#039;&#039;&#039;: Only applies to products with SRI extenders. Additional latency due to access going through an S2S&amp;lt;/small&amp;gt;&lt;br /&gt;
* &amp;lt;small&amp;gt;&#039;&#039;&#039;PWS&#039;&#039;&#039;: Configured PFlash Wait States (Includes cycles for PFlash access cycles only). ECC correction latency is only incurred when the incoming data requires ECC correction.&amp;lt;/small&amp;gt;&lt;br /&gt;
* &amp;lt;small&amp;gt;&#039;&#039;&#039;PWS&#039;&#039;&#039;: Configured PFlash Wait States (Includes cycles for PFlash access cycles only). ECC correction latency is only incurred when the incoming data requires ECC correction.&amp;lt;/small&amp;gt;&lt;br /&gt;
* &amp;lt;small&amp;gt;&#039;&#039;&#039;DCWS&#039;&#039;&#039;: Configured DFlash Corrected Wait States (Includes cycles for DFlash access cycles and ECC correction latency)&amp;lt;/small&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== CPU Subsystem ==&lt;br /&gt;
[[File:TC3xx Processor Core, Local Memory and Connectivity.png|alt=TC3xx Processor Core, Local Memory and Connectivity|thumb|Processor Core, Local Memory and Connectivity]]The Infineon AURIX™ TC3xx features up to 6 processor cores (CPU0 ... CPU5) implementing the [[TriCore Instruction Set Architecture|TC1.6.2 instruction set architecture]]. The following section focuses on the microarchitectural details of the CPU subsystem. For more information about the ISA, please take a look at the [[TriCore Instruction Set Architecture]].&lt;br /&gt;
&lt;br /&gt;
The processor core connects to the following memories and bus interfaces (where implemented):&lt;br /&gt;
*Program Scratch-Pad SRAM (PSPR)&lt;br /&gt;
*Data Scratch-Pad SRAM (DSPR)&lt;br /&gt;
*Program Cache (PCache)&lt;br /&gt;
*Data Cache (DCache)&lt;br /&gt;
*Local Memory Unit (DLMU)&lt;br /&gt;
*Local Pflash bank (LPB)&lt;br /&gt;
*SRI slave interface (x2)&lt;br /&gt;
*SRI master Interface&lt;br /&gt;
*SPB master interface&lt;br /&gt;
&lt;br /&gt;
=== TC1.6.2P Implementation Features ===&lt;br /&gt;
*Most instructions executed in 1 cycle&lt;br /&gt;
*Branch instructions in 1, 2 or 3 cycles (using dynamic branch prediction)&lt;br /&gt;
*Wide memory interface for fast context switch&lt;br /&gt;
* Automatic context save-on-entry and restore-on-exit for: subroutine, interrupt, trap&lt;br /&gt;
*Six memory protection register sets&lt;br /&gt;
*Dual instruction issuing (in parallel into Integer Pipeline and Load/Store Pipeline)&lt;br /&gt;
*Third pipeline for loop instruction only (zero overhead loop)&lt;br /&gt;
*Single precision Floating Point Unit (IEEE-754 Compatible)&lt;br /&gt;
*Dedicated Integer divide unit&lt;br /&gt;
*18 data memory protection ranges, 10 code memory protection ranges arranged in 6 sets&lt;br /&gt;
&lt;br /&gt;
=== Superscalar Architecture ===&lt;br /&gt;
The processor core within the AURIX™ TC3xx family, specifically the TC1.6.2P implementation, employs a superscalar architecture characterized by three parallel pipelines: the Integer Pipeline, the Load/Store Pipeline, and the Loop Pipeline. Superscalar execution is a form of instruction-level parallelism that enables the processor to issue and execute multiple instructions during a single clock cycle. The TriCore™ TC3xx core&#039;s superscalar architecture allows it to issue and execute multiple instructions in parallel. Specifically, it can issue up to two instructions simultaneously to the Integer and Load/Store pipelines. However, there are certain constraints on this dual issuing capability. For example, back-to-back data arithmetic instructions can only be issued in separate cycles, and a load/store instruction can be issued either on its own or paired with a data arithmetic instruction, provided the load/store instruction is the second in the pair. Under ideal conditions, with careful instruction scheduling, this dual-issue capability can lead to a throughput of close to 0.5 clock cycles per instruction, effectively executing two instructions per cycle.&lt;br /&gt;
&lt;br /&gt;
=== Pipeline Stages ===&lt;br /&gt;
The execution of instructions within the TriCore™ TC3xx core is managed through a pipeline consisting of several stages. In one document it is mentioned that the pipeline consists of 6 stages &amp;lt;ref name=&amp;quot;ref3635926&amp;quot; /&amp;gt;, but the exact stages remain unclear. From the architecture the following stages can be inferred:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Fetch Stage&#039;&#039;&#039;: The initial stage in the pipeline is the Fetch stage, where instructions are retrieved from memory. This task is handled by the Instruction Fetch Unit (IFU), which is responsible for fetching instructions from the memory system. To improve efficiency, the IFU employs a pre-fetching mechanism, anticipating the instructions that will be needed and bringing them into the processor before they are actually required. Instructions are fetched through a 64-bit wide Program Memory Interface (PMI), allowing for the retrieval of a significant amount of instruction data in each cycle. The fetched instructions are then placed into an issue FIFO, a buffer capable of holding up to six instructions. This buffer plays a crucial role in managing the flow of instructions to the subsequent execution pipelines. The 64-bit fetch width and the instruction buffer are indicative of a design that aims to ensure a continuous supply of instructions to the pipelines, preventing them from being starved of work. A wider fetch path allows for more instruction bits to be obtained from memory in each clock cycle, while the buffer helps to mitigate temporary delays in instruction fetching.&lt;br /&gt;
* &#039;&#039;&#039;Execution Unit&#039;&#039;&#039;: The Execution Unit contains the Integer Pipeline, the Load/Store Pipeline and the Loop Pipeline. All three pipelines operate in parallel and are capable of executing instructions simultaneously. Each pipline has its own decode stage and two execute stages. Furthermore, the TC3xx features a Floating Point Unit (FPU). It is not clear whether the FPU is part of the execution unit or a separate unit. It is assumed that the FPU also has its own decode stage and an unknown number of execute stages&lt;br /&gt;
** &#039;&#039;&#039;Decode Stage&#039;&#039;&#039;: Following the Fetch stage, the instruction are issued to one of the pipelines. In the decode stage of each pipeline, the instruction is examined and translated to determine the specific operation it represents and to identify the operands it will operate on. It is assumed that operands are read from the register file during the decode stage.&lt;br /&gt;
** &#039;&#039;&#039;Execute Stage&#039;&#039;&#039;: The Execute stage is where the actual operation specified by the instruction is carried out. Pipeline hazards (stalls) are minimised by the use of forwarding paths between pipeline stages allowing the results of one instruction to be used by a following instruction as soon as the result becomes available.&lt;br /&gt;
** &#039;&#039;&#039;Writeback Stage&#039;&#039;&#039;: The final stage in the pipeline is the Writeback stage. In this stage, the results produced by the executed instruction are written back to the processor&#039;s register file, making them available for subsequent instructions. It is unclear whether the writeback stage is part of the (two) execution stages or a separate stage.&lt;br /&gt;
&lt;br /&gt;
=== Execution Pipelines ===&lt;br /&gt;
The TriCore™ TC3xx core leverages four parallel pipelines to enhance its performance. Each pipeline is designed to handle a specific class of instructions:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Integer Pipeline:&#039;&#039;&#039; This pipeline is responsible for executing integer arithmetic and logical instructions, including data-conditional jump instructions, bit manipulation operations, as well as division and multiply-accumulate (MAC) instructions.&lt;br /&gt;
* &#039;&#039;&#039;Load/Store Pipeline:&#039;&#039;&#039; The primary function of this pipeline is to manage memory access operations, including loading data from memory and storing data to memory. It also handles address arithmetic, unconditional jump instructions, procedure call instructions, context-switching operations, and control flow related to context save areas (CSAs).&lt;br /&gt;
* &#039;&#039;&#039;Loop Pipeline:&#039;&#039;&#039; This is a specialized pipeline designed to facilitate zero-overhead loops, a technique that significantly improves the performance of iterative code sections. Its design aims to eliminate or minimize the overhead typically associated with loop control, such as incrementing counters and checking loop conditions. Some documentation suggests that the loop pipeline comes along with a &amp;quot;Loop Cache&amp;quot;. This dedicated cache would likely be used to store instructions belonging to the loop, enabling very fast access and execution for repeated iterations.&lt;br /&gt;
* &#039;&#039;&#039;Floating Point Unit (FPU):&#039;&#039;&#039; The FPU is responsible for executing floating-point arithmetic operations&lt;br /&gt;
&lt;br /&gt;
=== Store Buffers ===&lt;br /&gt;
The TriCore™ TC3xx core include &#039;&#039;&#039;Store Buffers&#039;&#039;&#039; to decouple memory write operations from CPU instruction execution. All stores from the Load/Store Pipeline are placed in the store buffer prior to being written to local memory or transferred via the bus system. Write data is taken from the store buffers and written to memory when the target memory or bus interface becomes available. In normal operation the CPU will prioritise memory load operations over store operations in order to improve performance unless the store buffer is full or the order of load and store operations must be preserved (e.g. peripheral space access). The store buffer can hold the data for up to 6 stores operations. To improve performance the store buffer will merge consecutive byte, half-word, and word writes of the same location to reduce the number of memory accesses required.&lt;br /&gt;
&lt;br /&gt;
=== Instruction Timing ===&lt;br /&gt;
&lt;br /&gt;
==Functional Safety and Security Features==&lt;br /&gt;
===Hardware Security Module and Encryption===&lt;br /&gt;
One of the defining features of the AURIX™ TC3xx family is its robust hardware-based security subsystem, implemented to protect against cyber threats and unauthorized manipulation of vehicle systems. Each TC3xx MCU includes a dedicated &#039;&#039;Hardware Security Module&#039;&#039; (HSM), which is essentially an on-chip co-processor with its own CPU and cryptographic engine designed to handle secure operations. In the second-generation AURIX™ (TC3xx), the HSM was upgraded with support for asymmetric cryptography (e.g., RSA, ECC algorithms) in hardware, providing significantly faster public-key encryption and authentication capabilities than pure software implementations. This HSM supports &#039;&#039;&#039;EVITA Full&#039;&#039;&#039; (the highest level of the EU’s EVITA automotive security requirements), meaning it can perform secure boot, message authentication, and encryption at a level suitable for protecting critical in-vehicle communications. For example, the HSM can encrypt and authenticate CAN/Ethernet messages to prevent spoofing or tampering on the vehicle network, and it can manage cryptographic keys securely isolated from the main application cores.&amp;lt;ref name=&amp;quot;ref14281090104&amp;quot;&amp;gt;AURIX™ microcontroller TC3xx family of Infineon fuels automated driving and electromobility - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2016/INFATV201610-005.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref2022357806&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The security module enables features like &#039;&#039;&#039;secure boot&#039;&#039;&#039;, where the AURIX™ will only execute firmware that is digitally signed by the vehicle manufacturer, thereby preventing unauthorized or malicious code. It also facilitates &#039;&#039;over-the-air (OTA) updates&#039;&#039; in a secure manner: the TC3xx HSM can verify and decrypt update packages received via telematics, allowing firmware upgrades while protecting against attackers attempting to hijack the software. In addition, the HSM includes a true random number generator and supports protocols for immobilizer and theft protection systems. Infineon’s implementation essentially treats the HSM as an “embedded smart card” inside the microcontroller. This approach, building on Infineon’s expertise in chip card security, helps to safeguard sensitive assets like cryptographic keys and to perform secure operations (encryption, decryption, authentication) in hardware, making attacks significantly more difficult. The strong cybersecurity provisions of the AURIX™ TC3xx have made it a preferred choice for vehicle gateway ECUs and central body controllers, which are points of entry for connected car communications and thus high-value targets for hackers. By incorporating the HSM, automotive OEMs can implement advanced security features such as encrypted onboard communication (to thwart bus tampering) and secure diagnostics, ensuring the vehicle’s electronic data is protected against cyber threats even as cars become more connected.&amp;lt;ref name=&amp;quot;ref14281090104&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref5727814513&amp;quot;&amp;gt;Safety Joins Performance: Infineon Introduces Automotive Multicore 32-bit Microcontroller Family AURIX™ to Meet Safety and Powertrain Requirements of Upcoming Vehicle Generations - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2012/INFATV201205-040.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref19484776953&amp;quot;&amp;gt;32-bit TriCore™ AURIX™– TC3xx - Infineon Technologies https://www.infineon.com/cms/en/product/microcontroller/32-bit-tricore-microcontroller/32-bit-tricore-aurix-tc3xx/&amp;lt;/ref&amp;gt;&lt;br /&gt;
=== Functional Safety and Fault Tolerance===&lt;br /&gt;
Beyond cybersecurity, the AURIX™ TC3xx architecture is built with extensive &#039;&#039;functional safety&#039;&#039; features to meet the highest safety integrity levels in automotive systems. The multi-core design supports &#039;&#039;lockstep operation&#039;&#039;, where redundant checker cores run in parallel with main cores and continuously compare results cycle-by-cycle. In TC3xx devices, up to four of the six TriCore™ CPUs have such lockstep partner cores, allowing the microcontroller to detect any single-point CPU failure instantly (if a discrepancy arises between the core and its checker, a fault is signaled). A &#039;&#039;Safety Management Unit (SMU)&#039;&#039; is integrated on-chip to monitor various fault conditions throughout the device – it aggregates error signals from the lockstep cores, memory ECC monitors, clock/watchdog monitors, and peripheral self-tests, and can initiate safe-state actions (like resetting the chip or signaling an error pin to an external supervisor) if a critical fault is detected. The AURIX™ safety concept also includes a distributed Memory Protection Unit (MPU) arrangement, with multiple memory protection contexts that prevent errant or malicious code from writing to wrong memory areas. This helps contain faults and eases the integration of software with mixed criticality (Infineon allows partitioning software components of different ASIL levels on the same MCU while keeping them isolated).&amp;lt;ref name=&amp;quot;ref14281090104&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref5727814513&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref19519075552&amp;quot;&amp;gt;Safety Microcontrollers: Texas Instruments Hercules vs Infineon AURIX https://www.linkedin.com/pulse/safety-microcontrollers-texas-instruments-hercules-vs-cook-meng-miet&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Crucially, the TC3xx family was developed according to ISO 26262 processes and can achieve &#039;&#039;&#039;ASIL-D&#039;&#039;&#039; (Automotive Safety Integrity Level D), the highest grade for automotive functional safety. Compared to a traditional dual-core lockstep approach, the AURIX’s™ multi-core safety architecture allowed Infineon to reduce certain safety development overheads (earlier Infineon data suggested up to 30% reduction in safety software workload) by providing built-in safety mechanisms and diagnostics. Infineon supplies a comprehensive safety manual and diagnostic libraries for the AURIX™, helping engineers utilize features like CPU self-tests, RAM tests, and CRC checks to comply with safety standards. In addition to automotive standards, the AURIX™ TC3xx has also been qualified for &#039;&#039;&#039;industrial safety&#039;&#039;&#039;: in 2023, Infineon announced support for IEC 61508 metrics, enabling designs with AURIX™ to reach SIL-2 and SIL-3 safety integrity levels for industrial applications. This cross-industry safety compliance is facilitated by the MCU’s redundant design and detailed failure mode documentation (FMEDA) provided by Infineon. In summary, the AURIX™ TC3xx integrates both proactive safety (fault prevention via lockstep, monitoring and protection) and reactive safety (fault detection and safe shutdown), making it a dependable component for systems that cannot afford unintended behavior, whether in cars, factories, or aerospace. &amp;lt;ref name=&amp;quot;ref14281090104&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref5727814513&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref4967712052&amp;quot;&amp;gt;Infineon&#039;s AURIX™ &amp;amp; TRAVEO™ microcontroller families extend their support for IEC 61508 hardware and software metrics enabling industrial safety up to SIL-3 - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2023/INFATV202303-078.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref196858272&amp;quot;&amp;gt;MCUs support industrial functional safety - Electronic Products https://www.electronicproducts.com/mcus-support-industrial-functional-safety/&amp;lt;/ref&amp;gt;&lt;br /&gt;
== Platform Devices ==&lt;br /&gt;
The following table shows a feature overview of the AURIX™ TC3xx Platform family focusing on memory and number of cores.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!&lt;br /&gt;
!Feature&lt;br /&gt;
!TC33x&lt;br /&gt;
!TC33xEXT&lt;br /&gt;
!TC35x&lt;br /&gt;
!TC36x&lt;br /&gt;
!TC37x&lt;br /&gt;
!TC37xEXT&lt;br /&gt;
!TC38x&lt;br /&gt;
!TC39x&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |CPUs&lt;br /&gt;
|Cores / Checker Cores&lt;br /&gt;
|1 / 1&lt;br /&gt;
|2 / 1&lt;br /&gt;
|3 / 2&lt;br /&gt;
|2 / 2&lt;br /&gt;
|3 / 2&lt;br /&gt;
|3 / 3&lt;br /&gt;
|4 / 2&lt;br /&gt;
|6 / 4&lt;br /&gt;
|-&lt;br /&gt;
|Max. Freq.&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; |300 MHz&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |Cache per CPU &lt;br /&gt;
|Program [KB]&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; |32&lt;br /&gt;
|-&lt;br /&gt;
|Data [KB]&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; |16&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; |SRAM per CPU&lt;br /&gt;
|PSPR [KB]&lt;br /&gt;
|8&lt;br /&gt;
|{{rev|32|CPU0}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|64&lt;br /&gt;
|32&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|DSPR [KB]&lt;br /&gt;
|192&lt;br /&gt;
|{{rev|192|CPU0}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|{{rev|240|CPU0&amp;amp;1}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|192&lt;br /&gt;
|{{rev|240|CPU0&amp;amp;1}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|{{rev|240|CPU0&amp;amp;1}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|{{rev|240|CPU0&amp;amp;1}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|{{rev|240|CPU0&amp;amp;1}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|-&lt;br /&gt;
|DLMU [KB]&lt;br /&gt;
|8&lt;br /&gt;
|{{rev|8|CPU0}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |SRAM global&lt;br /&gt;
|LMU [KB]&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|512&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|128&lt;br /&gt;
|768&lt;br /&gt;
|-&lt;br /&gt;
|DAM [KB]&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|32&lt;br /&gt;
| -&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; |Extension Memory (EMEM)&lt;br /&gt;
|TCM [MB]&lt;br /&gt;
| -&lt;br /&gt;
|1&lt;br /&gt;
|2&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|2&lt;br /&gt;
| -&lt;br /&gt;
|2&lt;br /&gt;
|-&lt;br /&gt;
|XCM [MB]&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|1&lt;br /&gt;
| -&lt;br /&gt;
|2&lt;br /&gt;
|-&lt;br /&gt;
|XTM [KB]&lt;br /&gt;
| -&lt;br /&gt;
|16&lt;br /&gt;
|16&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|16&lt;br /&gt;
| -&lt;br /&gt;
|16&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |Program Flash&lt;br /&gt;
|Size [MB]&lt;br /&gt;
|2&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|6&lt;br /&gt;
|10&lt;br /&gt;
|10&lt;br /&gt;
|16&lt;br /&gt;
|-&lt;br /&gt;
|Banks [MB]&lt;br /&gt;
|1 x 2&lt;br /&gt;
|2 x 2&lt;br /&gt;
|2 x 2&lt;br /&gt;
|2 x 2&lt;br /&gt;
|2 x 3&lt;br /&gt;
|3 x 3, 1 x 1&lt;br /&gt;
|3 x 3, 1 x 1&lt;br /&gt;
|5 x 3, 1 x 1&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |Data Flash&lt;br /&gt;
|DF0 Size (single-ended) [KB]&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|256&lt;br /&gt;
|512&lt;br /&gt;
|512&lt;br /&gt;
|1024&lt;br /&gt;
|-&lt;br /&gt;
|DF1 Size (single-ended) [KB]&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; |DMA&lt;br /&gt;
|Channels&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|-&lt;br /&gt;
|Move Engines&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|-&lt;br /&gt;
|Resource Partitions&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Compilers ==&lt;br /&gt;
The AURIX™ TC3xx family is a series of high-performance microcontrollers widely used in automotive and industrial applications. Compilers for the AURIX™ TC3xx are crucial for developers aiming to optimize performance, reliability, and safety in their applications. One significant aspect of the compiler landscape for AURIX™ TC3xx is the limited direct support from mainline open-source compilers such as GCC or LLVM/Clang. The primary reason for this is the stringent requirements for functional safety in automotive and industrial applications, which demand specialized features and compliance with safety standards that are often not met by general-purpose open-source compilers.&lt;br /&gt;
&lt;br /&gt;
=== Commercial Compilers ===&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Tasking&#039;&#039;&#039;: Tasking compilers are renowned for their robust support for automotive applications, offering advanced debugging capabilities and optimization techniques tailored for the AURIX™ architecture. They provide extensive code optimization, comprehensive debugging tools, and strong support for safety standards such as ISO 26262, making them ideal for developing high-performance, reliable, and safe applications.&lt;br /&gt;
# &#039;&#039;&#039;HighTec&#039;&#039;&#039;: The HighTec compiler is a popular choice, known for its Eclipse-based development environment and strong multicore support. HighTec provides both GCC and LLVM-based ports of open-source compilers tailored specifically for the AURIX™ TC3xx family. These compilers offer efficient parallel execution, advanced code analysis, and an integrated development environment, ensuring robust performance and compliance with safety standards.&lt;br /&gt;
# &#039;&#039;&#039;Green Hills Software&#039;&#039;&#039;: Green Hills Software provides a highly optimized toolchain aimed at safety-critical applications, focusing on high performance and strict compliance with automotive standards. Their compiler offers superior optimization, extensive safety features, and a proprietary IDE with specialized tools for automotive development, ensuring developers can meet the stringent demands of functional safety.&lt;br /&gt;
&lt;br /&gt;
=== GCC for AURIX™ ===&lt;br /&gt;
&lt;br /&gt;
While mainline GCC does not directly support the AURIX™ TC3xx family, there is an unofficial GCC version available for AURIX™. Due to the GNU General Public License (GPL), the source code from HighTec was retrieved and, along with binary versions, published on GitHub:&lt;br /&gt;
&lt;br /&gt;
*Infineon&#039;s release of GCC for Aurix with [https://softwaretools.infineon.com/tools/com.ifx.tb.tool.aurixgcc binaries for both Linux and Windows] and Infineon&#039;s instruction set simulator [https://softwaretools.infineon.com/tools/com.ifx.tb.tool.tsimtricoreinstructionsetsimulator TSim] for Aurix.&lt;br /&gt;
*GCC 4.9.4/Binutils 2.20/Newlib 1.18 for Tricore™ AURIX™ [[https://github.com/volumit/package_494 Source]] [[https://github.com/volumit/tricore_gcc494_mingw_bins Prebuild Mingw Binaries]] [[https://github.com/volumit/tricore_gcc494_linux_bins Prebuild Linux Binaries]]&lt;br /&gt;
*GCC 9.4.0/Binutils 2.20/Newlib 1.18 for Tricore™ AURIX™ [[https://github.com/volumit/package_940 Source]] [[https://github.com/volumit/tricore_gcc940_mingw_bins Prebuild Mingw Binaries]] [[https://github.com/volumit/tricore_gcc940_linux_bins Prebuild Linux Binaries]]&lt;br /&gt;
*EEESLab&#039;s repositories of GCC, Binutils and Newlib for Tricore™ AURIX™ [[https://github.com/EEESlab/tricore-gcc-toolchain-11.3.0 Source]]&lt;br /&gt;
*NoMore201&#039;s fork of EEESLab&#039;s GCC for Tricore™ AURIX™ including GCC, Binutils, Newlib, GDB and QEmu [[https://github.com/NoMore201/tricore-gcc-toolchain Source]] [[https://github.com/NoMore201/tricore-gcc-toolchain/releases/tag/11.3.1-20250101 Sources and prebuilt binaries of gcc-11.3.1 for Linux and Windows]] [[https://github.com/NoMore201/tricore-gcc-toolchain/releases/tag/13.3-20250516 Sources and prebuilt binaries of gcc-13.3.0 for Linux and Windows]]&lt;br /&gt;
*GDB 10.0.50 for Tricore™ AURIX™ [[https://github.com/volumit/gdb-tricore Source]]&lt;br /&gt;
==emmtrix Tools for AURIX™ TC3xx==&lt;br /&gt;
[[File:lowres-Signet Associated Partner EN.eps.png|thumb|305x305px|emmtrix Technologies is an Infineon Associated Partner with over 10 years of experience working with the &#039;&#039;&#039;Infineon AURIX™ microcontroller family&#039;&#039;&#039; and has been actively collaborating with Infineon for the past five years.|alt=emmtrix Associated Partner]]&lt;br /&gt;
emmtrix offers the following tool for the Infineon AURIX™ TC3xx architecture:&lt;br /&gt;
=== emmtrix Performance Estimator ===&lt;br /&gt;
[[emmtrix Performance Estimator|emmtrix Performance Estimator (ePE)]] provides static timing analysis of C code. Compared to simulation or measurement on hardware, static performance analysis can be applied significantly earlier in the development process and will deliver results on average 6 months earlier compared to a typical automotive HIL setup. The analysis only takes a few minutes at most and runs on the developer’s PC independently of any target hardware. Function developers can analyze their runnables or SWCs without the need of a fully integrated program. emmtrix Performance Estimator is fully compatible with &#039;&#039;&#039;Infineon&#039;s AURIX™ [[Infineon AURIX TC2xx|TC2xx]] / TC3xx/ [[Infineon AURIX TC4x|TC4x]] microcontroller family,&#039;&#039;&#039; ensuring precise and reliable performance analysis for embedded systems. &lt;br /&gt;
&lt;br /&gt;
A unique feature is the combination with TargetLink or Embedded Coder generated code. Without any measurement overhead, our static performance estimation can analyze even the smallest code snippets. This allows us to map the timing analysis to Simulink blocks, giving function developers insight into the timing behavior of their models. &lt;br /&gt;
&lt;br /&gt;
ePE offers three accuracy levels: &lt;br /&gt;
&lt;br /&gt;
* analysis of C code&lt;br /&gt;
* generically compiler-optimized code&lt;br /&gt;
* assembly code from the target compiler.&lt;br /&gt;
&lt;br /&gt;
Method 1 yields results with minimum effort while method 3 takes the timing of the processor pipeline into account. All methods offer excellent reliability when tracking the tendency of changes in software runtimes e.g. when used in a continuous integration environment.&lt;br /&gt;
&lt;br /&gt;
=== Services ===&lt;br /&gt;
====Architecture &amp;amp; Variant Consulting====&lt;br /&gt;
* Support in selecting suitable AURIX derivatives for new product generations (trade-offs between performance, price, safety, and peripheral features)&lt;br /&gt;
*Analysis of requirements and mapping to specific AURIX product families (e.g., TC3xx vs. TC4xx)&lt;br /&gt;
*Analysis/identification of performance margins for down- or upgrades&lt;br /&gt;
==== Runtime Analysis &amp;amp; Performance Engineering ====&lt;br /&gt;
* Execution of detailed runtime analyses (maximum/minimum vs. average)&lt;br /&gt;
* Identification of bottlenecks in program execution, including benchmarking&lt;br /&gt;
*Optimization of access to flash, RAM, and peripherals (e.g., cache configuration, DMA utilization)&lt;br /&gt;
====Dependency Analysis====&lt;br /&gt;
*Freedom from interference (ASIL level check)&lt;br /&gt;
&lt;br /&gt;
==== Multicore Design &amp;amp; Scheduling Concepts ====&lt;br /&gt;
&lt;br /&gt;
* Development of core mapping strategies (static vs. dynamic assignment of compute-intensive tasks)&lt;br /&gt;
&lt;br /&gt;
*Support for integrating bare-metal scheduling on AURIX&lt;br /&gt;
*Assistance with architectural decisions to meet functional safety requirements in multicore systems&lt;br /&gt;
&lt;br /&gt;
==== Training &amp;amp; Know-how Transfer ====&lt;br /&gt;
&lt;br /&gt;
* Technical in-house workshops on AURIX optimization, multicore strategies&lt;br /&gt;
&lt;br /&gt;
*Coaching of employees/project teams in performance analysis and systematic tuning&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Infineon AURIX TC4x|Infineon AURIX™ TC4x]]&lt;br /&gt;
*[[Infineon AURIX TC2xx|Infineon AURIX™ TC2xx]]&lt;br /&gt;
*[[TriCore Instruction Set Architecture]]&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
*https://www.infineon.com/cms/en/product/microcontroller/32-bit-tricore-microcontroller/32-bit-tricore-aurix-tc3xx/&lt;br /&gt;
*[https://documentation.infineon.com/aurixtc3xx/docs/qmd1702366622648 AURIX™ TC3xx user manuals]&lt;br /&gt;
**[https://www.infineon.com/dgdl/Infineon-AURIX_TC3xx_Part1-UserManual-v02_00-EN.pdf?fileId=5546d462712ef9b701717d3605221d96 AURIX™ TC3xx User Manual Part-1]&lt;br /&gt;
**[https://www.infineon.com/dgdl/Infineon-AURIX_TC3xx_Part2-UserManual-v02_00-EN.pdf?fileId=5546d462712ef9b701717d35f8541d94 AURIX™ TC3xx User Manual Part-2]&lt;br /&gt;
**[https://www.infineon.com/dgdl/Infineon-AURIX_TC3xx_Architecture_vol1-UserManual-v01_00-EN.pdf?fileId=5546d46276fb756a01771bc4c2e33bdd AURIX™ TC3xx Architecture vol 1]&lt;br /&gt;
**[https://www.infineon.com/dgdl/Infineon-AURIX_TC3xx_Architecture_vol2-UserManual-v01_00-EN.pdf?fileId=5546d46276fb756a01771bc4a6d73b70 AURIX™ TC3xx Architecture vol 2]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
[[Category:Supported Architectures]]&lt;br /&gt;
&lt;br /&gt;
{{#seo:&lt;br /&gt;
 |title=Infineon AURIX TC3xx Family – Deep Dive&lt;br /&gt;
 |description=This deep dive into Infineon&#039;s AURIX TC3xx family explores its multi-core TriCore architecture, real-time capabilities, safety and security features and tools&lt;br /&gt;
 |image=File:AURIX_TC399_LFBGA-516_plain.jpg&lt;br /&gt;
 |keywords=AURIX, Infineon, TC3xx, microcontroller, TriCore, automotive, ADAS, ISO 26262, functional safety, embedded security, ASIL-D, deep dive&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Infineon_AURIX_TC3xx&amp;diff=2758</id>
		<title>Infineon AURIX TC3xx</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Infineon_AURIX_TC3xx&amp;diff=2758"/>
		<updated>2025-07-04T07:03:46Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: /* GCC for AURIX™ */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:AURIX TC399 LFBGA-516 plain.jpg|thumb|Infineon AURIX™ TC3xx second-generation microcontroller (TC399 BGA package). The AURIX™ TC3xx family integrates multiple TriCore™ CPUs and on-chip safety/security modules for automotive applications.|alt=TC399 BGA package]]&lt;br /&gt;
The &#039;&#039;&#039;Infineon AURIX™ TC3xx&#039;&#039;&#039; (Automotive Realtime Integrated neXt-generation architecture) is a family of 32-bit automotive microcontrollers introduced as the second generation of Infineon’s AURIX™ platform. Launched in 2016 as a successor to the earlier [[Infineon AURIX TC2xx|AURIX™ TC2xx series]], the TC3xx devices emphasize high performance, functional safety, and embedded security for advanced automotive and industrial applications. AURIX™ TC3xx microcontrollers feature a hexa-core [[TriCore Instruction Set Architecture|&#039;&#039;TriCore&#039;&#039; processor architecture]] with enhanced safety measures, making them well-suited for demanding tasks such as autonomous driving domain controllers, powertrain control, and sensor data fusion in advanced driver-assistance systems (ADAS). The combination of multiple CPU cores, extensive on-chip memory, and a rich peripheral set allows TC3xx chips to deliver real-time responsiveness and reliability in safety-critical environments while meeting stringent automotive standards (such as ISO 26262 for functional safety).&amp;lt;ref name=&amp;quot;ref1951907555&amp;quot;&amp;gt;Safety Microcontrollers: Texas Instruments Hercules vs Infineon AURIX https://www.linkedin.com/pulse/safety-microcontrollers-texas-instruments-hercules-vs-cook-meng-miet&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref1428109010&amp;quot;&amp;gt;AURIX™ microcontroller TC3xx family of Infineon fuels automated driving and electromobility - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2016/INFATV201610-005.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref1948477695&amp;quot;&amp;gt;32-bit TriCore™ AURIX™– TC3xx - Infineon Technologies https://www.infineon.com/cms/en/product/microcontroller/32-bit-tricore-microcontroller/32-bit-tricore-aurix-tc3xx/&amp;lt;/ref&amp;gt;&lt;br /&gt;
==Applications==&lt;br /&gt;
AURIX™ TC3xx microcontrollers are used in a variety of safety-critical and high-performance applications across the automotive and industrial sectors. Key application domains include:&lt;br /&gt;
*&#039;&#039;&#039;Automotive Systems:&#039;&#039;&#039; The TC3xx family was primarily designed for automotive use and finds broad application in vehicles’ electronic control units (ECUs). Notably, it is used in &#039;&#039;powertrain and drivetrain control&#039;&#039; (engine management, transmission control, hybrid/electric vehicle inverter control, battery management) where its real-time processing and resilience support precise actuator control. It is also deployed in &#039;&#039;chassis and safety systems&#039;&#039; such as anti-lock braking systems (ABS), electronic stability control, airbag controllers, and electric power steering, leveraging its redundancy and fast response for fail-safe operation. With the growth of ADAS and autonomous driving, AURIX™ TC3xx devices serve as domain controllers for &#039;&#039;ADAS sensor fusion&#039;&#039;, handling data from radar, LiDAR, camera, and ultrasonic sensors and performing environment modeling and decision-making algorithms. Their combination of multiple cores and a built-in radar signal processing sub-system makes them an attractive choice for tasks like front radar processing, sensor fusion hubs, and &#039;&#039;autonomous driving domain controllers&#039;&#039; that must meet ASIL-D safety levels.&amp;lt;ref name=&amp;quot;ref14281090102&amp;quot;&amp;gt;AURIX™ microcontroller TC3xx family of Infineon fuels automated driving and electromobility - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2016/INFATV201610-005.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref572781451&amp;quot;&amp;gt;Safety Joins Performance: Infineon Introduces Automotive Multicore 32-bit Microcontroller Family AURIX™ to Meet Safety and Powertrain Requirements of Upcoming Vehicle Generations - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2012/INFATV201205-040.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
*&#039;&#039;&#039;Industrial Automation and Robotics:&#039;&#039;&#039; Infineon has also positioned the AURIX™ family for use in industrial applications that demand high reliability. The TC3xx MCUs, with their hardware self-test mechanisms and safety documentation, can be found in &#039;&#039;industrial motor drives, robotics controllers, and PLCs (programmable logic controllers)&#039;&#039;, where they help achieve functional safety certifications up to SIL-3 (according to IEC 61508). The powerful TriCore™ cores and rich peripheral set enable complex motor control algorithms (e.g., field-oriented control for servo motors) and real-time sensor processing in factory automation. Additionally, the support for automotive-grade networking (CAN/CAN-FD, Ethernet) and timing peripherals makes them suitable for synchronous control in robotics and machinery. Infineon’s provision of AUTOSAR-compatible software for AURIX™ has even facilitated its use in non-automotive settings such as medical devices and elevator controllers, where proven safety and security are required.&amp;lt;ref name=&amp;quot;ref496771205&amp;quot;&amp;gt;Infineon&#039;s AURIX™ &amp;amp; TRAVEO™ microcontroller families extend their support for IEC 61508 hardware and software metrics enabling industrial safety up to SIL-3 - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2023/INFATV202303-078.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
*&#039;&#039;&#039;Aerospace and Defense:&#039;&#039;&#039; The high fault-tolerance and deterministic performance of the AURIX™ TC3xx architecture also attract interest for aerospace and other mission-critical uses. In avionics systems or autonomous drones, for instance, the lockstep core configuration and robust error checking of AURIX™ can provide an added layer of reliability for flight control computers or navigation systems. While primarily an automotive chip, its ability to operate in harsh conditions (−40°C to 150°C range) and to detect internal failures in real-time makes it viable for certain aerospace electronic systems. Some defense and aerospace projects leverage COTS (commercial off-the-shelf) automotive components like AURIX™ for applications such as unmanned vehicle control and high-reliability communication links, given its safety pedigree. (In these domains, additional analysis and possibly radiation testing would be performed if the devices are used in high-altitude or space environments.)&lt;br /&gt;
== Architecture Overview ==&lt;br /&gt;
[[File:Infineon TC3xx Architecture.webp|545x545px|thumb|alt=TC3xx Architecture Overview|TC3xx Architecture Overview]]&lt;br /&gt;
=== Processor Cores and Architecture===&lt;br /&gt;
The AURIX™ TC3xx family implements Infineon’s TriCore™ architecture, which uniquely combines aspects of RISC microcontrollers, DSP (digital signal processor) capabilities, and conventional microprocessor features into a single core design. Each TC3xx device can include up to six independent TriCore™ 32-bit CPU cores (TriCore™ version 1.6.2), all capable of running at up to 300 MHz clock frequency. Four of these cores can operate in dual-core lockstep pairs (with one core acting as a redundant checker for its partner) to provide fault detection for safety-critical functions. The TriCore™ cores use a superscalar Harvard architecture with 32-bit instruction words (and support for mixed 16/32-bit instruction encoding for code density). Each core includes a dedicated Floating Point Unit (FPU) for single-precision arithmetic and supports DSP-oriented instructions (e.g. single-cycle 16×16 MAC operations, SIMD for 16-bit/8-bit data) to accelerate signal processing tasks. For fast real-time performance, the architecture offers low interrupt latency (with automatic context save on-chip) and features like zero-overhead loops and a programmable peripheral timer unit for scheduling tasks. In total, a high-end TC3xx (such as the TC39x) provides six 300 MHz cores plus four checker cores, yielding a theoretical processing throughput of up to ~2400 DMIPS (Dhrystone MIPS) when all cores are utilized. This represents roughly a threefold increase in computational power over the previous AURIX™ generation (which achieved ~740 DMIPS with three cores).&amp;lt;ref name=&amp;quot;ref19484776952&amp;quot;&amp;gt;32-bit TriCore™ AURIX™– TC3xx - Infineon Technologies https://www.infineon.com/cms/en/product/microcontroller/32-bit-tricore-microcontroller/32-bit-tricore-aurix-tc3xx/&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref2022357806&amp;quot;&amp;gt;UDE Debug, Trace and Test solutions for Infineon TriCore™ AURIX™ TC32, TC33, TC35, TC36, TC37, TC38, TC39, TC3x Microcontrollers       https://www.pls-mc.com/products/infineon-tricore-aurix-tc32-tc33-tc35-tc36-tc37-tc38-tc39-microcontrollers/&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref14281090103&amp;quot;&amp;gt;AURIX™ microcontroller TC3xx family of Infineon fuels automated driving and electromobility - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2016/INFATV201610-005.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Memory Subsystem and Cache===&lt;br /&gt;
AURIX™ TC3xx microcontrollers integrate a large on-chip memory system to meet real-time and safety requirements. Program flash memory sizes range up to 16 MB on the largest models, with over 6 MB of on-chip SRAM available for data and scratchpad usage. Each TriCore™ CPU has its own tightly-coupled local memories as well as caches: for example, each core includes a 32 KB instruction cache and a 16 KB data cache. In addition, every core has separate local RAM blocks (Harvard architecture) for instructions and data (often termed PSPR and DSPR – Program Scratch-Pad RAM and Data Scratch-Pad RAM) that allow deterministic access for time-critical routines. Shared global memory is provided via a Local Memory Unit (LMU) which can be up to several hundred kilobytes (for instance, 768 KB in the TC39x) accessible by all cores. The memory system is connected by a high-bandwidth internal bus fabric (the SRI crossbar and peripheral buses) that arbitrates access between cores, DMA engines, and peripherals. This architecture ensures that each core can quickly fetch instructions and data either from its local cache/RAM or the central memory, enabling efficient parallel processing. Memory protection hardware is also built-in to isolate tasks of different criticality, an important feature for safety and security.&amp;lt;ref name=&amp;quot;ref19484776952&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref5727814512&amp;quot;&amp;gt;Safety Joins Performance: Infineon Introduces Automotive Multicore 32-bit Microcontroller Family AURIX™ to Meet Safety and Powertrain Requirements of Upcoming Vehicle Generations - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2012/INFATV201205-040.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Peripherals and Interfaces===&lt;br /&gt;
One of the strengths of the AURIX™ TC3xx architecture is its extensive set of on-chip peripherals and interface controllers, designed to handle the complex networking and I/O needs of modern vehicles and industrial systems. Key peripheral features and interfaces include:&amp;lt;ref name=&amp;quot;ref19484776952&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref2022357806&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref14281090103&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref3635926&amp;quot;&amp;gt;AURIX™ Training System Architecture https://www.infineon.com/dgdl/Infineon-AURIX_TC3xx_System_Architecture-Training-v01_00-EN.pdf?fileId=5546d46272e49d2a0172eb476d56739e&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Networking Interfaces:&#039;&#039;&#039; Multiple CAN FD controllers (supporting up to 12 CAN-FD channels in high-end variants) for in-vehicle networking, with legacy CAN and LIN interfaces (up to 24 LIN channels) for lower-speed device communication. Many TC3xx devices also integrate up to two FlexRay controllers for time-triggered automotive networks (maintaining compatibility with earlier automotive communication standards). For high-bandwidth data, a Gigabit Ethernet MAC is included, enabling uses like automotive Ethernet (e.g., audio-video bridging and IP-based connectivity).&lt;br /&gt;
* &#039;&#039;&#039;Analog and Timers:&#039;&#039;&#039; A suite of analog peripherals including multiple ADC units for sensor interfacing and motor control, and DACs in some models. Sophisticated timer units are on-chip, notably the Generic Timer Module (GTM) and Capture/Compare Units (CCU6), which can generate complex PWM waveforms and handle events for engine control, power inverters, or robotics with minimal CPU intervention. These timers enable precise control of actuators (e.g. fuel injection timing, electric motor commutation) in real time.&lt;br /&gt;
*&#039;&#039;&#039;Serial Interfaces:&#039;&#039;&#039; SPI, I²C, UART/LIN and other serial interfaces for connecting to external sensors, actuator controllers, and memory. The controllers often support multiple instances (several SPI channels, etc.) to accommodate numerous devices. AURIX™ TC3xx also provides an external bus interface (EBU) for parallel memory or FPGA connectivity, and an SD/MMC interface (eMMC) to connect external flash memory for data logging or over-the-air update storage.&lt;br /&gt;
*&#039;&#039;&#039;Dedicated Processing Units:&#039;&#039;&#039; Some AURIX™ TC3xx models include special accelerators, such as a hardware Fast Fourier Transform unit (HW-FFT) and up to two Signal Processing Units (SPUs) optimized for radar signal processing. These enable the microcontroller to handle radar front-end data or other intensive DSP tasks (like Fourier transforms for radar or audio signals) on-chip, which is crucial for advanced driver assistance sensors. There is also a &#039;&#039;Security Module&#039;&#039; (covered in a later section) and a Safety Management Unit that supervise system integrity.&lt;br /&gt;
*&#039;&#039;&#039;Other On-chip Modules:&#039;&#039;&#039; Watchdog timers, error-correction coders for memories (ECC on flash and RAM for reliability), a programmable interrupt system, multiple DMA channels (up to 128 DMA channels) for efficient data movement, and debug interfaces (JTAG and a 2/3-pin DAP) for development. The on-chip debug system supports tracing of program execution (via a built-in Emulation Memory, MCDS) to aid in complex multicore software development.&lt;br /&gt;
&lt;br /&gt;
Overall, the AURIX™TC3xx provides a highly integrated SoC-like platform, allowing a single chip to manage tasks ranging from powertrain control and chassis control to infotainment gateways. This high level of integration reduces the need for separate companion chips and helps to lower system complexity and cost.&lt;br /&gt;
===Performance and Benchmarking===&lt;br /&gt;
Fabricated in a 40 nm embedded flash process&amp;lt;ref name=&amp;quot;ref2022357806&amp;quot; /&amp;gt;, AURIX™ TC3xx processors achieve significant performance gains over prior generations while maintaining automotive-grade power efficiency. With six 32-bit cores at 300 MHz (four of them paired in lockstep), a flagship TC3xx device (e.g., TC39x) can deliver up to approximately 2,400 DMIPS of aggregate computing throughput&amp;lt;ref name=&amp;quot;ref2022357806&amp;quot; /&amp;gt;. In terms of per-core performance, the TriCore™ architecture yields around 1.3–2.0 DMIPS/MHz depending on the instruction mix, meaning each 300 MHz core can approach ~400–600 DMIPS in optimized scenarios&amp;lt;ref name=&amp;quot;ref14281090103&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref1222155140&amp;quot;&amp;gt;Infineon AURIX - Wikipedia https://en.wikipedia.org/wiki/Infineon_AURIX&amp;lt;/ref&amp;gt;. This is on par with or exceeding many contemporary 32-bit automotive MCUs. The integrated DSP extensions and optional accelerators (like the FFT unit) also enable high-speed signal processing, with Infineon citing up to ~1.8 GFLOPS of DSP throughput for certain operations&amp;lt;ref name=&amp;quot;ref1222155140&amp;quot; /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Real-world performance improvements of the TC3xx over its predecessor are notable: for example, compared to the first-generation AURIX™ (which offered up to 740 DMIPS), the TC3xx’s expanded core count and clock speed provide roughly three times the computational power on the same type of workload&amp;lt;ref name=&amp;quot;ref14281090103&amp;quot; /&amp;gt;. This extra headroom allows more functions (such as running multiple control algorithms or sensor fusion tasks in parallel) to be consolidated onto one microcontroller without compromising real-time deadlines&amp;lt;ref name=&amp;quot;ref14281090103&amp;quot; /&amp;gt;. Despite the increase in speed and core count, the TC3xx family was designed with power and thermal constraints in mind, achieving these gains within similar power consumption envelopes as the previous generation by leveraging the smaller 40 nm process and power-saving features like clock gating&amp;lt;ref name=&amp;quot;ref5727814512&amp;quot; /&amp;gt;. The deterministic real-time performance (with features like lockstep and tightly-coupled memory) makes the AURIX™ TC3xx suitable for the most demanding automotive applications, where consistent low latency and high throughput are required simultaneously.&lt;br /&gt;
&lt;br /&gt;
== On-Chip System Connectivity and Bridges ==&lt;br /&gt;
[[File:TC3xx On-Chip System Connectivity.png|thumb|The TC3xx has two &#039;&#039;&#039;SRI Domains&#039;&#039;&#039; (SRI0 and SRI1). CPU0 to CPU3 are connected to SRI0, CPU4/5 are connected to SRI1.|alt=TC3xx On-Chip System Connectivity|423x423px]]&lt;br /&gt;
The AURIX™ TC3xx Platform has three independent on-chip connectivity resources: &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System Resource Interconnect Fabric (SRI Fabric)&#039;&#039;&#039; connects the TriCore™ CPUs, the DMA module, and other high bandwidth requestors to high bandwidth memories and other resources for instruction fetches and data accesses. A key component of the fabric is the SRI crossbar, which connects all the agents in one SRI domain. The SRI crossbar carries the transactions between the SRI Masters and SRI Slaves of the domain. The SRI crossbar supports parallel transactions between different SRI Master and SRI Slave agents. In addition to the parallelism of concurrent requests, it also supports pipelined requests from an SRI Master to a SRI Slave.&lt;br /&gt;
* &#039;&#039;&#039;System Peripheral Bus (SPB)&#039;&#039;&#039; connects the TriCore™ CPUs, the DMA module, and other SPB masters to the medium and low bandwidth peripherals. SPB masters do not directly connect to the SRI Fabric, and will access SRI attached resources via a SFI_F2S Bridge.&lt;br /&gt;
* &#039;&#039;&#039;Back Bone Bus (BBB)&#039;&#039;&#039; connects the TriCore™ CPUs, the DMA module, and SPB masters with ADAS resources. SRI Masters do not directly connect to the BBB, but access BBB attached resources via a SFI_S2F Bridge. SPB masters also do not directly connect to the BBB, but access BBB attached resources via bridging over the SRI Fabric.&lt;br /&gt;
&lt;br /&gt;
=== CPU Resource Access Times ===&lt;br /&gt;
These tables describe the CPU access times to various resources in CPU clock cycles for the AURIX™ TC3xx Platform. In the case of load or fetch accesses, the access times are the minimum number of CPU stall cycles to complete the access. If there is a conflict for the resource accessed, there may be additional stall cycles till the conflicting access completes. &lt;br /&gt;
&lt;br /&gt;
For write access, the access times are the maximum for a sequence of such access (non-conflicting). In many cases for a singleton access, or a short sequence, write buffering reduces the stall effect seen by a CPU, sometimes to 0. However, as with loads and fetches, if there is a conflict for the resource accessed, there may be additional stall cycles till the conflicting access completes.&lt;br /&gt;
&lt;br /&gt;
==== Access latency for global resources ====&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+&lt;br /&gt;
! CPU Access Type !! CPU stall cycles&lt;br /&gt;
!Notes&lt;br /&gt;
|-&lt;br /&gt;
| Data read from System Peripheral Bus (SPB) || &amp;lt;math&amp;gt;\frac{f_\text{CPU}}{f_\text{SPB}} \cdot (4 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |The final number of stall cycles will depend&amp;lt;br&amp;gt;on the real number of WS generated by the target resource.&lt;br /&gt;
|-&lt;br /&gt;
| Data write to System Peripheral Bus (SPB) || &amp;lt;math&amp;gt;\frac{f_\text{CPU}}{f_\text{SPB}} \cdot (4 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Data read from Back Bone Bus (BBB) (TC39x, TC37xED) || &amp;lt;math&amp;gt;9 + \frac{f_\text{CPU}}{f_\text{BBB}} \cdot (5 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |When SFI_S2F is connected to XBar2 (TC39x and TC37xED)&amp;lt;br&amp;gt;there is an additional latency due to access going through an S2S.&lt;br /&gt;
|-&lt;br /&gt;
| Data write to Back Bone Bus (BBB) (TC39x, TC37xED) || &amp;lt;math&amp;gt;5 + \frac{f_\text{CPU}}{f_\text{BBB}} \cdot (4 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Data read from Back Bone Bus (BBB) (TC35x, TC33xED) || &amp;lt;math&amp;gt;6 + \frac{f_\text{CPU}}{f_\text{BBB}} \cdot (5 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
|-&lt;br /&gt;
| Data write to Back Bone Bus (BBB) (TC35x, TC33xED) || &amp;lt;math&amp;gt;3 + \frac{f_\text{CPU}}{f_\text{BBB}} \cdot (4 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;small&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;Module Wait State&#039;&#039;&#039;: The number of wait states for read and for write accesses is &amp;gt;= 1 and depends on the accessed module and its configuration.&lt;br /&gt;
&amp;lt;/small&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== CPU Accesses: Stall cycles for local and SRI resources ====&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+&lt;br /&gt;
! CPU Access Type !! Local CPU !! Local SRI !! Remote SRI Domain&lt;br /&gt;
|-&lt;br /&gt;
| Data read from DSPR || 0 || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data write to DSPR || 0 || 5, {{rev|3|with Pipelining}}|| 5, {{rev|4|with Pipelining}}&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from DSPR || See local SRI column || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data read from DLMU || 0 || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data write to DLMU || 2 || 5, {{rev|3|with Pipelining}}|| 5, 4&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from DLMU || See local SRI column || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data read from PSPR || See local SRI column || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data write to PSPR || See local SRI column || 5, {{rev|3|with Pipelining}}|| 5, {{rev|4|with Pipelining}}&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from PSPR || 0 || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data read from PFlash || 5 + PWS || 10 + PWS || 13 + PWS&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from PFlash (buffer miss) || 2 + PWS || 9 + PWS || 12 + PWS&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from PFlash (buffer hit) || 3 || 6 || 9&lt;br /&gt;
|-&lt;br /&gt;
| Data read from LMU || n.a. || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data write to LMU || n.a. || 5, {{rev|3|with Pipelining}}|| 5, {{rev|4|with Pipelining}}&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from LMU || n.a. || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data read from DFlash || n.a. || 5 + 3*(3 + DCWS) || 8 + 3*(3 + DCWS)&lt;br /&gt;
|-&lt;br /&gt;
| Data read access from EMEM (TC39x, TC37xED) || n.a. || n.a. || 14, {{rev|15|f&amp;lt;sub&amp;gt;BBB&amp;lt;/sub&amp;gt; &amp;lt; f&amp;lt;sub&amp;gt;SRI&amp;lt;/sub&amp;gt;}}&lt;br /&gt;
|-&lt;br /&gt;
| Data write access to EMEM (TC39x, TC37xED) || n.a. || n.a. || 9&lt;br /&gt;
|-&lt;br /&gt;
| Data read access from EMEM (TC35x, TC33xED) || n.a. || 11, {{rev|12|f&amp;lt;sub&amp;gt;BBB&amp;lt;/sub&amp;gt; &amp;lt; f&amp;lt;sub&amp;gt;SRI&amp;lt;/sub&amp;gt;}}|| n.a.&lt;br /&gt;
|-&lt;br /&gt;
| Data write access to EMEM (TC35x, TC33xED) || n.a. || 9 || n.a.&lt;br /&gt;
|-&lt;br /&gt;
| Data read access from DAM || n.a. || 10 || 13&lt;br /&gt;
|-&lt;br /&gt;
| Data write access to DAM || n.a. || 7 || 7&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;small&amp;gt;&#039;&#039;&#039;Remote SRI Domain&#039;&#039;&#039;: Only applies to products with SRI extenders. Additional latency due to access going through an S2S&amp;lt;/small&amp;gt;&lt;br /&gt;
* &amp;lt;small&amp;gt;&#039;&#039;&#039;PWS&#039;&#039;&#039;: Configured PFlash Wait States (Includes cycles for PFlash access cycles only). ECC correction latency is only incurred when the incoming data requires ECC correction.&amp;lt;/small&amp;gt;&lt;br /&gt;
* &amp;lt;small&amp;gt;&#039;&#039;&#039;PWS&#039;&#039;&#039;: Configured PFlash Wait States (Includes cycles for PFlash access cycles only). ECC correction latency is only incurred when the incoming data requires ECC correction.&amp;lt;/small&amp;gt;&lt;br /&gt;
* &amp;lt;small&amp;gt;&#039;&#039;&#039;DCWS&#039;&#039;&#039;: Configured DFlash Corrected Wait States (Includes cycles for DFlash access cycles and ECC correction latency)&amp;lt;/small&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== CPU Subsystem ==&lt;br /&gt;
[[File:TC3xx Processor Core, Local Memory and Connectivity.png|alt=TC3xx Processor Core, Local Memory and Connectivity|thumb|Processor Core, Local Memory and Connectivity]]The Infineon AURIX™ TC3xx features up to 6 processor cores (CPU0 ... CPU5) implementing the [[TriCore Instruction Set Architecture|TC1.6.2 instruction set architecture]]. The following section focuses on the microarchitectural details of the CPU subsystem. For more information about the ISA, please take a look at the [[TriCore Instruction Set Architecture]].&lt;br /&gt;
&lt;br /&gt;
The processor core connects to the following memories and bus interfaces (where implemented):&lt;br /&gt;
*Program Scratch-Pad SRAM (PSPR)&lt;br /&gt;
*Data Scratch-Pad SRAM (DSPR)&lt;br /&gt;
*Program Cache (PCache)&lt;br /&gt;
*Data Cache (DCache)&lt;br /&gt;
*Local Memory Unit (DLMU)&lt;br /&gt;
*Local Pflash bank (LPB)&lt;br /&gt;
*SRI slave interface (x2)&lt;br /&gt;
*SRI master Interface&lt;br /&gt;
*SPB master interface&lt;br /&gt;
&lt;br /&gt;
=== TC1.6.2P Implementation Features ===&lt;br /&gt;
*Most instructions executed in 1 cycle&lt;br /&gt;
*Branch instructions in 1, 2 or 3 cycles (using dynamic branch prediction)&lt;br /&gt;
*Wide memory interface for fast context switch&lt;br /&gt;
* Automatic context save-on-entry and restore-on-exit for: subroutine, interrupt, trap&lt;br /&gt;
*Six memory protection register sets&lt;br /&gt;
*Dual instruction issuing (in parallel into Integer Pipeline and Load/Store Pipeline)&lt;br /&gt;
*Third pipeline for loop instruction only (zero overhead loop)&lt;br /&gt;
*Single precision Floating Point Unit (IEEE-754 Compatible)&lt;br /&gt;
*Dedicated Integer divide unit&lt;br /&gt;
*18 data memory protection ranges, 10 code memory protection ranges arranged in 6 sets&lt;br /&gt;
&lt;br /&gt;
=== Superscalar Architecture ===&lt;br /&gt;
The processor core within the AURIX™ TC3xx family, specifically the TC1.6.2P implementation, employs a superscalar architecture characterized by three parallel pipelines: the Integer Pipeline, the Load/Store Pipeline, and the Loop Pipeline. Superscalar execution is a form of instruction-level parallelism that enables the processor to issue and execute multiple instructions during a single clock cycle. The TriCore™ TC3xx core&#039;s superscalar architecture allows it to issue and execute multiple instructions in parallel. Specifically, it can issue up to two instructions simultaneously to the Integer and Load/Store pipelines. However, there are certain constraints on this dual issuing capability. For example, back-to-back data arithmetic instructions can only be issued in separate cycles, and a load/store instruction can be issued either on its own or paired with a data arithmetic instruction, provided the load/store instruction is the second in the pair. Under ideal conditions, with careful instruction scheduling, this dual-issue capability can lead to a throughput of close to 0.5 clock cycles per instruction, effectively executing two instructions per cycle.&lt;br /&gt;
&lt;br /&gt;
=== Pipeline Stages ===&lt;br /&gt;
The execution of instructions within the TriCore™ TC3xx core is managed through a pipeline consisting of several stages. In one document it is mentioned that the pipeline consists of 6 stages &amp;lt;ref name=&amp;quot;ref3635926&amp;quot; /&amp;gt;, but the exact stages remain unclear. From the architecture the following stages can be inferred:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Fetch Stage&#039;&#039;&#039;: The initial stage in the pipeline is the Fetch stage, where instructions are retrieved from memory. This task is handled by the Instruction Fetch Unit (IFU), which is responsible for fetching instructions from the memory system. To improve efficiency, the IFU employs a pre-fetching mechanism, anticipating the instructions that will be needed and bringing them into the processor before they are actually required. Instructions are fetched through a 64-bit wide Program Memory Interface (PMI), allowing for the retrieval of a significant amount of instruction data in each cycle. The fetched instructions are then placed into an issue FIFO, a buffer capable of holding up to six instructions. This buffer plays a crucial role in managing the flow of instructions to the subsequent execution pipelines. The 64-bit fetch width and the instruction buffer are indicative of a design that aims to ensure a continuous supply of instructions to the pipelines, preventing them from being starved of work. A wider fetch path allows for more instruction bits to be obtained from memory in each clock cycle, while the buffer helps to mitigate temporary delays in instruction fetching.&lt;br /&gt;
* &#039;&#039;&#039;Execution Unit&#039;&#039;&#039;: The Execution Unit contains the Integer Pipeline, the Load/Store Pipeline and the Loop Pipeline. All three pipelines operate in parallel and are capable of executing instructions simultaneously. Each pipline has its own decode stage and two execute stages. Furthermore, the TC3xx features a Floating Point Unit (FPU). It is not clear whether the FPU is part of the execution unit or a separate unit. It is assumed that the FPU also has its own decode stage and an unknown number of execute stages&lt;br /&gt;
** &#039;&#039;&#039;Decode Stage&#039;&#039;&#039;: Following the Fetch stage, the instruction are issued to one of the pipelines. In the decode stage of each pipeline, the instruction is examined and translated to determine the specific operation it represents and to identify the operands it will operate on. It is assumed that operands are read from the register file during the decode stage.&lt;br /&gt;
** &#039;&#039;&#039;Execute Stage&#039;&#039;&#039;: The Execute stage is where the actual operation specified by the instruction is carried out. Pipeline hazards (stalls) are minimised by the use of forwarding paths between pipeline stages allowing the results of one instruction to be used by a following instruction as soon as the result becomes available.&lt;br /&gt;
** &#039;&#039;&#039;Writeback Stage&#039;&#039;&#039;: The final stage in the pipeline is the Writeback stage. In this stage, the results produced by the executed instruction are written back to the processor&#039;s register file, making them available for subsequent instructions. It is unclear whether the writeback stage is part of the (two) execution stages or a separate stage.&lt;br /&gt;
&lt;br /&gt;
=== Execution Pipelines ===&lt;br /&gt;
The TriCore™ TC3xx core leverages four parallel pipelines to enhance its performance. Each pipeline is designed to handle a specific class of instructions:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Integer Pipeline:&#039;&#039;&#039; This pipeline is responsible for executing integer arithmetic and logical instructions, including data-conditional jump instructions, bit manipulation operations, as well as division and multiply-accumulate (MAC) instructions.&lt;br /&gt;
* &#039;&#039;&#039;Load/Store Pipeline:&#039;&#039;&#039; The primary function of this pipeline is to manage memory access operations, including loading data from memory and storing data to memory. It also handles address arithmetic, unconditional jump instructions, procedure call instructions, context-switching operations, and control flow related to context save areas (CSAs).&lt;br /&gt;
* &#039;&#039;&#039;Loop Pipeline:&#039;&#039;&#039; This is a specialized pipeline designed to facilitate zero-overhead loops, a technique that significantly improves the performance of iterative code sections. Its design aims to eliminate or minimize the overhead typically associated with loop control, such as incrementing counters and checking loop conditions. Some documentation suggests that the loop pipeline comes along with a &amp;quot;Loop Cache&amp;quot;. This dedicated cache would likely be used to store instructions belonging to the loop, enabling very fast access and execution for repeated iterations.&lt;br /&gt;
* &#039;&#039;&#039;Floating Point Unit (FPU):&#039;&#039;&#039; The FPU is responsible for executing floating-point arithmetic operations&lt;br /&gt;
&lt;br /&gt;
=== Store Buffers ===&lt;br /&gt;
The TriCore™ TC3xx core include &#039;&#039;&#039;Store Buffers&#039;&#039;&#039; to decouple memory write operations from CPU instruction execution. All stores from the Load/Store Pipeline are placed in the store buffer prior to being written to local memory or transferred via the bus system. Write data is taken from the store buffers and written to memory when the target memory or bus interface becomes available. In normal operation the CPU will prioritise memory load operations over store operations in order to improve performance unless the store buffer is full or the order of load and store operations must be preserved (e.g. peripheral space access). The store buffer can hold the data for up to 6 stores operations. To improve performance the store buffer will merge consecutive byte, half-word, and word writes of the same location to reduce the number of memory accesses required.&lt;br /&gt;
&lt;br /&gt;
=== Instruction Timing ===&lt;br /&gt;
&lt;br /&gt;
==Functional Safety and Security Features==&lt;br /&gt;
===Hardware Security Module and Encryption===&lt;br /&gt;
One of the defining features of the AURIX™ TC3xx family is its robust hardware-based security subsystem, implemented to protect against cyber threats and unauthorized manipulation of vehicle systems. Each TC3xx MCU includes a dedicated &#039;&#039;Hardware Security Module&#039;&#039; (HSM), which is essentially an on-chip co-processor with its own CPU and cryptographic engine designed to handle secure operations. In the second-generation AURIX™ (TC3xx), the HSM was upgraded with support for asymmetric cryptography (e.g., RSA, ECC algorithms) in hardware, providing significantly faster public-key encryption and authentication capabilities than pure software implementations. This HSM supports &#039;&#039;&#039;EVITA Full&#039;&#039;&#039; (the highest level of the EU’s EVITA automotive security requirements), meaning it can perform secure boot, message authentication, and encryption at a level suitable for protecting critical in-vehicle communications. For example, the HSM can encrypt and authenticate CAN/Ethernet messages to prevent spoofing or tampering on the vehicle network, and it can manage cryptographic keys securely isolated from the main application cores.&amp;lt;ref name=&amp;quot;ref14281090104&amp;quot;&amp;gt;AURIX™ microcontroller TC3xx family of Infineon fuels automated driving and electromobility - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2016/INFATV201610-005.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref2022357806&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The security module enables features like &#039;&#039;&#039;secure boot&#039;&#039;&#039;, where the AURIX™ will only execute firmware that is digitally signed by the vehicle manufacturer, thereby preventing unauthorized or malicious code. It also facilitates &#039;&#039;over-the-air (OTA) updates&#039;&#039; in a secure manner: the TC3xx HSM can verify and decrypt update packages received via telematics, allowing firmware upgrades while protecting against attackers attempting to hijack the software. In addition, the HSM includes a true random number generator and supports protocols for immobilizer and theft protection systems. Infineon’s implementation essentially treats the HSM as an “embedded smart card” inside the microcontroller. This approach, building on Infineon’s expertise in chip card security, helps to safeguard sensitive assets like cryptographic keys and to perform secure operations (encryption, decryption, authentication) in hardware, making attacks significantly more difficult. The strong cybersecurity provisions of the AURIX™ TC3xx have made it a preferred choice for vehicle gateway ECUs and central body controllers, which are points of entry for connected car communications and thus high-value targets for hackers. By incorporating the HSM, automotive OEMs can implement advanced security features such as encrypted onboard communication (to thwart bus tampering) and secure diagnostics, ensuring the vehicle’s electronic data is protected against cyber threats even as cars become more connected.&amp;lt;ref name=&amp;quot;ref14281090104&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref5727814513&amp;quot;&amp;gt;Safety Joins Performance: Infineon Introduces Automotive Multicore 32-bit Microcontroller Family AURIX™ to Meet Safety and Powertrain Requirements of Upcoming Vehicle Generations - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2012/INFATV201205-040.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref19484776953&amp;quot;&amp;gt;32-bit TriCore™ AURIX™– TC3xx - Infineon Technologies https://www.infineon.com/cms/en/product/microcontroller/32-bit-tricore-microcontroller/32-bit-tricore-aurix-tc3xx/&amp;lt;/ref&amp;gt;&lt;br /&gt;
=== Functional Safety and Fault Tolerance===&lt;br /&gt;
Beyond cybersecurity, the AURIX™ TC3xx architecture is built with extensive &#039;&#039;functional safety&#039;&#039; features to meet the highest safety integrity levels in automotive systems. The multi-core design supports &#039;&#039;lockstep operation&#039;&#039;, where redundant checker cores run in parallel with main cores and continuously compare results cycle-by-cycle. In TC3xx devices, up to four of the six TriCore™ CPUs have such lockstep partner cores, allowing the microcontroller to detect any single-point CPU failure instantly (if a discrepancy arises between the core and its checker, a fault is signaled). A &#039;&#039;Safety Management Unit (SMU)&#039;&#039; is integrated on-chip to monitor various fault conditions throughout the device – it aggregates error signals from the lockstep cores, memory ECC monitors, clock/watchdog monitors, and peripheral self-tests, and can initiate safe-state actions (like resetting the chip or signaling an error pin to an external supervisor) if a critical fault is detected. The AURIX™ safety concept also includes a distributed Memory Protection Unit (MPU) arrangement, with multiple memory protection contexts that prevent errant or malicious code from writing to wrong memory areas. This helps contain faults and eases the integration of software with mixed criticality (Infineon allows partitioning software components of different ASIL levels on the same MCU while keeping them isolated).&amp;lt;ref name=&amp;quot;ref14281090104&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref5727814513&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref19519075552&amp;quot;&amp;gt;Safety Microcontrollers: Texas Instruments Hercules vs Infineon AURIX https://www.linkedin.com/pulse/safety-microcontrollers-texas-instruments-hercules-vs-cook-meng-miet&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Crucially, the TC3xx family was developed according to ISO 26262 processes and can achieve &#039;&#039;&#039;ASIL-D&#039;&#039;&#039; (Automotive Safety Integrity Level D), the highest grade for automotive functional safety. Compared to a traditional dual-core lockstep approach, the AURIX’s™ multi-core safety architecture allowed Infineon to reduce certain safety development overheads (earlier Infineon data suggested up to 30% reduction in safety software workload) by providing built-in safety mechanisms and diagnostics. Infineon supplies a comprehensive safety manual and diagnostic libraries for the AURIX™, helping engineers utilize features like CPU self-tests, RAM tests, and CRC checks to comply with safety standards. In addition to automotive standards, the AURIX™ TC3xx has also been qualified for &#039;&#039;&#039;industrial safety&#039;&#039;&#039;: in 2023, Infineon announced support for IEC 61508 metrics, enabling designs with AURIX™ to reach SIL-2 and SIL-3 safety integrity levels for industrial applications. This cross-industry safety compliance is facilitated by the MCU’s redundant design and detailed failure mode documentation (FMEDA) provided by Infineon. In summary, the AURIX™ TC3xx integrates both proactive safety (fault prevention via lockstep, monitoring and protection) and reactive safety (fault detection and safe shutdown), making it a dependable component for systems that cannot afford unintended behavior, whether in cars, factories, or aerospace. &amp;lt;ref name=&amp;quot;ref14281090104&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref5727814513&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref4967712052&amp;quot;&amp;gt;Infineon&#039;s AURIX™ &amp;amp; TRAVEO™ microcontroller families extend their support for IEC 61508 hardware and software metrics enabling industrial safety up to SIL-3 - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2023/INFATV202303-078.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref196858272&amp;quot;&amp;gt;MCUs support industrial functional safety - Electronic Products https://www.electronicproducts.com/mcus-support-industrial-functional-safety/&amp;lt;/ref&amp;gt;&lt;br /&gt;
== Platform Devices ==&lt;br /&gt;
The following table shows a feature overview of the AURIX™ TC3xx Platform family focusing on memory and number of cores.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!&lt;br /&gt;
!Feature&lt;br /&gt;
!TC33x&lt;br /&gt;
!TC33xEXT&lt;br /&gt;
!TC35x&lt;br /&gt;
!TC36x&lt;br /&gt;
!TC37x&lt;br /&gt;
!TC37xEXT&lt;br /&gt;
!TC38x&lt;br /&gt;
!TC39x&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |CPUs&lt;br /&gt;
|Cores / Checker Cores&lt;br /&gt;
|1 / 1&lt;br /&gt;
|2 / 1&lt;br /&gt;
|3 / 2&lt;br /&gt;
|2 / 2&lt;br /&gt;
|3 / 2&lt;br /&gt;
|3 / 3&lt;br /&gt;
|4 / 2&lt;br /&gt;
|6 / 4&lt;br /&gt;
|-&lt;br /&gt;
|Max. Freq.&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; |300 MHz&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |Cache per CPU &lt;br /&gt;
|Program [KB]&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; |32&lt;br /&gt;
|-&lt;br /&gt;
|Data [KB]&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; |16&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; |SRAM per CPU&lt;br /&gt;
|PSPR [KB]&lt;br /&gt;
|8&lt;br /&gt;
|{{rev|32|CPU0}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|64&lt;br /&gt;
|32&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|DSPR [KB]&lt;br /&gt;
|192&lt;br /&gt;
|{{rev|192|CPU0}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|{{rev|240|CPU0&amp;amp;1}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|192&lt;br /&gt;
|{{rev|240|CPU0&amp;amp;1}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|{{rev|240|CPU0&amp;amp;1}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|{{rev|240|CPU0&amp;amp;1}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|{{rev|240|CPU0&amp;amp;1}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|-&lt;br /&gt;
|DLMU [KB]&lt;br /&gt;
|8&lt;br /&gt;
|{{rev|8|CPU0}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |SRAM global&lt;br /&gt;
|LMU [KB]&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|512&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|128&lt;br /&gt;
|768&lt;br /&gt;
|-&lt;br /&gt;
|DAM [KB]&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|32&lt;br /&gt;
| -&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; |Extension Memory (EMEM)&lt;br /&gt;
|TCM [MB]&lt;br /&gt;
| -&lt;br /&gt;
|1&lt;br /&gt;
|2&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|2&lt;br /&gt;
| -&lt;br /&gt;
|2&lt;br /&gt;
|-&lt;br /&gt;
|XCM [MB]&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|1&lt;br /&gt;
| -&lt;br /&gt;
|2&lt;br /&gt;
|-&lt;br /&gt;
|XTM [KB]&lt;br /&gt;
| -&lt;br /&gt;
|16&lt;br /&gt;
|16&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|16&lt;br /&gt;
| -&lt;br /&gt;
|16&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |Program Flash&lt;br /&gt;
|Size [MB]&lt;br /&gt;
|2&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|6&lt;br /&gt;
|10&lt;br /&gt;
|10&lt;br /&gt;
|16&lt;br /&gt;
|-&lt;br /&gt;
|Banks [MB]&lt;br /&gt;
|1 x 2&lt;br /&gt;
|2 x 2&lt;br /&gt;
|2 x 2&lt;br /&gt;
|2 x 2&lt;br /&gt;
|2 x 3&lt;br /&gt;
|3 x 3, 1 x 1&lt;br /&gt;
|3 x 3, 1 x 1&lt;br /&gt;
|5 x 3, 1 x 1&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |Data Flash&lt;br /&gt;
|DF0 Size (single-ended) [KB]&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|256&lt;br /&gt;
|512&lt;br /&gt;
|512&lt;br /&gt;
|1024&lt;br /&gt;
|-&lt;br /&gt;
|DF1 Size (single-ended) [KB]&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; |DMA&lt;br /&gt;
|Channels&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|-&lt;br /&gt;
|Move Engines&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|-&lt;br /&gt;
|Resource Partitions&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Compilers ==&lt;br /&gt;
The AURIX™ TC3xx family is a series of high-performance microcontrollers widely used in automotive and industrial applications. Compilers for the AURIX™ TC3xx are crucial for developers aiming to optimize performance, reliability, and safety in their applications. One significant aspect of the compiler landscape for AURIX™ TC3xx is the limited direct support from mainline open-source compilers such as GCC or LLVM/Clang. The primary reason for this is the stringent requirements for functional safety in automotive and industrial applications, which demand specialized features and compliance with safety standards that are often not met by general-purpose open-source compilers.&lt;br /&gt;
&lt;br /&gt;
=== Commercial Compilers ===&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Tasking&#039;&#039;&#039;: Tasking compilers are renowned for their robust support for automotive applications, offering advanced debugging capabilities and optimization techniques tailored for the AURIX™ architecture. They provide extensive code optimization, comprehensive debugging tools, and strong support for safety standards such as ISO 26262, making them ideal for developing high-performance, reliable, and safe applications.&lt;br /&gt;
# &#039;&#039;&#039;HighTec&#039;&#039;&#039;: The HighTec compiler is a popular choice, known for its Eclipse-based development environment and strong multicore support. HighTec provides both GCC and LLVM-based ports of open-source compilers tailored specifically for the AURIX™ TC3xx family. These compilers offer efficient parallel execution, advanced code analysis, and an integrated development environment, ensuring robust performance and compliance with safety standards.&lt;br /&gt;
# &#039;&#039;&#039;Green Hills Software&#039;&#039;&#039;: Green Hills Software provides a highly optimized toolchain aimed at safety-critical applications, focusing on high performance and strict compliance with automotive standards. Their compiler offers superior optimization, extensive safety features, and a proprietary IDE with specialized tools for automotive development, ensuring developers can meet the stringent demands of functional safety.&lt;br /&gt;
&lt;br /&gt;
=== GCC for AURIX™ ===&lt;br /&gt;
&lt;br /&gt;
While mainline GCC does not directly support the AURIX™ TC3xx family, there is an unofficial GCC version available for AURIX™. Due to the GNU General Public License (GPL), the source code from HighTec was retrieved and, along with binary versions, published on GitHub:&lt;br /&gt;
&lt;br /&gt;
*Infineon&#039;s release of GCC for Aurix with binaries for both Linux and Windows [https://softwaretools.infineon.com/tools/com.ifx.tb.tool.aurixgcc]&lt;br /&gt;
*GCC 4.9.4/Binutils 2.20/Newlib 1.18 for Tricore™ AURIX™ [[https://github.com/volumit/package_494 Source]] [[https://github.com/volumit/tricore_gcc494_mingw_bins Prebuild Mingw Binaries]] [[https://github.com/volumit/tricore_gcc494_linux_bins Prebuild Linux Binaries]]&lt;br /&gt;
*GCC 9.4.0/Binutils 2.20/Newlib 1.18 for Tricore™ AURIX™ [[https://github.com/volumit/package_940 Source]] [[https://github.com/volumit/tricore_gcc940_mingw_bins Prebuild Mingw Binaries]] [[https://github.com/volumit/tricore_gcc940_linux_bins Prebuild Linux Binaries]]&lt;br /&gt;
*EEESLab&#039;s repositories of GCC, Binutils and Newlib for Tricore™ AURIX™ [[https://github.com/EEESlab/tricore-gcc-toolchain-11.3.0 Source]]&lt;br /&gt;
*NoMore201&#039;s fork of EEESLab&#039;s GCC for Tricore™ AURIX™ including GCC, Binutils, Newlib, GDB and QEmu [[https://github.com/NoMore201/tricore-gcc-toolchain Source]] [[https://github.com/NoMore201/tricore-gcc-toolchain/releases/tag/11.3.1-20250101 Sources and prebuilt binaries of gcc-11.3.1 for Linux and Windows]] [[https://github.com/NoMore201/tricore-gcc-toolchain/releases/tag/13.3-20250516 Sources and prebuilt binaries of gcc-13.3.0 for Linux and Windows]]&lt;br /&gt;
*GDB 10.0.50 for Tricore™ AURIX™ [[https://github.com/volumit/gdb-tricore Source]]&lt;br /&gt;
==emmtrix Tools for AURIX™ TC3xx==&lt;br /&gt;
[[File:lowres-Signet Associated Partner EN.eps.png|thumb|305x305px|emmtrix Technologies is an Infineon Associated Partner with over 10 years of experience working with the &#039;&#039;&#039;Infineon AURIX™ microcontroller family&#039;&#039;&#039; and has been actively collaborating with Infineon for the past five years.|alt=emmtrix Associated Partner]]&lt;br /&gt;
emmtrix offers the following tool for the Infineon AURIX™ TC3xx architecture:&lt;br /&gt;
=== emmtrix Performance Estimator ===&lt;br /&gt;
[[emmtrix Performance Estimator|emmtrix Performance Estimator (ePE)]] provides static timing analysis of C code. Compared to simulation or measurement on hardware, static performance analysis can be applied significantly earlier in the development process and will deliver results on average 6 months earlier compared to a typical automotive HIL setup. The analysis only takes a few minutes at most and runs on the developer’s PC independently of any target hardware. Function developers can analyze their runnables or SWCs without the need of a fully integrated program. emmtrix Performance Estimator is fully compatible with &#039;&#039;&#039;Infineon&#039;s AURIX™ [[Infineon AURIX TC2xx|TC2xx]] / TC3xx/ [[Infineon AURIX TC4x|TC4x]] microcontroller family,&#039;&#039;&#039; ensuring precise and reliable performance analysis for embedded systems. &lt;br /&gt;
&lt;br /&gt;
A unique feature is the combination with TargetLink or Embedded Coder generated code. Without any measurement overhead, our static performance estimation can analyze even the smallest code snippets. This allows us to map the timing analysis to Simulink blocks, giving function developers insight into the timing behavior of their models. &lt;br /&gt;
&lt;br /&gt;
ePE offers three accuracy levels: &lt;br /&gt;
&lt;br /&gt;
* analysis of C code&lt;br /&gt;
* generically compiler-optimized code&lt;br /&gt;
* assembly code from the target compiler.&lt;br /&gt;
&lt;br /&gt;
Method 1 yields results with minimum effort while method 3 takes the timing of the processor pipeline into account. All methods offer excellent reliability when tracking the tendency of changes in software runtimes e.g. when used in a continuous integration environment.&lt;br /&gt;
&lt;br /&gt;
=== Services ===&lt;br /&gt;
====Architecture &amp;amp; Variant Consulting====&lt;br /&gt;
* Support in selecting suitable AURIX derivatives for new product generations (trade-offs between performance, price, safety, and peripheral features)&lt;br /&gt;
*Analysis of requirements and mapping to specific AURIX product families (e.g., TC3xx vs. TC4xx)&lt;br /&gt;
*Analysis/identification of performance margins for down- or upgrades&lt;br /&gt;
==== Runtime Analysis &amp;amp; Performance Engineering ====&lt;br /&gt;
* Execution of detailed runtime analyses (maximum/minimum vs. average)&lt;br /&gt;
* Identification of bottlenecks in program execution, including benchmarking&lt;br /&gt;
*Optimization of access to flash, RAM, and peripherals (e.g., cache configuration, DMA utilization)&lt;br /&gt;
====Dependency Analysis====&lt;br /&gt;
*Freedom from interference (ASIL level check)&lt;br /&gt;
&lt;br /&gt;
==== Multicore Design &amp;amp; Scheduling Concepts ====&lt;br /&gt;
&lt;br /&gt;
* Development of core mapping strategies (static vs. dynamic assignment of compute-intensive tasks)&lt;br /&gt;
&lt;br /&gt;
*Support for integrating bare-metal scheduling on AURIX&lt;br /&gt;
*Assistance with architectural decisions to meet functional safety requirements in multicore systems&lt;br /&gt;
&lt;br /&gt;
==== Training &amp;amp; Know-how Transfer ====&lt;br /&gt;
&lt;br /&gt;
* Technical in-house workshops on AURIX optimization, multicore strategies&lt;br /&gt;
&lt;br /&gt;
*Coaching of employees/project teams in performance analysis and systematic tuning&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Infineon AURIX TC4x|Infineon AURIX™ TC4x]]&lt;br /&gt;
*[[Infineon AURIX TC2xx|Infineon AURIX™ TC2xx]]&lt;br /&gt;
*[[TriCore Instruction Set Architecture]]&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
*https://www.infineon.com/cms/en/product/microcontroller/32-bit-tricore-microcontroller/32-bit-tricore-aurix-tc3xx/&lt;br /&gt;
*[https://documentation.infineon.com/aurixtc3xx/docs/qmd1702366622648 AURIX™ TC3xx user manuals]&lt;br /&gt;
**[https://www.infineon.com/dgdl/Infineon-AURIX_TC3xx_Part1-UserManual-v02_00-EN.pdf?fileId=5546d462712ef9b701717d3605221d96 AURIX™ TC3xx User Manual Part-1]&lt;br /&gt;
**[https://www.infineon.com/dgdl/Infineon-AURIX_TC3xx_Part2-UserManual-v02_00-EN.pdf?fileId=5546d462712ef9b701717d35f8541d94 AURIX™ TC3xx User Manual Part-2]&lt;br /&gt;
**[https://www.infineon.com/dgdl/Infineon-AURIX_TC3xx_Architecture_vol1-UserManual-v01_00-EN.pdf?fileId=5546d46276fb756a01771bc4c2e33bdd AURIX™ TC3xx Architecture vol 1]&lt;br /&gt;
**[https://www.infineon.com/dgdl/Infineon-AURIX_TC3xx_Architecture_vol2-UserManual-v01_00-EN.pdf?fileId=5546d46276fb756a01771bc4a6d73b70 AURIX™ TC3xx Architecture vol 2]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
[[Category:Supported Architectures]]&lt;br /&gt;
&lt;br /&gt;
{{#seo:&lt;br /&gt;
 |title=Infineon AURIX TC3xx Family – Deep Dive&lt;br /&gt;
 |description=This deep dive into Infineon&#039;s AURIX TC3xx family explores its multi-core TriCore architecture, real-time capabilities, safety and security features and tools&lt;br /&gt;
 |image=File:AURIX_TC399_LFBGA-516_plain.jpg&lt;br /&gt;
 |keywords=AURIX, Infineon, TC3xx, microcontroller, TriCore, automotive, ADAS, ISO 26262, functional safety, embedded security, ASIL-D, deep dive&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Infineon_AURIX_TC3xx&amp;diff=2757</id>
		<title>Infineon AURIX TC3xx</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Infineon_AURIX_TC3xx&amp;diff=2757"/>
		<updated>2025-07-03T09:37:34Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: more toolchains&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;[[File:AURIX TC399 LFBGA-516 plain.jpg|thumb|Infineon AURIX™ TC3xx second-generation microcontroller (TC399 BGA package). The AURIX™ TC3xx family integrates multiple TriCore™ CPUs and on-chip safety/security modules for automotive applications.|alt=TC399 BGA package]]&lt;br /&gt;
The &#039;&#039;&#039;Infineon AURIX™ TC3xx&#039;&#039;&#039; (Automotive Realtime Integrated neXt-generation architecture) is a family of 32-bit automotive microcontrollers introduced as the second generation of Infineon’s AURIX™ platform. Launched in 2016 as a successor to the earlier [[Infineon AURIX TC2xx|AURIX™ TC2xx series]], the TC3xx devices emphasize high performance, functional safety, and embedded security for advanced automotive and industrial applications. AURIX™ TC3xx microcontrollers feature a hexa-core [[TriCore Instruction Set Architecture|&#039;&#039;TriCore&#039;&#039; processor architecture]] with enhanced safety measures, making them well-suited for demanding tasks such as autonomous driving domain controllers, powertrain control, and sensor data fusion in advanced driver-assistance systems (ADAS). The combination of multiple CPU cores, extensive on-chip memory, and a rich peripheral set allows TC3xx chips to deliver real-time responsiveness and reliability in safety-critical environments while meeting stringent automotive standards (such as ISO 26262 for functional safety).&amp;lt;ref name=&amp;quot;ref1951907555&amp;quot;&amp;gt;Safety Microcontrollers: Texas Instruments Hercules vs Infineon AURIX https://www.linkedin.com/pulse/safety-microcontrollers-texas-instruments-hercules-vs-cook-meng-miet&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref1428109010&amp;quot;&amp;gt;AURIX™ microcontroller TC3xx family of Infineon fuels automated driving and electromobility - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2016/INFATV201610-005.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref1948477695&amp;quot;&amp;gt;32-bit TriCore™ AURIX™– TC3xx - Infineon Technologies https://www.infineon.com/cms/en/product/microcontroller/32-bit-tricore-microcontroller/32-bit-tricore-aurix-tc3xx/&amp;lt;/ref&amp;gt;&lt;br /&gt;
==Applications==&lt;br /&gt;
AURIX™ TC3xx microcontrollers are used in a variety of safety-critical and high-performance applications across the automotive and industrial sectors. Key application domains include:&lt;br /&gt;
*&#039;&#039;&#039;Automotive Systems:&#039;&#039;&#039; The TC3xx family was primarily designed for automotive use and finds broad application in vehicles’ electronic control units (ECUs). Notably, it is used in &#039;&#039;powertrain and drivetrain control&#039;&#039; (engine management, transmission control, hybrid/electric vehicle inverter control, battery management) where its real-time processing and resilience support precise actuator control. It is also deployed in &#039;&#039;chassis and safety systems&#039;&#039; such as anti-lock braking systems (ABS), electronic stability control, airbag controllers, and electric power steering, leveraging its redundancy and fast response for fail-safe operation. With the growth of ADAS and autonomous driving, AURIX™ TC3xx devices serve as domain controllers for &#039;&#039;ADAS sensor fusion&#039;&#039;, handling data from radar, LiDAR, camera, and ultrasonic sensors and performing environment modeling and decision-making algorithms. Their combination of multiple cores and a built-in radar signal processing sub-system makes them an attractive choice for tasks like front radar processing, sensor fusion hubs, and &#039;&#039;autonomous driving domain controllers&#039;&#039; that must meet ASIL-D safety levels.&amp;lt;ref name=&amp;quot;ref14281090102&amp;quot;&amp;gt;AURIX™ microcontroller TC3xx family of Infineon fuels automated driving and electromobility - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2016/INFATV201610-005.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref572781451&amp;quot;&amp;gt;Safety Joins Performance: Infineon Introduces Automotive Multicore 32-bit Microcontroller Family AURIX™ to Meet Safety and Powertrain Requirements of Upcoming Vehicle Generations - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2012/INFATV201205-040.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
*&#039;&#039;&#039;Industrial Automation and Robotics:&#039;&#039;&#039; Infineon has also positioned the AURIX™ family for use in industrial applications that demand high reliability. The TC3xx MCUs, with their hardware self-test mechanisms and safety documentation, can be found in &#039;&#039;industrial motor drives, robotics controllers, and PLCs (programmable logic controllers)&#039;&#039;, where they help achieve functional safety certifications up to SIL-3 (according to IEC 61508). The powerful TriCore™ cores and rich peripheral set enable complex motor control algorithms (e.g., field-oriented control for servo motors) and real-time sensor processing in factory automation. Additionally, the support for automotive-grade networking (CAN/CAN-FD, Ethernet) and timing peripherals makes them suitable for synchronous control in robotics and machinery. Infineon’s provision of AUTOSAR-compatible software for AURIX™ has even facilitated its use in non-automotive settings such as medical devices and elevator controllers, where proven safety and security are required.&amp;lt;ref name=&amp;quot;ref496771205&amp;quot;&amp;gt;Infineon&#039;s AURIX™ &amp;amp; TRAVEO™ microcontroller families extend their support for IEC 61508 hardware and software metrics enabling industrial safety up to SIL-3 - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2023/INFATV202303-078.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
*&#039;&#039;&#039;Aerospace and Defense:&#039;&#039;&#039; The high fault-tolerance and deterministic performance of the AURIX™ TC3xx architecture also attract interest for aerospace and other mission-critical uses. In avionics systems or autonomous drones, for instance, the lockstep core configuration and robust error checking of AURIX™ can provide an added layer of reliability for flight control computers or navigation systems. While primarily an automotive chip, its ability to operate in harsh conditions (−40°C to 150°C range) and to detect internal failures in real-time makes it viable for certain aerospace electronic systems. Some defense and aerospace projects leverage COTS (commercial off-the-shelf) automotive components like AURIX™ for applications such as unmanned vehicle control and high-reliability communication links, given its safety pedigree. (In these domains, additional analysis and possibly radiation testing would be performed if the devices are used in high-altitude or space environments.)&lt;br /&gt;
== Architecture Overview ==&lt;br /&gt;
[[File:Infineon TC3xx Architecture.webp|545x545px|thumb|alt=TC3xx Architecture Overview|TC3xx Architecture Overview]]&lt;br /&gt;
=== Processor Cores and Architecture===&lt;br /&gt;
The AURIX™ TC3xx family implements Infineon’s TriCore™ architecture, which uniquely combines aspects of RISC microcontrollers, DSP (digital signal processor) capabilities, and conventional microprocessor features into a single core design. Each TC3xx device can include up to six independent TriCore™ 32-bit CPU cores (TriCore™ version 1.6.2), all capable of running at up to 300 MHz clock frequency. Four of these cores can operate in dual-core lockstep pairs (with one core acting as a redundant checker for its partner) to provide fault detection for safety-critical functions. The TriCore™ cores use a superscalar Harvard architecture with 32-bit instruction words (and support for mixed 16/32-bit instruction encoding for code density). Each core includes a dedicated Floating Point Unit (FPU) for single-precision arithmetic and supports DSP-oriented instructions (e.g. single-cycle 16×16 MAC operations, SIMD for 16-bit/8-bit data) to accelerate signal processing tasks. For fast real-time performance, the architecture offers low interrupt latency (with automatic context save on-chip) and features like zero-overhead loops and a programmable peripheral timer unit for scheduling tasks. In total, a high-end TC3xx (such as the TC39x) provides six 300 MHz cores plus four checker cores, yielding a theoretical processing throughput of up to ~2400 DMIPS (Dhrystone MIPS) when all cores are utilized. This represents roughly a threefold increase in computational power over the previous AURIX™ generation (which achieved ~740 DMIPS with three cores).&amp;lt;ref name=&amp;quot;ref19484776952&amp;quot;&amp;gt;32-bit TriCore™ AURIX™– TC3xx - Infineon Technologies https://www.infineon.com/cms/en/product/microcontroller/32-bit-tricore-microcontroller/32-bit-tricore-aurix-tc3xx/&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref2022357806&amp;quot;&amp;gt;UDE Debug, Trace and Test solutions for Infineon TriCore™ AURIX™ TC32, TC33, TC35, TC36, TC37, TC38, TC39, TC3x Microcontrollers       https://www.pls-mc.com/products/infineon-tricore-aurix-tc32-tc33-tc35-tc36-tc37-tc38-tc39-microcontrollers/&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref14281090103&amp;quot;&amp;gt;AURIX™ microcontroller TC3xx family of Infineon fuels automated driving and electromobility - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2016/INFATV201610-005.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Memory Subsystem and Cache===&lt;br /&gt;
AURIX™ TC3xx microcontrollers integrate a large on-chip memory system to meet real-time and safety requirements. Program flash memory sizes range up to 16 MB on the largest models, with over 6 MB of on-chip SRAM available for data and scratchpad usage. Each TriCore™ CPU has its own tightly-coupled local memories as well as caches: for example, each core includes a 32 KB instruction cache and a 16 KB data cache. In addition, every core has separate local RAM blocks (Harvard architecture) for instructions and data (often termed PSPR and DSPR – Program Scratch-Pad RAM and Data Scratch-Pad RAM) that allow deterministic access for time-critical routines. Shared global memory is provided via a Local Memory Unit (LMU) which can be up to several hundred kilobytes (for instance, 768 KB in the TC39x) accessible by all cores. The memory system is connected by a high-bandwidth internal bus fabric (the SRI crossbar and peripheral buses) that arbitrates access between cores, DMA engines, and peripherals. This architecture ensures that each core can quickly fetch instructions and data either from its local cache/RAM or the central memory, enabling efficient parallel processing. Memory protection hardware is also built-in to isolate tasks of different criticality, an important feature for safety and security.&amp;lt;ref name=&amp;quot;ref19484776952&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref5727814512&amp;quot;&amp;gt;Safety Joins Performance: Infineon Introduces Automotive Multicore 32-bit Microcontroller Family AURIX™ to Meet Safety and Powertrain Requirements of Upcoming Vehicle Generations - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2012/INFATV201205-040.html&amp;lt;/ref&amp;gt;&lt;br /&gt;
===Peripherals and Interfaces===&lt;br /&gt;
One of the strengths of the AURIX™ TC3xx architecture is its extensive set of on-chip peripherals and interface controllers, designed to handle the complex networking and I/O needs of modern vehicles and industrial systems. Key peripheral features and interfaces include:&amp;lt;ref name=&amp;quot;ref19484776952&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref2022357806&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref14281090103&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref3635926&amp;quot;&amp;gt;AURIX™ Training System Architecture https://www.infineon.com/dgdl/Infineon-AURIX_TC3xx_System_Architecture-Training-v01_00-EN.pdf?fileId=5546d46272e49d2a0172eb476d56739e&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Networking Interfaces:&#039;&#039;&#039; Multiple CAN FD controllers (supporting up to 12 CAN-FD channels in high-end variants) for in-vehicle networking, with legacy CAN and LIN interfaces (up to 24 LIN channels) for lower-speed device communication. Many TC3xx devices also integrate up to two FlexRay controllers for time-triggered automotive networks (maintaining compatibility with earlier automotive communication standards). For high-bandwidth data, a Gigabit Ethernet MAC is included, enabling uses like automotive Ethernet (e.g., audio-video bridging and IP-based connectivity).&lt;br /&gt;
* &#039;&#039;&#039;Analog and Timers:&#039;&#039;&#039; A suite of analog peripherals including multiple ADC units for sensor interfacing and motor control, and DACs in some models. Sophisticated timer units are on-chip, notably the Generic Timer Module (GTM) and Capture/Compare Units (CCU6), which can generate complex PWM waveforms and handle events for engine control, power inverters, or robotics with minimal CPU intervention. These timers enable precise control of actuators (e.g. fuel injection timing, electric motor commutation) in real time.&lt;br /&gt;
*&#039;&#039;&#039;Serial Interfaces:&#039;&#039;&#039; SPI, I²C, UART/LIN and other serial interfaces for connecting to external sensors, actuator controllers, and memory. The controllers often support multiple instances (several SPI channels, etc.) to accommodate numerous devices. AURIX™ TC3xx also provides an external bus interface (EBU) for parallel memory or FPGA connectivity, and an SD/MMC interface (eMMC) to connect external flash memory for data logging or over-the-air update storage.&lt;br /&gt;
*&#039;&#039;&#039;Dedicated Processing Units:&#039;&#039;&#039; Some AURIX™ TC3xx models include special accelerators, such as a hardware Fast Fourier Transform unit (HW-FFT) and up to two Signal Processing Units (SPUs) optimized for radar signal processing. These enable the microcontroller to handle radar front-end data or other intensive DSP tasks (like Fourier transforms for radar or audio signals) on-chip, which is crucial for advanced driver assistance sensors. There is also a &#039;&#039;Security Module&#039;&#039; (covered in a later section) and a Safety Management Unit that supervise system integrity.&lt;br /&gt;
*&#039;&#039;&#039;Other On-chip Modules:&#039;&#039;&#039; Watchdog timers, error-correction coders for memories (ECC on flash and RAM for reliability), a programmable interrupt system, multiple DMA channels (up to 128 DMA channels) for efficient data movement, and debug interfaces (JTAG and a 2/3-pin DAP) for development. The on-chip debug system supports tracing of program execution (via a built-in Emulation Memory, MCDS) to aid in complex multicore software development.&lt;br /&gt;
&lt;br /&gt;
Overall, the AURIX™TC3xx provides a highly integrated SoC-like platform, allowing a single chip to manage tasks ranging from powertrain control and chassis control to infotainment gateways. This high level of integration reduces the need for separate companion chips and helps to lower system complexity and cost.&lt;br /&gt;
===Performance and Benchmarking===&lt;br /&gt;
Fabricated in a 40 nm embedded flash process&amp;lt;ref name=&amp;quot;ref2022357806&amp;quot; /&amp;gt;, AURIX™ TC3xx processors achieve significant performance gains over prior generations while maintaining automotive-grade power efficiency. With six 32-bit cores at 300 MHz (four of them paired in lockstep), a flagship TC3xx device (e.g., TC39x) can deliver up to approximately 2,400 DMIPS of aggregate computing throughput&amp;lt;ref name=&amp;quot;ref2022357806&amp;quot; /&amp;gt;. In terms of per-core performance, the TriCore™ architecture yields around 1.3–2.0 DMIPS/MHz depending on the instruction mix, meaning each 300 MHz core can approach ~400–600 DMIPS in optimized scenarios&amp;lt;ref name=&amp;quot;ref14281090103&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref1222155140&amp;quot;&amp;gt;Infineon AURIX - Wikipedia https://en.wikipedia.org/wiki/Infineon_AURIX&amp;lt;/ref&amp;gt;. This is on par with or exceeding many contemporary 32-bit automotive MCUs. The integrated DSP extensions and optional accelerators (like the FFT unit) also enable high-speed signal processing, with Infineon citing up to ~1.8 GFLOPS of DSP throughput for certain operations&amp;lt;ref name=&amp;quot;ref1222155140&amp;quot; /&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Real-world performance improvements of the TC3xx over its predecessor are notable: for example, compared to the first-generation AURIX™ (which offered up to 740 DMIPS), the TC3xx’s expanded core count and clock speed provide roughly three times the computational power on the same type of workload&amp;lt;ref name=&amp;quot;ref14281090103&amp;quot; /&amp;gt;. This extra headroom allows more functions (such as running multiple control algorithms or sensor fusion tasks in parallel) to be consolidated onto one microcontroller without compromising real-time deadlines&amp;lt;ref name=&amp;quot;ref14281090103&amp;quot; /&amp;gt;. Despite the increase in speed and core count, the TC3xx family was designed with power and thermal constraints in mind, achieving these gains within similar power consumption envelopes as the previous generation by leveraging the smaller 40 nm process and power-saving features like clock gating&amp;lt;ref name=&amp;quot;ref5727814512&amp;quot; /&amp;gt;. The deterministic real-time performance (with features like lockstep and tightly-coupled memory) makes the AURIX™ TC3xx suitable for the most demanding automotive applications, where consistent low latency and high throughput are required simultaneously.&lt;br /&gt;
&lt;br /&gt;
== On-Chip System Connectivity and Bridges ==&lt;br /&gt;
[[File:TC3xx On-Chip System Connectivity.png|thumb|The TC3xx has two &#039;&#039;&#039;SRI Domains&#039;&#039;&#039; (SRI0 and SRI1). CPU0 to CPU3 are connected to SRI0, CPU4/5 are connected to SRI1.|alt=TC3xx On-Chip System Connectivity|423x423px]]&lt;br /&gt;
The AURIX™ TC3xx Platform has three independent on-chip connectivity resources: &lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;System Resource Interconnect Fabric (SRI Fabric)&#039;&#039;&#039; connects the TriCore™ CPUs, the DMA module, and other high bandwidth requestors to high bandwidth memories and other resources for instruction fetches and data accesses. A key component of the fabric is the SRI crossbar, which connects all the agents in one SRI domain. The SRI crossbar carries the transactions between the SRI Masters and SRI Slaves of the domain. The SRI crossbar supports parallel transactions between different SRI Master and SRI Slave agents. In addition to the parallelism of concurrent requests, it also supports pipelined requests from an SRI Master to a SRI Slave.&lt;br /&gt;
* &#039;&#039;&#039;System Peripheral Bus (SPB)&#039;&#039;&#039; connects the TriCore™ CPUs, the DMA module, and other SPB masters to the medium and low bandwidth peripherals. SPB masters do not directly connect to the SRI Fabric, and will access SRI attached resources via a SFI_F2S Bridge.&lt;br /&gt;
* &#039;&#039;&#039;Back Bone Bus (BBB)&#039;&#039;&#039; connects the TriCore™ CPUs, the DMA module, and SPB masters with ADAS resources. SRI Masters do not directly connect to the BBB, but access BBB attached resources via a SFI_S2F Bridge. SPB masters also do not directly connect to the BBB, but access BBB attached resources via bridging over the SRI Fabric.&lt;br /&gt;
&lt;br /&gt;
=== CPU Resource Access Times ===&lt;br /&gt;
These tables describe the CPU access times to various resources in CPU clock cycles for the AURIX™ TC3xx Platform. In the case of load or fetch accesses, the access times are the minimum number of CPU stall cycles to complete the access. If there is a conflict for the resource accessed, there may be additional stall cycles till the conflicting access completes. &lt;br /&gt;
&lt;br /&gt;
For write access, the access times are the maximum for a sequence of such access (non-conflicting). In many cases for a singleton access, or a short sequence, write buffering reduces the stall effect seen by a CPU, sometimes to 0. However, as with loads and fetches, if there is a conflict for the resource accessed, there may be additional stall cycles till the conflicting access completes.&lt;br /&gt;
&lt;br /&gt;
==== Access latency for global resources ====&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+&lt;br /&gt;
! CPU Access Type !! CPU stall cycles&lt;br /&gt;
!Notes&lt;br /&gt;
|-&lt;br /&gt;
| Data read from System Peripheral Bus (SPB) || &amp;lt;math&amp;gt;\frac{f_\text{CPU}}{f_\text{SPB}} \cdot (4 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |The final number of stall cycles will depend&amp;lt;br&amp;gt;on the real number of WS generated by the target resource.&lt;br /&gt;
|-&lt;br /&gt;
| Data write to System Peripheral Bus (SPB) || &amp;lt;math&amp;gt;\frac{f_\text{CPU}}{f_\text{SPB}} \cdot (4 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Data read from Back Bone Bus (BBB) (TC39x, TC37xED) || &amp;lt;math&amp;gt;9 + \frac{f_\text{CPU}}{f_\text{BBB}} \cdot (5 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |When SFI_S2F is connected to XBar2 (TC39x and TC37xED)&amp;lt;br&amp;gt;there is an additional latency due to access going through an S2S.&lt;br /&gt;
|-&lt;br /&gt;
| Data write to Back Bone Bus (BBB) (TC39x, TC37xED) || &amp;lt;math&amp;gt;5 + \frac{f_\text{CPU}}{f_\text{BBB}} \cdot (4 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| Data read from Back Bone Bus (BBB) (TC35x, TC33xED) || &amp;lt;math&amp;gt;6 + \frac{f_\text{CPU}}{f_\text{BBB}} \cdot (5 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
|-&lt;br /&gt;
| Data write to Back Bone Bus (BBB) (TC35x, TC33xED) || &amp;lt;math&amp;gt;3 + \frac{f_\text{CPU}}{f_\text{BBB}} \cdot (4 + \text{Module Wait State})&amp;lt;/math&amp;gt;&lt;br /&gt;
|&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;small&amp;gt;&lt;br /&gt;
* &#039;&#039;&#039;Module Wait State&#039;&#039;&#039;: The number of wait states for read and for write accesses is &amp;gt;= 1 and depends on the accessed module and its configuration.&lt;br /&gt;
&amp;lt;/small&amp;gt;&lt;br /&gt;
&lt;br /&gt;
==== CPU Accesses: Stall cycles for local and SRI resources ====&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+&lt;br /&gt;
! CPU Access Type !! Local CPU !! Local SRI !! Remote SRI Domain&lt;br /&gt;
|-&lt;br /&gt;
| Data read from DSPR || 0 || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data write to DSPR || 0 || 5, {{rev|3|with Pipelining}}|| 5, {{rev|4|with Pipelining}}&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from DSPR || See local SRI column || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data read from DLMU || 0 || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data write to DLMU || 2 || 5, {{rev|3|with Pipelining}}|| 5, 4&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from DLMU || See local SRI column || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data read from PSPR || See local SRI column || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data write to PSPR || See local SRI column || 5, {{rev|3|with Pipelining}}|| 5, {{rev|4|with Pipelining}}&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from PSPR || 0 || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data read from PFlash || 5 + PWS || 10 + PWS || 13 + PWS&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from PFlash (buffer miss) || 2 + PWS || 9 + PWS || 12 + PWS&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from PFlash (buffer hit) || 3 || 6 || 9&lt;br /&gt;
|-&lt;br /&gt;
| Data read from LMU || n.a. || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data write to LMU || n.a. || 5, {{rev|3|with Pipelining}}|| 5, {{rev|4|with Pipelining}}&lt;br /&gt;
|-&lt;br /&gt;
| Instruction fetch from LMU || n.a. || 7 || 10&lt;br /&gt;
|-&lt;br /&gt;
| Data read from DFlash || n.a. || 5 + 3*(3 + DCWS) || 8 + 3*(3 + DCWS)&lt;br /&gt;
|-&lt;br /&gt;
| Data read access from EMEM (TC39x, TC37xED) || n.a. || n.a. || 14, {{rev|15|f&amp;lt;sub&amp;gt;BBB&amp;lt;/sub&amp;gt; &amp;lt; f&amp;lt;sub&amp;gt;SRI&amp;lt;/sub&amp;gt;}}&lt;br /&gt;
|-&lt;br /&gt;
| Data write access to EMEM (TC39x, TC37xED) || n.a. || n.a. || 9&lt;br /&gt;
|-&lt;br /&gt;
| Data read access from EMEM (TC35x, TC33xED) || n.a. || 11, {{rev|12|f&amp;lt;sub&amp;gt;BBB&amp;lt;/sub&amp;gt; &amp;lt; f&amp;lt;sub&amp;gt;SRI&amp;lt;/sub&amp;gt;}}|| n.a.&lt;br /&gt;
|-&lt;br /&gt;
| Data write access to EMEM (TC35x, TC33xED) || n.a. || 9 || n.a.&lt;br /&gt;
|-&lt;br /&gt;
| Data read access from DAM || n.a. || 10 || 13&lt;br /&gt;
|-&lt;br /&gt;
| Data write access to DAM || n.a. || 7 || 7&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
* &amp;lt;small&amp;gt;&#039;&#039;&#039;Remote SRI Domain&#039;&#039;&#039;: Only applies to products with SRI extenders. Additional latency due to access going through an S2S&amp;lt;/small&amp;gt;&lt;br /&gt;
* &amp;lt;small&amp;gt;&#039;&#039;&#039;PWS&#039;&#039;&#039;: Configured PFlash Wait States (Includes cycles for PFlash access cycles only). ECC correction latency is only incurred when the incoming data requires ECC correction.&amp;lt;/small&amp;gt;&lt;br /&gt;
* &amp;lt;small&amp;gt;&#039;&#039;&#039;PWS&#039;&#039;&#039;: Configured PFlash Wait States (Includes cycles for PFlash access cycles only). ECC correction latency is only incurred when the incoming data requires ECC correction.&amp;lt;/small&amp;gt;&lt;br /&gt;
* &amp;lt;small&amp;gt;&#039;&#039;&#039;DCWS&#039;&#039;&#039;: Configured DFlash Corrected Wait States (Includes cycles for DFlash access cycles and ECC correction latency)&amp;lt;/small&amp;gt;&lt;br /&gt;
&lt;br /&gt;
== CPU Subsystem ==&lt;br /&gt;
[[File:TC3xx Processor Core, Local Memory and Connectivity.png|alt=TC3xx Processor Core, Local Memory and Connectivity|thumb|Processor Core, Local Memory and Connectivity]]The Infineon AURIX™ TC3xx features up to 6 processor cores (CPU0 ... CPU5) implementing the [[TriCore Instruction Set Architecture|TC1.6.2 instruction set architecture]]. The following section focuses on the microarchitectural details of the CPU subsystem. For more information about the ISA, please take a look at the [[TriCore Instruction Set Architecture]].&lt;br /&gt;
&lt;br /&gt;
The processor core connects to the following memories and bus interfaces (where implemented):&lt;br /&gt;
*Program Scratch-Pad SRAM (PSPR)&lt;br /&gt;
*Data Scratch-Pad SRAM (DSPR)&lt;br /&gt;
*Program Cache (PCache)&lt;br /&gt;
*Data Cache (DCache)&lt;br /&gt;
*Local Memory Unit (DLMU)&lt;br /&gt;
*Local Pflash bank (LPB)&lt;br /&gt;
*SRI slave interface (x2)&lt;br /&gt;
*SRI master Interface&lt;br /&gt;
*SPB master interface&lt;br /&gt;
&lt;br /&gt;
=== TC1.6.2P Implementation Features ===&lt;br /&gt;
*Most instructions executed in 1 cycle&lt;br /&gt;
*Branch instructions in 1, 2 or 3 cycles (using dynamic branch prediction)&lt;br /&gt;
*Wide memory interface for fast context switch&lt;br /&gt;
* Automatic context save-on-entry and restore-on-exit for: subroutine, interrupt, trap&lt;br /&gt;
*Six memory protection register sets&lt;br /&gt;
*Dual instruction issuing (in parallel into Integer Pipeline and Load/Store Pipeline)&lt;br /&gt;
*Third pipeline for loop instruction only (zero overhead loop)&lt;br /&gt;
*Single precision Floating Point Unit (IEEE-754 Compatible)&lt;br /&gt;
*Dedicated Integer divide unit&lt;br /&gt;
*18 data memory protection ranges, 10 code memory protection ranges arranged in 6 sets&lt;br /&gt;
&lt;br /&gt;
=== Superscalar Architecture ===&lt;br /&gt;
The processor core within the AURIX™ TC3xx family, specifically the TC1.6.2P implementation, employs a superscalar architecture characterized by three parallel pipelines: the Integer Pipeline, the Load/Store Pipeline, and the Loop Pipeline. Superscalar execution is a form of instruction-level parallelism that enables the processor to issue and execute multiple instructions during a single clock cycle. The TriCore™ TC3xx core&#039;s superscalar architecture allows it to issue and execute multiple instructions in parallel. Specifically, it can issue up to two instructions simultaneously to the Integer and Load/Store pipelines. However, there are certain constraints on this dual issuing capability. For example, back-to-back data arithmetic instructions can only be issued in separate cycles, and a load/store instruction can be issued either on its own or paired with a data arithmetic instruction, provided the load/store instruction is the second in the pair. Under ideal conditions, with careful instruction scheduling, this dual-issue capability can lead to a throughput of close to 0.5 clock cycles per instruction, effectively executing two instructions per cycle.&lt;br /&gt;
&lt;br /&gt;
=== Pipeline Stages ===&lt;br /&gt;
The execution of instructions within the TriCore™ TC3xx core is managed through a pipeline consisting of several stages. In one document it is mentioned that the pipeline consists of 6 stages &amp;lt;ref name=&amp;quot;ref3635926&amp;quot; /&amp;gt;, but the exact stages remain unclear. From the architecture the following stages can be inferred:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Fetch Stage&#039;&#039;&#039;: The initial stage in the pipeline is the Fetch stage, where instructions are retrieved from memory. This task is handled by the Instruction Fetch Unit (IFU), which is responsible for fetching instructions from the memory system. To improve efficiency, the IFU employs a pre-fetching mechanism, anticipating the instructions that will be needed and bringing them into the processor before they are actually required. Instructions are fetched through a 64-bit wide Program Memory Interface (PMI), allowing for the retrieval of a significant amount of instruction data in each cycle. The fetched instructions are then placed into an issue FIFO, a buffer capable of holding up to six instructions. This buffer plays a crucial role in managing the flow of instructions to the subsequent execution pipelines. The 64-bit fetch width and the instruction buffer are indicative of a design that aims to ensure a continuous supply of instructions to the pipelines, preventing them from being starved of work. A wider fetch path allows for more instruction bits to be obtained from memory in each clock cycle, while the buffer helps to mitigate temporary delays in instruction fetching.&lt;br /&gt;
* &#039;&#039;&#039;Execution Unit&#039;&#039;&#039;: The Execution Unit contains the Integer Pipeline, the Load/Store Pipeline and the Loop Pipeline. All three pipelines operate in parallel and are capable of executing instructions simultaneously. Each pipline has its own decode stage and two execute stages. Furthermore, the TC3xx features a Floating Point Unit (FPU). It is not clear whether the FPU is part of the execution unit or a separate unit. It is assumed that the FPU also has its own decode stage and an unknown number of execute stages&lt;br /&gt;
** &#039;&#039;&#039;Decode Stage&#039;&#039;&#039;: Following the Fetch stage, the instruction are issued to one of the pipelines. In the decode stage of each pipeline, the instruction is examined and translated to determine the specific operation it represents and to identify the operands it will operate on. It is assumed that operands are read from the register file during the decode stage.&lt;br /&gt;
** &#039;&#039;&#039;Execute Stage&#039;&#039;&#039;: The Execute stage is where the actual operation specified by the instruction is carried out. Pipeline hazards (stalls) are minimised by the use of forwarding paths between pipeline stages allowing the results of one instruction to be used by a following instruction as soon as the result becomes available.&lt;br /&gt;
** &#039;&#039;&#039;Writeback Stage&#039;&#039;&#039;: The final stage in the pipeline is the Writeback stage. In this stage, the results produced by the executed instruction are written back to the processor&#039;s register file, making them available for subsequent instructions. It is unclear whether the writeback stage is part of the (two) execution stages or a separate stage.&lt;br /&gt;
&lt;br /&gt;
=== Execution Pipelines ===&lt;br /&gt;
The TriCore™ TC3xx core leverages four parallel pipelines to enhance its performance. Each pipeline is designed to handle a specific class of instructions:&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Integer Pipeline:&#039;&#039;&#039; This pipeline is responsible for executing integer arithmetic and logical instructions, including data-conditional jump instructions, bit manipulation operations, as well as division and multiply-accumulate (MAC) instructions.&lt;br /&gt;
* &#039;&#039;&#039;Load/Store Pipeline:&#039;&#039;&#039; The primary function of this pipeline is to manage memory access operations, including loading data from memory and storing data to memory. It also handles address arithmetic, unconditional jump instructions, procedure call instructions, context-switching operations, and control flow related to context save areas (CSAs).&lt;br /&gt;
* &#039;&#039;&#039;Loop Pipeline:&#039;&#039;&#039; This is a specialized pipeline designed to facilitate zero-overhead loops, a technique that significantly improves the performance of iterative code sections. Its design aims to eliminate or minimize the overhead typically associated with loop control, such as incrementing counters and checking loop conditions. Some documentation suggests that the loop pipeline comes along with a &amp;quot;Loop Cache&amp;quot;. This dedicated cache would likely be used to store instructions belonging to the loop, enabling very fast access and execution for repeated iterations.&lt;br /&gt;
* &#039;&#039;&#039;Floating Point Unit (FPU):&#039;&#039;&#039; The FPU is responsible for executing floating-point arithmetic operations&lt;br /&gt;
&lt;br /&gt;
=== Store Buffers ===&lt;br /&gt;
The TriCore™ TC3xx core include &#039;&#039;&#039;Store Buffers&#039;&#039;&#039; to decouple memory write operations from CPU instruction execution. All stores from the Load/Store Pipeline are placed in the store buffer prior to being written to local memory or transferred via the bus system. Write data is taken from the store buffers and written to memory when the target memory or bus interface becomes available. In normal operation the CPU will prioritise memory load operations over store operations in order to improve performance unless the store buffer is full or the order of load and store operations must be preserved (e.g. peripheral space access). The store buffer can hold the data for up to 6 stores operations. To improve performance the store buffer will merge consecutive byte, half-word, and word writes of the same location to reduce the number of memory accesses required.&lt;br /&gt;
&lt;br /&gt;
=== Instruction Timing ===&lt;br /&gt;
&lt;br /&gt;
==Functional Safety and Security Features==&lt;br /&gt;
===Hardware Security Module and Encryption===&lt;br /&gt;
One of the defining features of the AURIX™ TC3xx family is its robust hardware-based security subsystem, implemented to protect against cyber threats and unauthorized manipulation of vehicle systems. Each TC3xx MCU includes a dedicated &#039;&#039;Hardware Security Module&#039;&#039; (HSM), which is essentially an on-chip co-processor with its own CPU and cryptographic engine designed to handle secure operations. In the second-generation AURIX™ (TC3xx), the HSM was upgraded with support for asymmetric cryptography (e.g., RSA, ECC algorithms) in hardware, providing significantly faster public-key encryption and authentication capabilities than pure software implementations. This HSM supports &#039;&#039;&#039;EVITA Full&#039;&#039;&#039; (the highest level of the EU’s EVITA automotive security requirements), meaning it can perform secure boot, message authentication, and encryption at a level suitable for protecting critical in-vehicle communications. For example, the HSM can encrypt and authenticate CAN/Ethernet messages to prevent spoofing or tampering on the vehicle network, and it can manage cryptographic keys securely isolated from the main application cores.&amp;lt;ref name=&amp;quot;ref14281090104&amp;quot;&amp;gt;AURIX™ microcontroller TC3xx family of Infineon fuels automated driving and electromobility - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2016/INFATV201610-005.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref2022357806&amp;quot; /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
The security module enables features like &#039;&#039;&#039;secure boot&#039;&#039;&#039;, where the AURIX™ will only execute firmware that is digitally signed by the vehicle manufacturer, thereby preventing unauthorized or malicious code. It also facilitates &#039;&#039;over-the-air (OTA) updates&#039;&#039; in a secure manner: the TC3xx HSM can verify and decrypt update packages received via telematics, allowing firmware upgrades while protecting against attackers attempting to hijack the software. In addition, the HSM includes a true random number generator and supports protocols for immobilizer and theft protection systems. Infineon’s implementation essentially treats the HSM as an “embedded smart card” inside the microcontroller. This approach, building on Infineon’s expertise in chip card security, helps to safeguard sensitive assets like cryptographic keys and to perform secure operations (encryption, decryption, authentication) in hardware, making attacks significantly more difficult. The strong cybersecurity provisions of the AURIX™ TC3xx have made it a preferred choice for vehicle gateway ECUs and central body controllers, which are points of entry for connected car communications and thus high-value targets for hackers. By incorporating the HSM, automotive OEMs can implement advanced security features such as encrypted onboard communication (to thwart bus tampering) and secure diagnostics, ensuring the vehicle’s electronic data is protected against cyber threats even as cars become more connected.&amp;lt;ref name=&amp;quot;ref14281090104&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref5727814513&amp;quot;&amp;gt;Safety Joins Performance: Infineon Introduces Automotive Multicore 32-bit Microcontroller Family AURIX™ to Meet Safety and Powertrain Requirements of Upcoming Vehicle Generations - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2012/INFATV201205-040.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref19484776953&amp;quot;&amp;gt;32-bit TriCore™ AURIX™– TC3xx - Infineon Technologies https://www.infineon.com/cms/en/product/microcontroller/32-bit-tricore-microcontroller/32-bit-tricore-aurix-tc3xx/&amp;lt;/ref&amp;gt;&lt;br /&gt;
=== Functional Safety and Fault Tolerance===&lt;br /&gt;
Beyond cybersecurity, the AURIX™ TC3xx architecture is built with extensive &#039;&#039;functional safety&#039;&#039; features to meet the highest safety integrity levels in automotive systems. The multi-core design supports &#039;&#039;lockstep operation&#039;&#039;, where redundant checker cores run in parallel with main cores and continuously compare results cycle-by-cycle. In TC3xx devices, up to four of the six TriCore™ CPUs have such lockstep partner cores, allowing the microcontroller to detect any single-point CPU failure instantly (if a discrepancy arises between the core and its checker, a fault is signaled). A &#039;&#039;Safety Management Unit (SMU)&#039;&#039; is integrated on-chip to monitor various fault conditions throughout the device – it aggregates error signals from the lockstep cores, memory ECC monitors, clock/watchdog monitors, and peripheral self-tests, and can initiate safe-state actions (like resetting the chip or signaling an error pin to an external supervisor) if a critical fault is detected. The AURIX™ safety concept also includes a distributed Memory Protection Unit (MPU) arrangement, with multiple memory protection contexts that prevent errant or malicious code from writing to wrong memory areas. This helps contain faults and eases the integration of software with mixed criticality (Infineon allows partitioning software components of different ASIL levels on the same MCU while keeping them isolated).&amp;lt;ref name=&amp;quot;ref14281090104&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref5727814513&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref19519075552&amp;quot;&amp;gt;Safety Microcontrollers: Texas Instruments Hercules vs Infineon AURIX https://www.linkedin.com/pulse/safety-microcontrollers-texas-instruments-hercules-vs-cook-meng-miet&amp;lt;/ref&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Crucially, the TC3xx family was developed according to ISO 26262 processes and can achieve &#039;&#039;&#039;ASIL-D&#039;&#039;&#039; (Automotive Safety Integrity Level D), the highest grade for automotive functional safety. Compared to a traditional dual-core lockstep approach, the AURIX’s™ multi-core safety architecture allowed Infineon to reduce certain safety development overheads (earlier Infineon data suggested up to 30% reduction in safety software workload) by providing built-in safety mechanisms and diagnostics. Infineon supplies a comprehensive safety manual and diagnostic libraries for the AURIX™, helping engineers utilize features like CPU self-tests, RAM tests, and CRC checks to comply with safety standards. In addition to automotive standards, the AURIX™ TC3xx has also been qualified for &#039;&#039;&#039;industrial safety&#039;&#039;&#039;: in 2023, Infineon announced support for IEC 61508 metrics, enabling designs with AURIX™ to reach SIL-2 and SIL-3 safety integrity levels for industrial applications. This cross-industry safety compliance is facilitated by the MCU’s redundant design and detailed failure mode documentation (FMEDA) provided by Infineon. In summary, the AURIX™ TC3xx integrates both proactive safety (fault prevention via lockstep, monitoring and protection) and reactive safety (fault detection and safe shutdown), making it a dependable component for systems that cannot afford unintended behavior, whether in cars, factories, or aerospace. &amp;lt;ref name=&amp;quot;ref14281090104&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref5727814513&amp;quot; /&amp;gt;&amp;lt;ref name=&amp;quot;ref4967712052&amp;quot;&amp;gt;Infineon&#039;s AURIX™ &amp;amp; TRAVEO™ microcontroller families extend their support for IEC 61508 hardware and software metrics enabling industrial safety up to SIL-3 - Infineon Technologies https://www.infineon.com/cms/en/about-infineon/press/market-news/2023/INFATV202303-078.html&amp;lt;/ref&amp;gt;&amp;lt;ref name=&amp;quot;ref196858272&amp;quot;&amp;gt;MCUs support industrial functional safety - Electronic Products https://www.electronicproducts.com/mcus-support-industrial-functional-safety/&amp;lt;/ref&amp;gt;&lt;br /&gt;
== Platform Devices ==&lt;br /&gt;
The following table shows a feature overview of the AURIX™ TC3xx Platform family focusing on memory and number of cores.&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!&lt;br /&gt;
!Feature&lt;br /&gt;
!TC33x&lt;br /&gt;
!TC33xEXT&lt;br /&gt;
!TC35x&lt;br /&gt;
!TC36x&lt;br /&gt;
!TC37x&lt;br /&gt;
!TC37xEXT&lt;br /&gt;
!TC38x&lt;br /&gt;
!TC39x&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |CPUs&lt;br /&gt;
|Cores / Checker Cores&lt;br /&gt;
|1 / 1&lt;br /&gt;
|2 / 1&lt;br /&gt;
|3 / 2&lt;br /&gt;
|2 / 2&lt;br /&gt;
|3 / 2&lt;br /&gt;
|3 / 3&lt;br /&gt;
|4 / 2&lt;br /&gt;
|6 / 4&lt;br /&gt;
|-&lt;br /&gt;
|Max. Freq.&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; |300 MHz&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |Cache per CPU &lt;br /&gt;
|Program [KB]&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; |32&lt;br /&gt;
|-&lt;br /&gt;
|Data [KB]&lt;br /&gt;
| colspan=&amp;quot;8&amp;quot; |16&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; |SRAM per CPU&lt;br /&gt;
|PSPR [KB]&lt;br /&gt;
|8&lt;br /&gt;
|{{rev|32|CPU0}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|64&lt;br /&gt;
|32&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
|DSPR [KB]&lt;br /&gt;
|192&lt;br /&gt;
|{{rev|192|CPU0}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|{{rev|240|CPU0&amp;amp;1}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|192&lt;br /&gt;
|{{rev|240|CPU0&amp;amp;1}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|{{rev|240|CPU0&amp;amp;1}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|{{rev|240|CPU0&amp;amp;1}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|{{rev|240|CPU0&amp;amp;1}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|-&lt;br /&gt;
|DLMU [KB]&lt;br /&gt;
|8&lt;br /&gt;
|{{rev|8|CPU0}}&lt;br /&gt;
{{rev|64|other}}&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |SRAM global&lt;br /&gt;
|LMU [KB]&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|512&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|128&lt;br /&gt;
|768&lt;br /&gt;
|-&lt;br /&gt;
|DAM [KB]&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|32&lt;br /&gt;
| -&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; |Extension Memory (EMEM)&lt;br /&gt;
|TCM [MB]&lt;br /&gt;
| -&lt;br /&gt;
|1&lt;br /&gt;
|2&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|2&lt;br /&gt;
| -&lt;br /&gt;
|2&lt;br /&gt;
|-&lt;br /&gt;
|XCM [MB]&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|1&lt;br /&gt;
| -&lt;br /&gt;
|2&lt;br /&gt;
|-&lt;br /&gt;
|XTM [KB]&lt;br /&gt;
| -&lt;br /&gt;
|16&lt;br /&gt;
|16&lt;br /&gt;
| -&lt;br /&gt;
| -&lt;br /&gt;
|16&lt;br /&gt;
| -&lt;br /&gt;
|16&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |Program Flash&lt;br /&gt;
|Size [MB]&lt;br /&gt;
|2&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|6&lt;br /&gt;
|10&lt;br /&gt;
|10&lt;br /&gt;
|16&lt;br /&gt;
|-&lt;br /&gt;
|Banks [MB]&lt;br /&gt;
|1 x 2&lt;br /&gt;
|2 x 2&lt;br /&gt;
|2 x 2&lt;br /&gt;
|2 x 2&lt;br /&gt;
|2 x 3&lt;br /&gt;
|3 x 3, 1 x 1&lt;br /&gt;
|3 x 3, 1 x 1&lt;br /&gt;
|5 x 3, 1 x 1&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;2&amp;quot; |Data Flash&lt;br /&gt;
|DF0 Size (single-ended) [KB]&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|256&lt;br /&gt;
|512&lt;br /&gt;
|512&lt;br /&gt;
|1024&lt;br /&gt;
|-&lt;br /&gt;
|DF1 Size (single-ended) [KB]&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|-&lt;br /&gt;
| rowspan=&amp;quot;3&amp;quot; |DMA&lt;br /&gt;
|Channels&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|64&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|128&lt;br /&gt;
|-&lt;br /&gt;
|Move Engines&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|2&lt;br /&gt;
|-&lt;br /&gt;
|Resource Partitions&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|4&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Compilers ==&lt;br /&gt;
The AURIX™ TC3xx family is a series of high-performance microcontrollers widely used in automotive and industrial applications. Compilers for the AURIX™ TC3xx are crucial for developers aiming to optimize performance, reliability, and safety in their applications. One significant aspect of the compiler landscape for AURIX™ TC3xx is the limited direct support from mainline open-source compilers such as GCC or LLVM/Clang. The primary reason for this is the stringent requirements for functional safety in automotive and industrial applications, which demand specialized features and compliance with safety standards that are often not met by general-purpose open-source compilers.&lt;br /&gt;
&lt;br /&gt;
=== Commercial Compilers ===&lt;br /&gt;
&lt;br /&gt;
# &#039;&#039;&#039;Tasking&#039;&#039;&#039;: Tasking compilers are renowned for their robust support for automotive applications, offering advanced debugging capabilities and optimization techniques tailored for the AURIX™ architecture. They provide extensive code optimization, comprehensive debugging tools, and strong support for safety standards such as ISO 26262, making them ideal for developing high-performance, reliable, and safe applications.&lt;br /&gt;
# &#039;&#039;&#039;HighTec&#039;&#039;&#039;: The HighTec compiler is a popular choice, known for its Eclipse-based development environment and strong multicore support. HighTec provides both GCC and LLVM-based ports of open-source compilers tailored specifically for the AURIX™ TC3xx family. These compilers offer efficient parallel execution, advanced code analysis, and an integrated development environment, ensuring robust performance and compliance with safety standards.&lt;br /&gt;
# &#039;&#039;&#039;Green Hills Software&#039;&#039;&#039;: Green Hills Software provides a highly optimized toolchain aimed at safety-critical applications, focusing on high performance and strict compliance with automotive standards. Their compiler offers superior optimization, extensive safety features, and a proprietary IDE with specialized tools for automotive development, ensuring developers can meet the stringent demands of functional safety.&lt;br /&gt;
&lt;br /&gt;
=== GCC for AURIX™ ===&lt;br /&gt;
&lt;br /&gt;
While mainline GCC does not directly support the AURIX™ TC3xx family, there is an unofficial GCC version available for AURIX™. Due to the GNU General Public License (GPL), the source code from HighTec was retrieved and, along with binary versions, published on GitHub:&lt;br /&gt;
&lt;br /&gt;
*GCC 4.9.4/Binutils 2.20/Newlib 1.18 for Tricore™ AURIX™ [[https://github.com/volumit/package_494 Source]] [[https://github.com/volumit/tricore_gcc494_mingw_bins Prebuild Mingw Binaries]] [[https://github.com/volumit/tricore_gcc494_linux_bins Prebuild Linux Binaries]]&lt;br /&gt;
*GCC 9.4.0/Binutils 2.20/Newlib 1.18 for Tricore™ AURIX™ [[https://github.com/volumit/package_940 Source]] [[https://github.com/volumit/tricore_gcc940_mingw_bins Prebuild Mingw Binaries]] [[https://github.com/volumit/tricore_gcc940_linux_bins Prebuild Linux Binaries]]&lt;br /&gt;
*EEESLab&#039;s repositories of GCC, Binutils and Newlib for Tricore™ AURIX™ [[https://github.com/EEESlab/tricore-gcc-toolchain-11.3.0 Source]]&lt;br /&gt;
*NoMore201&#039;s fork of EEESLab&#039;s GCC for Tricore™ AURIX™ including GCC, Binutils, Newlib, GDB and QEmu [[https://github.com/NoMore201/tricore-gcc-toolchain Source]] [[https://github.com/NoMore201/tricore-gcc-toolchain/releases/tag/11.3.1-20250101 Sources and prebuilt binaries of gcc-11.3.1 for Linux and Windows]] [[https://github.com/NoMore201/tricore-gcc-toolchain/releases/tag/13.3-20250516 Sources and prebuilt binaries of gcc-13.3.0 for Linux and Windows]]&lt;br /&gt;
*GDB 10.0.50 for Tricore™ AURIX™ [[https://github.com/volumit/gdb-tricore Source]]&lt;br /&gt;
==emmtrix Tools for AURIX™ TC3xx==&lt;br /&gt;
[[File:lowres-Signet Associated Partner EN.eps.png|thumb|305x305px|emmtrix Technologies is an Infineon Associated Partner with over 10 years of experience working with the &#039;&#039;&#039;Infineon AURIX™ microcontroller family&#039;&#039;&#039; and has been actively collaborating with Infineon for the past five years.|alt=emmtrix Associated Partner]]&lt;br /&gt;
emmtrix offers the following tool for the Infineon AURIX™ TC3xx architecture:&lt;br /&gt;
=== emmtrix Performance Estimator ===&lt;br /&gt;
[[emmtrix Performance Estimator|emmtrix Performance Estimator (ePE)]] provides static timing analysis of C code. Compared to simulation or measurement on hardware, static performance analysis can be applied significantly earlier in the development process and will deliver results on average 6 months earlier compared to a typical automotive HIL setup. The analysis only takes a few minutes at most and runs on the developer’s PC independently of any target hardware. Function developers can analyze their runnables or SWCs without the need of a fully integrated program. emmtrix Performance Estimator is fully compatible with &#039;&#039;&#039;Infineon&#039;s AURIX™ [[Infineon AURIX TC2xx|TC2xx]] / TC3xx/ [[Infineon AURIX TC4x|TC4x]] microcontroller family,&#039;&#039;&#039; ensuring precise and reliable performance analysis for embedded systems. &lt;br /&gt;
&lt;br /&gt;
A unique feature is the combination with TargetLink or Embedded Coder generated code. Without any measurement overhead, our static performance estimation can analyze even the smallest code snippets. This allows us to map the timing analysis to Simulink blocks, giving function developers insight into the timing behavior of their models. &lt;br /&gt;
&lt;br /&gt;
ePE offers three accuracy levels: &lt;br /&gt;
&lt;br /&gt;
* analysis of C code&lt;br /&gt;
* generically compiler-optimized code&lt;br /&gt;
* assembly code from the target compiler.&lt;br /&gt;
&lt;br /&gt;
Method 1 yields results with minimum effort while method 3 takes the timing of the processor pipeline into account. All methods offer excellent reliability when tracking the tendency of changes in software runtimes e.g. when used in a continuous integration environment.&lt;br /&gt;
&lt;br /&gt;
=== Services ===&lt;br /&gt;
====Architecture &amp;amp; Variant Consulting====&lt;br /&gt;
* Support in selecting suitable AURIX derivatives for new product generations (trade-offs between performance, price, safety, and peripheral features)&lt;br /&gt;
*Analysis of requirements and mapping to specific AURIX product families (e.g., TC3xx vs. TC4xx)&lt;br /&gt;
*Analysis/identification of performance margins for down- or upgrades&lt;br /&gt;
==== Runtime Analysis &amp;amp; Performance Engineering ====&lt;br /&gt;
* Execution of detailed runtime analyses (maximum/minimum vs. average)&lt;br /&gt;
* Identification of bottlenecks in program execution, including benchmarking&lt;br /&gt;
*Optimization of access to flash, RAM, and peripherals (e.g., cache configuration, DMA utilization)&lt;br /&gt;
====Dependency Analysis====&lt;br /&gt;
*Freedom from interference (ASIL level check)&lt;br /&gt;
&lt;br /&gt;
==== Multicore Design &amp;amp; Scheduling Concepts ====&lt;br /&gt;
&lt;br /&gt;
* Development of core mapping strategies (static vs. dynamic assignment of compute-intensive tasks)&lt;br /&gt;
&lt;br /&gt;
*Support for integrating bare-metal scheduling on AURIX&lt;br /&gt;
*Assistance with architectural decisions to meet functional safety requirements in multicore systems&lt;br /&gt;
&lt;br /&gt;
==== Training &amp;amp; Know-how Transfer ====&lt;br /&gt;
&lt;br /&gt;
* Technical in-house workshops on AURIX optimization, multicore strategies&lt;br /&gt;
&lt;br /&gt;
*Coaching of employees/project teams in performance analysis and systematic tuning&lt;br /&gt;
==See Also==&lt;br /&gt;
&lt;br /&gt;
*[[Infineon AURIX TC4x|Infineon AURIX™ TC4x]]&lt;br /&gt;
*[[Infineon AURIX TC2xx|Infineon AURIX™ TC2xx]]&lt;br /&gt;
*[[TriCore Instruction Set Architecture]]&lt;br /&gt;
&lt;br /&gt;
==External Links==&lt;br /&gt;
&lt;br /&gt;
*https://www.infineon.com/cms/en/product/microcontroller/32-bit-tricore-microcontroller/32-bit-tricore-aurix-tc3xx/&lt;br /&gt;
*[https://documentation.infineon.com/aurixtc3xx/docs/qmd1702366622648 AURIX™ TC3xx user manuals]&lt;br /&gt;
**[https://www.infineon.com/dgdl/Infineon-AURIX_TC3xx_Part1-UserManual-v02_00-EN.pdf?fileId=5546d462712ef9b701717d3605221d96 AURIX™ TC3xx User Manual Part-1]&lt;br /&gt;
**[https://www.infineon.com/dgdl/Infineon-AURIX_TC3xx_Part2-UserManual-v02_00-EN.pdf?fileId=5546d462712ef9b701717d35f8541d94 AURIX™ TC3xx User Manual Part-2]&lt;br /&gt;
**[https://www.infineon.com/dgdl/Infineon-AURIX_TC3xx_Architecture_vol1-UserManual-v01_00-EN.pdf?fileId=5546d46276fb756a01771bc4c2e33bdd AURIX™ TC3xx Architecture vol 1]&lt;br /&gt;
**[https://www.infineon.com/dgdl/Infineon-AURIX_TC3xx_Architecture_vol2-UserManual-v01_00-EN.pdf?fileId=5546d46276fb756a01771bc4a6d73b70 AURIX™ TC3xx Architecture vol 2]&lt;br /&gt;
&lt;br /&gt;
== References ==&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
[[Category:Supported Architectures]]&lt;br /&gt;
&lt;br /&gt;
{{#seo:&lt;br /&gt;
 |title=Infineon AURIX TC3xx Family – Deep Dive&lt;br /&gt;
 |description=This deep dive into Infineon&#039;s AURIX TC3xx family explores its multi-core TriCore architecture, real-time capabilities, safety and security features and tools&lt;br /&gt;
 |image=File:AURIX_TC399_LFBGA-516_plain.jpg&lt;br /&gt;
 |keywords=AURIX, Infineon, TC3xx, microcontroller, TriCore, automotive, ADAS, ISO 26262, functional safety, embedded security, ASIL-D, deep dive&lt;br /&gt;
}}&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Explicit_Cast_Insertion_Transformation&amp;diff=2719</id>
		<title>Explicit Cast Insertion Transformation</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Explicit_Cast_Insertion_Transformation&amp;diff=2719"/>
		<updated>2025-05-26T14:44:17Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{DISPLAYTITLE:Explicit Cast Insertion Transformation}}&lt;br /&gt;
&lt;br /&gt;
Explicit cast insertion is a transformation that introduces explicit type casts in situations where implicit type conversions would otherwise occur. By making type conversions explicit, this technique improves code clarity, prevents unintended behavior, and enhances static code analysis. It is particularly useful in strongly typed languages, ensuring that conversions are intentional and reducing ambiguity in expressions involving mixed data types. While preserving program semantics, explicit cast insertion helps avoid compiler warnings and improves maintainability by making type changes more transparent.&lt;br /&gt;
==Explicit Cast Insertion Transformation in emmtrix Studio==&lt;br /&gt;
emmtrix Studio can implement explicit cast insertion using #pragma directives or via the GUI. Cast insertion is a transformation that inserts explicit type casts in situations where implicit type conversion will take place. The transformation preserves the code-semantics.&lt;br /&gt;
===Typical Usage and Benefits===&lt;br /&gt;
The transformation is used to generate clearer code and to increase capabilities of the code analysis.&lt;br /&gt;
===Example===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
| &amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* The following code tests cast insertion transformation applied to main function.&lt;br /&gt;
 * In the given example, in expressions sum(a, b), if (a) and x ? a : b, there is implicit type conversion. &lt;br /&gt;
 * Explicit type casts are inserted in the relevant places.&lt;br /&gt;
 */   &lt;br /&gt;
&lt;br /&gt;
int sum(int num1, int num2) {&lt;br /&gt;
    return num1 + num2;&lt;br /&gt;
}&lt;br /&gt;
#pragma EMX_TRANSFORMATION ExplicitCastInserter&lt;br /&gt;
int main(void) {&lt;br /&gt;
    char a = 5;&lt;br /&gt;
    char x = &#039;s&#039;;&lt;br /&gt;
    float b = 3.14159;&lt;br /&gt;
    int c;&lt;br /&gt;
    sum(a, b);&lt;br /&gt;
    if (a) {&lt;br /&gt;
        printf(” % c\ n”, a);&lt;br /&gt;
    }&lt;br /&gt;
    c = x ? a : b;&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* The following code is the generated code after the transformation has been applied.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
int sum(int num1, int num2) {&lt;br /&gt;
    return num1 + num2;&lt;br /&gt;
}&lt;br /&gt;
int main() {&lt;br /&gt;
    char a = (char) 5;&lt;br /&gt;
    char x = (char)&lt;br /&gt;
    &#039;s&#039;;&lt;br /&gt;
    float b = (float) 3.14159;&lt;br /&gt;
    int c;&lt;br /&gt;
    sum((int) a, (int) b);&lt;br /&gt;
    if ((_Bool) a) {&lt;br /&gt;
        printf(” % c\ n”, a);&lt;br /&gt;
    }&lt;br /&gt;
    c = (int)((_Bool) x ? (float) a : b);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
[[Category:Code Transformation]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Constant_Propagation_Transformation&amp;diff=2718</id>
		<title>Constant Propagation Transformation</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Constant_Propagation_Transformation&amp;diff=2718"/>
		<updated>2025-05-26T14:42:15Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{DISPLAYTITLE:Constant Propagation Transformation}}&lt;br /&gt;
&lt;br /&gt;
Constant propagation is an optimization technique used in compilers to improve execution efficiency by replacing variables with known constant values during compilation. If an expression consists entirely of constants or previously propagated values, it is evaluated at compile time and replaced with its computed result. This transformation reduces runtime computations, simplifies code, and enables further optimizations by minimizing unnecessary variable assignments. By eliminating redundant calculations, constant propagation enhances program performance while preserving its original behavior.&lt;br /&gt;
==Constant Propagation Transformation in emmtrix Studio==&lt;br /&gt;
emmtrix Studio implements constant propagation using #pragma directives or via the GUI. Constant propagation is a transformation that propagates all known variable values within the block it is applied to. If an expression consists of propagated values and of constants, it will itself be evaluated and replaced with its constant value.&lt;br /&gt;
&lt;br /&gt;
All arithmetic operations for integral types are supported. Interprocedural propagation, i.e. propagating into and out of function calls, is partially supported.&lt;br /&gt;
&lt;br /&gt;
===Typical Usage and Benefits===&lt;br /&gt;
Constant propagation is typically used to generate a clearer code, to reduce complexity for dependency analysis and increase potential for better optimization possibilities.&lt;br /&gt;
===Example===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* The following code tests constant propagation applied to main function. &lt;br /&gt;
 * In the given example, variables a, b and c are known. &lt;br /&gt;
 * They are propagated to the rest of the body of main.&lt;br /&gt;
 */   &lt;br /&gt;
&lt;br /&gt;
#pragma EMX_TRANSFORMATION ConstPropagation&lt;br /&gt;
int main(void) {&lt;br /&gt;
    int a = 2;&lt;br /&gt;
    int b = a;&lt;br /&gt;
    &lt;br /&gt;
    int c, d;&lt;br /&gt;
    c = 2 + 2;&lt;br /&gt;
    d = b + c;&lt;br /&gt;
    &lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* The following code is the generated code after the transformation has been applied.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
    int a = 2;&lt;br /&gt;
    int b = a;&lt;br /&gt;
    int c;&lt;br /&gt;
    int d;&lt;br /&gt;
    &lt;br /&gt;
    c = 4;&lt;br /&gt;
    d = 6;&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Parameters ===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+&lt;br /&gt;
!Id&lt;br /&gt;
!Possible Values&lt;br /&gt;
!Default Value&lt;br /&gt;
!Description&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;true, false&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;&lt;br /&gt;
|Determines whether the constant propagation should work across function boundaries&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;modify_nonstatic_functions&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;true, false&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;&lt;br /&gt;
|Determines whether the constant propagation may modify non-static functions. This should be used with care as it might be illegal if not all call-sites of the non-static functions are known.&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;remove_dead_blocks&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;no, defensive, aggressive&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;no&amp;lt;/code&amp;gt;&lt;br /&gt;
|Determines whether parts of the code that are unreachable after constant propagation should be removed. The &amp;lt;code&amp;gt;aggressive&amp;lt;/code&amp;gt; variant might seldomly insert additional &amp;lt;code&amp;gt;goto&amp;lt;/code&amp;gt; statements in cases where the&amp;lt;code&amp;gt;defensive&amp;lt;/code&amp;gt; variant rather keeps some unreachable parts of the code to avoid &amp;lt;code&amp;gt;goto&amp;lt;/code&amp;gt; statements.&lt;br /&gt;
|}&lt;br /&gt;
[[Category:Code Transformation]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Constant_Propagation_Transformation&amp;diff=2717</id>
		<title>Constant Propagation Transformation</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Constant_Propagation_Transformation&amp;diff=2717"/>
		<updated>2025-05-26T14:39:31Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Constant propagation is an optimization technique used in compilers to improve execution efficiency by replacing variables with known constant values during compilation. If an expression consists entirely of constants or previously propagated values, it is evaluated at compile time and replaced with its computed result. This transformation reduces runtime computations, simplifies code, and enables further optimizations by minimizing unnecessary variable assignments. By eliminating redundant calculations, constant propagation enhances program performance while preserving its original behavior.&lt;br /&gt;
==Constant Propagation Transformation in emmtrix Studio==&lt;br /&gt;
emmtrix Studio implements constant propagation using #pragma directives or via the GUI. Constant propagation is a transformation that propagates all known variable values within the block it is applied to. If an expression consists of propagated values and of constants, it will itself be evaluated and replaced with its constant value.&lt;br /&gt;
&lt;br /&gt;
All arithmetic operations for integral types are supported. Interprocedural propagation, i.e. propagating into and out of function calls, is partially supported.&lt;br /&gt;
&lt;br /&gt;
===Typical Usage and Benefits===&lt;br /&gt;
Constant propagation is typically used to generate a clearer code, to reduce complexity for dependency analysis and increase potential for better optimization possibilities.&lt;br /&gt;
===Example===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* The following code tests constant propagation applied to main function. &lt;br /&gt;
 * In the given example, variables a, b and c are known. &lt;br /&gt;
 * They are propagated to the rest of the body of main.&lt;br /&gt;
 */   &lt;br /&gt;
&lt;br /&gt;
#pragma EMX_TRANSFORMATION ConstPropagation&lt;br /&gt;
int main(void) {&lt;br /&gt;
    int a = 2;&lt;br /&gt;
    int b = a;&lt;br /&gt;
    &lt;br /&gt;
    int c, d;&lt;br /&gt;
    c = 2 + 2;&lt;br /&gt;
    d = b + c;&lt;br /&gt;
    &lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* The following code is the generated code after the transformation has been applied.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
    int a = 2;&lt;br /&gt;
    int b = a;&lt;br /&gt;
    int c;&lt;br /&gt;
    int d;&lt;br /&gt;
    &lt;br /&gt;
    c = 4;&lt;br /&gt;
    d = 6;&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Parameters ===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+&lt;br /&gt;
!Id&lt;br /&gt;
!Possible Values&lt;br /&gt;
!Default Value&lt;br /&gt;
!Description&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;true, false&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;&lt;br /&gt;
|Determines whether the constant propagation should work across function boundaries&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;modify_nonstatic_functions&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;true, false&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;&lt;br /&gt;
|Determines whether the constant propagation may modify non-static functions. This should be used with care as it might be illegal if not all call-sites of the non-static functions are known.&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;remove_dead_blocks&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;no, defensive, aggressive&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;no&amp;lt;/code&amp;gt;&lt;br /&gt;
|Determines whether parts of the code that are unreachable after constant propagation should be removed. The &amp;lt;code&amp;gt;aggressive&amp;lt;/code&amp;gt; variant might seldomly insert additional &amp;lt;code&amp;gt;goto&amp;lt;/code&amp;gt; statements in cases where the&amp;lt;code&amp;gt;defensive&amp;lt;/code&amp;gt; variant rather keeps some unreachable parts of the code to avoid &amp;lt;code&amp;gt;goto&amp;lt;/code&amp;gt; statements.&lt;br /&gt;
|}&lt;br /&gt;
[[Category:Code Transformation]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Constant_Propagation_Transformation&amp;diff=2455</id>
		<title>Constant Propagation Transformation</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Constant_Propagation_Transformation&amp;diff=2455"/>
		<updated>2025-02-14T15:01:40Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Constant propagation is an optimization technique used in compilers to improve execution efficiency by replacing variables with known constant values during compilation. If an expression consists entirely of constants or previously propagated values, it is evaluated at compile time and replaced with its computed result. This transformation reduces runtime computations, simplifies code, and enables further optimizations by minimizing unnecessary variable assignments. By eliminating redundant calculations, constant propagation enhances program performance while preserving its original behavior.&lt;br /&gt;
==Constant Propagation Transformation in emmtrix Studio==&lt;br /&gt;
emmtrix Studio implements constant propagation using #pragma directives or via the GUI. Constant propagation is a transformation that propagates all known variable values within the block it is applied to. If an expression consists of propagated values and of constants, it will itself be evaluated to and replaced with its constant value.&lt;br /&gt;
&lt;br /&gt;
All arithmetic operations for integral types are supported. Interprocedural propagation, i.e. propagating into and out of function calls, is partially supported.&lt;br /&gt;
&lt;br /&gt;
===Typical Usage and Benefits===&lt;br /&gt;
Constant propagation is typically used to generate a clearer code, to reduce complexity for dependency analysis and increase potential for better optimization possibilities.&lt;br /&gt;
===Example===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* The following code tests constant propagation applied to main function. &lt;br /&gt;
 * In the given example, variables a, b and c are known. &lt;br /&gt;
 * They are propagated to the rest of the body of main.&lt;br /&gt;
 */   &lt;br /&gt;
&lt;br /&gt;
#pragma EMX_TRANSFORMATION ConstPropagation&lt;br /&gt;
int main(void) {&lt;br /&gt;
    int a = 2;&lt;br /&gt;
    int b = a;&lt;br /&gt;
    &lt;br /&gt;
    int c, d;&lt;br /&gt;
    c = 2 + 2;&lt;br /&gt;
    d = b + c;&lt;br /&gt;
    &lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* The following code is the generated code after the transformation has been applied.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
    int a = 2;&lt;br /&gt;
    int b = a;&lt;br /&gt;
    int c;&lt;br /&gt;
    int d;&lt;br /&gt;
    &lt;br /&gt;
    c = 4;&lt;br /&gt;
    d = 6;&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Parameters ===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+&lt;br /&gt;
!Id&lt;br /&gt;
!Possible Values&lt;br /&gt;
!Default Value&lt;br /&gt;
!Description&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;true, false&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;&lt;br /&gt;
|Determines whether the constant propagation should work across function boundaries&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;modify_nonstatic_functions&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;true, false&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;&lt;br /&gt;
|Determines whether the constant propagation may modify non-static functions. This should be used with care as it might be illegal if not all call-sites of the non-static functions are known.&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;remove_dead_blocks&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;no, defensive, aggressive&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;no&amp;lt;/code&amp;gt;&lt;br /&gt;
|Determines whether parts of the code that are unreachable after constant propagation should be removed. The &amp;lt;code&amp;gt;aggressive&amp;lt;/code&amp;gt; variant might seldomly insert additional &amp;lt;code&amp;gt;goto&amp;lt;/code&amp;gt; statements in cases where the&amp;lt;code&amp;gt;defensive&amp;lt;/code&amp;gt; variant rather keeps some unreachable parts of the code to avoid &amp;lt;code&amp;gt;goto&amp;lt;/code&amp;gt; statements.&lt;br /&gt;
|}&lt;br /&gt;
[[Category:Code Transformation]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Constant_Propagation_Transformation&amp;diff=2454</id>
		<title>Constant Propagation Transformation</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Constant_Propagation_Transformation&amp;diff=2454"/>
		<updated>2025-02-14T13:30:27Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Constant propagation is an optimization technique used in compilers to improve execution efficiency by replacing variables with known constant values during compilation. If an expression consists entirely of constants or previously propagated values, it is evaluated at compile time and replaced with its computed result. This transformation reduces runtime computations, simplifies code, and enables further optimizations by minimizing unnecessary variable assignments. By eliminating redundant calculations, constant propagation enhances program performance while preserving its original behavior.&lt;br /&gt;
==Constant Propagation Transformation in emmtrix Studio==&lt;br /&gt;
emmtrix Studio implements constant propagation using #pragma directives or via the GUI. Constant propagation is a transformation that propagates all known variable values within the block it is applied to. If an expression consists of propagated values and of constants, it will itself be evaluated to and replaced with its constant value.&lt;br /&gt;
&lt;br /&gt;
All arithmetic operations for integral types are supported. Interprocedural propagation, i.e. propagating into and out of function calls, is partially supported.&lt;br /&gt;
&lt;br /&gt;
===Typical Usage and Benefits===&lt;br /&gt;
Constant propagation is typically used to generate a clearer code, to reduce complexity for dependency analysis and increase potential for better optimization possibilities.&lt;br /&gt;
===Example===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* The following code tests constant propagation applied to main function. &lt;br /&gt;
 * In the given example, variables a, b and c are known. &lt;br /&gt;
 * They are propagated to the rest of the body of main.&lt;br /&gt;
 */   &lt;br /&gt;
&lt;br /&gt;
#pragma EMX_TRANSFORMATION ConstPropagation&lt;br /&gt;
int main(void) {&lt;br /&gt;
    int a = 2;&lt;br /&gt;
    int b = a;&lt;br /&gt;
    &lt;br /&gt;
    int c, d;&lt;br /&gt;
    c = 2 + 2;&lt;br /&gt;
    d = b + c;&lt;br /&gt;
    &lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* The following code is the generated code after the transformation has been applied.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
int main(void) {&lt;br /&gt;
    int a = 2;&lt;br /&gt;
    int b = a;&lt;br /&gt;
    int c;&lt;br /&gt;
    int d;&lt;br /&gt;
    &lt;br /&gt;
    c = 4;&lt;br /&gt;
    d = 6;&lt;br /&gt;
&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
=== Parameters ===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+&lt;br /&gt;
!Id&lt;br /&gt;
!Possible Values&lt;br /&gt;
!Default Value&lt;br /&gt;
!Description&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;true, false&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;true&amp;lt;/code&amp;gt;&lt;br /&gt;
|Determines whether the constant propagation should work across function boundaries&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;modify_nonstatic_functions&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;true, false&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;false&amp;lt;/code&amp;gt;&lt;br /&gt;
|Determines whether the constant propagation may modify non-static functions. This should be used with care as it might be illegal if not all call-sites of the non-static functions are known.&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;remove_dead_blocks&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;no, defensive, aggressive&amp;lt;/code&amp;gt;&lt;br /&gt;
|&amp;lt;code&amp;gt;no&amp;lt;/code&amp;gt;&lt;br /&gt;
|Determines whether parts of the code that are unreachable after constant propagation should be removed. The &amp;lt;code&amp;gt;aggressive&amp;lt;/code&amp;gt; variant might seldomly insert additional &amp;lt;code&amp;gt;goto&amp;lt;/code&amp;gt; statements in cases where the&amp;lt;code&amp;gt;defensive&amp;lt;/code&amp;gt; variant rather keeps some unreachable parts of the code to avoid &amp;lt;code&amp;gt;goto&amp;lt;/code&amp;gt; statements.&lt;br /&gt;
|}&lt;br /&gt;
[[Category:Code Transformation]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Obfuscation_and_Anonymization_Transformation&amp;diff=2453</id>
		<title>Obfuscation and Anonymization Transformation</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Obfuscation_and_Anonymization_Transformation&amp;diff=2453"/>
		<updated>2025-02-14T13:17:07Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Obfuscation is a code transformation technique used to make source code difficult to read and understand while preserving its functionality. This process typically involves renaming identifiers, restructuring code, or inserting misleading elements to obscure the original logic. Obfuscation is often used to protect intellectual property, prevent reverse engineering, or secure sensitive logic in software. While the transformed code remains executable, it becomes significantly harder for humans to analyze, ensuring that key implementation details are concealed without altering program behavior.&lt;br /&gt;
==Obfuscation Transformation in emmtrix Studio==&lt;br /&gt;
emmtrix Studio implements obfuscation using #pragma directives or via the GUI. Obfuscation is a transformation that renames identifiers so that they become obscure. This feature is typically used to ensure that the code is unrecognizable. Functionality of the code and its original structure are preserved. The algorithms used are Secure Hash Algorithms SHA-1 and SHA-256.&lt;br /&gt;
===Typical Usage and Benefits===&lt;br /&gt;
The transformation is used make the source code hard to understand for humans while still preserving the original structure of the source code. It is meant for cases where the source code needs to be shared (e.g. for debugging) but no intellectual property should be shown.The transformation can be applied only to functions.&lt;br /&gt;
&lt;br /&gt;
Even though the transformation is selectable only for functions, it can affect other identifiers, as well, depending on the user settings. In its full scope, obfuscation affects all identifiers, including:&lt;br /&gt;
&lt;br /&gt;
* functions and their parameters&lt;br /&gt;
* global and local variables&lt;br /&gt;
* user-defined types&lt;br /&gt;
* structs and unions&lt;br /&gt;
* enumerators&lt;br /&gt;
* file names&lt;br /&gt;
&lt;br /&gt;
===Example===&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* The following code tests obfuscation transformation applied to main function with default parameters. &lt;br /&gt;
 * According to the default settings, all identifiers except for the external definitions and the main func-&lt;br /&gt;
   tion, shall be renamed and become obscure. &lt;br /&gt;
 * In the current code example the identifiers with the suffix _obfu are renamed.&lt;br /&gt;
 * Other identifiers include printf and main functions and they remain unmodified.&lt;br /&gt;
 */   &lt;br /&gt;
&lt;br /&gt;
const int g_obfu = 10;&lt;br /&gt;
void print_obfu(int a_obfu) {&lt;br /&gt;
    printf(” % d\ n”, a_obfu);&lt;br /&gt;
}&lt;br /&gt;
#pragma EMX_TRANSFORMATION ObfuscateIdentifiers&lt;br /&gt;
int main() {&lt;br /&gt;
    int l_obfu = 1;&lt;br /&gt;
    print_obfu(l_obfu);&lt;br /&gt;
    print_obfu(g_obfu);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
/* The following code is the generated code after the transformation has been applied.&lt;br /&gt;
 */&lt;br /&gt;
&lt;br /&gt;
const int var_ee832f = 10;&lt;br /&gt;
void func_3db980(int var_e86e08) {&lt;br /&gt;
    printf(” % d\ n”, var_e86e08);&lt;br /&gt;
}&lt;br /&gt;
void func_baf390(int var_e86e08) {&lt;br /&gt;
    printf(” % d\ n”, var_e86e08);&lt;br /&gt;
}&lt;br /&gt;
int main() {&lt;br /&gt;
    int var_32bc72 = 1;&lt;br /&gt;
    func_baf390(var_32bc72);&lt;br /&gt;
    func_3db980(var_ee832f);&lt;br /&gt;
    return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
|}Along with the transformed code, a mapping between the old and the new identifiers is created. It is stored in a CSV file obfuscation_mapping.csv.&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;&lt;br /&gt;
file_b93f1b;test_obfuscation00&lt;br /&gt;
func_3db980;print_obfu&lt;br /&gt;
func_baf390;print_obfu_duplicate2&lt;br /&gt;
var_e86e08;a_obfu&lt;br /&gt;
var_ee832f;g_obfu&lt;br /&gt;
var_32bc72;l_obfu&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
===Parameters===&lt;br /&gt;
Following parameters can be set (each description is followed by keyword in pragma-syntax and default value):&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|+&lt;br /&gt;
!Id&lt;br /&gt;
!Default Value&lt;br /&gt;
!Description&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;all&amp;lt;/code&amp;gt;&lt;br /&gt;
|true&lt;br /&gt;
|&#039;&#039;&#039;Whole Project&#039;&#039;&#039; - apply obfuscation on all translation units across project&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;external&amp;lt;/code&amp;gt;&lt;br /&gt;
|false&lt;br /&gt;
|&#039;&#039;&#039;External definitions&#039;&#039;&#039; - apply obfuscation on identifiers in header files; use with caution, since it affects used system identifiers and produces uncompilable code&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;output&amp;lt;/code&amp;gt;&lt;br /&gt;
|true&lt;br /&gt;
|&#039;&#039;&#039;Output identifiers&#039;&#039;&#039; - generate CSV file containing mapping of old and new names&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;seed&amp;lt;/code&amp;gt;&lt;br /&gt;
|enc1&lt;br /&gt;
|&#039;&#039;&#039;Seed for hash-function&#039;&#039;&#039; - arbitrary string used as input for hashing algorithms&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;code&amp;gt;n&amp;lt;/code&amp;gt;&lt;br /&gt;
|6&lt;br /&gt;
|&#039;&#039;&#039;Hash-length&#039;&#039;&#039; - length of hash code in obfuscated identifiers&lt;br /&gt;
|}&lt;br /&gt;
===Note===&lt;br /&gt;
*For same seeds and hash-lengths, obfuscation is deterministic.&lt;br /&gt;
[[Category:Code Transformation]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=emmtrix_Performance_Estimator&amp;diff=1727</id>
		<title>emmtrix Performance Estimator</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=emmtrix_Performance_Estimator&amp;diff=1727"/>
		<updated>2024-04-18T11:41:45Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: Created page with &amp;quot;emmtrix Performance Estimator (ePE) supports different ways to acquire the duration of the tasks of an application. These methods vary in accuracy and additional software or hardware requirements. Static code analysis provides basic information without the need for hardware or special software and may thus be seamlessly incorporated into early stages of the Software Development Life Cycle (SDLC). More accurate numbers can be collected with interfaces to simulators or the...&amp;quot;&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;emmtrix Performance Estimator (ePE) supports different ways to acquire the duration of the tasks of an application. These methods vary in accuracy and additional software or hardware requirements. Static code analysis provides basic information without the need for hardware or special software and may thus be seamlessly incorporated into early stages of the Software Development Life Cycle (SDLC). More accurate numbers can be collected with interfaces to simulators or the hardware. Depending on the requirements, the methods can be combined as desired.&lt;br /&gt;
&lt;br /&gt;
Addressing performance issues at the onset of the SDLC is not only proactive but also cost-efficient. Early estimation and intervention, as suggested by the below graph, can drastically reduce the complexities and expenses associated with fixing defects post-deployment. Although early estimates may not be as precise as measurements taken during later stages, they serve as a crucial checkpoint to prevent potential timing issues from escalating. This preemptive approach aligns with the principle that the cost to repair defects increases exponentially as they progress through the SDLC, as shown by the sharp rise in cost depicted in the graph. Hence, initial performance estimations are a strategic investment, minimizing the risk of facing a steep climb in remediation costs and efforts at later stages.&lt;br /&gt;
[[File:DefectDetectionandRemediation.webp|center|frameless|800x800px|Cost of Defect Remediation across the Software Development Life Cycle]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=File:DefectDetectionandRemediation.webp&amp;diff=1726</id>
		<title>File:DefectDetectionandRemediation.webp</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=File:DefectDetectionandRemediation.webp&amp;diff=1726"/>
		<updated>2024-04-18T11:36:38Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Graph showing cost of remediation of defects across the SDLC&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Main_Page&amp;diff=1725</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Main_Page&amp;diff=1725"/>
		<updated>2024-04-18T11:33:27Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;languages/&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:1--&amp;gt;&lt;br /&gt;
Welcome to the emmtrix Technologies Wiki. As a company with a deep-rooted passion for compilers, we specialize in source-to-source compilers designed to analyze, optimize and transform your code. This Wiki aims to offer detailed, technical background information that complements the tools and resources available on our official website. Here, you&#039;ll find in-depth explanations, usage guidelines, and insights into the engineering behind our specialized software solutions. Whether you&#039;re a developer or a technically-inclined enthusiast, this space is designed to deepen your understanding of what makes our tools essential for your projects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:6--&amp;gt;&lt;br /&gt;
=== emmtrix Products ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:7--&amp;gt;&lt;br /&gt;
* [[emmtrix Studio Release Notes]]&lt;br /&gt;
* emmtrix Parallel Studio&lt;br /&gt;
* [[emmtrix Performance Estimator]]&lt;br /&gt;
* [[emmtrix C++ to C Compiler]]&lt;br /&gt;
** [https://online-ecpp2c.emmtrix.com Live C++ to C compiler demo]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Compiler Background ===&lt;br /&gt;
&lt;br /&gt;
* [[Loop Transformations]]&lt;br /&gt;
*[[Demystifying C++]]&lt;br /&gt;
*[[The alias Attribute|The alias attribute]]&lt;br /&gt;
*[[Clang Diagnostics Overview]]&lt;br /&gt;
*[[:Category:Clang Diagnostics]]&lt;br /&gt;
&lt;br /&gt;
=== FAQ ===&lt;br /&gt;
&lt;br /&gt;
* [[Error Handling]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=527</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=527"/>
		<updated>2024-03-11T15:12:53Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;seo title=&amp;quot;Some things I learned about the alias attribute&amp;quot; &lt;br /&gt;
     description=&amp;quot;A deep dive into the pre- and post-C23 syntax and semantics of the alias attribute in GNU C, with fun examples for both GCC and clang.&amp;quot; &lt;br /&gt;
     keywords=&amp;quot;alias attribute, alias attribute on variables, GNU C attributes, C23 attributes, symbol table&amp;quot;&lt;br /&gt;
     robots=&amp;quot;index, follow&amp;quot; &lt;br /&gt;
     og:title=&amp;quot;Some things I learned about the alias attribute&amp;quot; &lt;br /&gt;
     og:type=&amp;quot;website&amp;quot;&lt;br /&gt;
     og:description=&amp;quot;A deep dive into the pre- and post-C23 syntax and semantics of the alias attribute in GNU C, with fun examples for both GCC and clang.&amp;quot;&lt;br /&gt;
     /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you’ve got some familiarity with C and are simply looking to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute, it’s advisable to skim through the [[#syntax|syntax]] and [[#semantics|semantics]] sections before diving into some more complicated [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll probably find some engaging content in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
Before delving in, it’s crucial to acknowledge (yet again) the portability issues associated with the alias attribute. While it offers some quite powerful capabilities, its usage should be approached with caution, especially in cross-platform projects.&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer]. Just make sure that you don&#039;t omit the flag &amp;lt;code&amp;gt;-std=c23&amp;lt;/code&amp;gt; when invoking clang.&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some address in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use the address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
The columns are also explained in &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;&#039;s [https://man7.org/linux/man-pages/man1/objdump.1.html manpage] and reading through &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt; &#039;s [https://man7.org/linux/man-pages/man1/nm.1.html manpage] might give some further information if you&#039;re really curious.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions and some tentative definitions such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt]. Funny enough, turning the tentative definition &amp;lt;code&amp;gt;int alias_var&amp;lt;/code&amp;gt; into a definition with external linkage (in the standard’s sense, see §6.9.2 in the latest [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf working draft]) as in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
reconciles clang with the code snippet. As already mentioned above, GCC is more forgiving and accepts both variants.&lt;br /&gt;
&lt;br /&gt;
So, what’s the upshot? I guess there are two points:&lt;br /&gt;
&lt;br /&gt;
* GCC and clang don’t necessarily agree on what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; actually is.&lt;br /&gt;
* If you’re using clang, an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute on some declaration is very close to a usual definition. Just make sure that either the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; is the first declaration or all other declarations are explicitly extern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her&amp;lt;ref&amp;gt;Actually, having an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; on a static symbol leads to that symbol being exposed in the corresponding object file’s symbol table, see [https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/--attribute----alias---variable-attribute ARM’s documentation] for instance. However, I did not find a way to modify the value through that entry in the symbol table as it is a non-global object.&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are highly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Links ===&lt;br /&gt;
&lt;br /&gt;
* [https://developers.redhat.com/blog/2020/06/02/the-joys-and-perils-of-c-and-c-aliasing-part-1 Series of very well-written articles] by Martin Sebor for RedHat Developer. The [https://developers.redhat.com/blog/2020/06/03/the-joys-and-perils-of-aliasing-in-c-and-c-part-2# second article] also deals with the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute from a slightly different perspective.&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html GCC&#039;s documentation on common variable attributes]&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html GCC&#039;s documentation on common function attributes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Compiler Background]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=526</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=526"/>
		<updated>2024-03-11T15:11:45Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;seo title=&amp;quot;Some things I learned about the alias attribute&amp;quot; &lt;br /&gt;
     description=&amp;quot;A deep dive into the pre- and post-C23 syntax and semantics of the alias attribute in GNU C, with fun examples for both GCC and clang.&amp;quot; &lt;br /&gt;
     keywords=&amp;quot;alias attribute, alias attribute on variables, GNU C attributes, C23 attributes, symbol table&amp;quot;&lt;br /&gt;
     robots=&amp;quot;index, follow&amp;quot; &lt;br /&gt;
     og:title=&amp;quot;Some things I learned about the alias attribute&amp;quot; &lt;br /&gt;
     og:type=&amp;quot;website&amp;quot;&lt;br /&gt;
     og:description=&amp;quot;A deep dive into the pre- and post-C23 syntax and semantics of the alias attribute in GNU C, with fun examples for both GCC and clang.&amp;quot;&lt;br /&gt;
     google-site-verification&amp;quot;=&amp;quot;OkCmr1SjqUIZTGUdEvGSYWmj36SN9CCuhvH8lbDx5Qg&amp;quot;&lt;br /&gt;
     /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you’ve got some familiarity with C and are simply looking to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute, it’s advisable to skim through the [[#syntax|syntax]] and [[#semantics|semantics]] sections before diving into some more complicated [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll probably find some engaging content in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
Before delving in, it’s crucial to acknowledge (yet again) the portability issues associated with the alias attribute. While it offers some quite powerful capabilities, its usage should be approached with caution, especially in cross-platform projects.&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer]. Just make sure that you don&#039;t omit the flag &amp;lt;code&amp;gt;-std=c23&amp;lt;/code&amp;gt; when invoking clang.&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some address in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use the address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
The columns are also explained in &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;&#039;s [https://man7.org/linux/man-pages/man1/objdump.1.html manpage] and reading through &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt; &#039;s [https://man7.org/linux/man-pages/man1/nm.1.html manpage] might give some further information if you&#039;re really curious.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions and some tentative definitions such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt]. Funny enough, turning the tentative definition &amp;lt;code&amp;gt;int alias_var&amp;lt;/code&amp;gt; into a definition with external linkage (in the standard’s sense, see §6.9.2 in the latest [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf working draft]) as in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
reconciles clang with the code snippet. As already mentioned above, GCC is more forgiving and accepts both variants.&lt;br /&gt;
&lt;br /&gt;
So, what’s the upshot? I guess there are two points:&lt;br /&gt;
&lt;br /&gt;
* GCC and clang don’t necessarily agree on what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; actually is.&lt;br /&gt;
* If you’re using clang, an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute on some declaration is very close to a usual definition. Just make sure that either the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; is the first declaration or all other declarations are explicitly extern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her&amp;lt;ref&amp;gt;Actually, having an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; on a static symbol leads to that symbol being exposed in the corresponding object file’s symbol table, see [https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/--attribute----alias---variable-attribute ARM’s documentation] for instance. However, I did not find a way to modify the value through that entry in the symbol table as it is a non-global object.&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are highly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Links ===&lt;br /&gt;
&lt;br /&gt;
* [https://developers.redhat.com/blog/2020/06/02/the-joys-and-perils-of-c-and-c-aliasing-part-1 Series of very well-written articles] by Martin Sebor for RedHat Developer. The [https://developers.redhat.com/blog/2020/06/03/the-joys-and-perils-of-aliasing-in-c-and-c-part-2# second article] also deals with the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute from a slightly different perspective.&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html GCC&#039;s documentation on common variable attributes]&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html GCC&#039;s documentation on common function attributes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Compiler Background]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Range-Based_for_Loops&amp;diff=496</id>
		<title>Demystifying C++ - Range-Based for Loops</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Range-Based_for_Loops&amp;diff=496"/>
		<updated>2024-02-28T08:00:35Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: seo block&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{#seo:&lt;br /&gt;
|title=Emulating C++ Range-Based For Loops in C: A Transpiler Perspective&lt;br /&gt;
|description=This article explores our transpiler&#039;s approach to preserving range-based loop behavior and temporary variable lifespan during C++ to C translation.&lt;br /&gt;
|keywords=range-based for loops, C++17, C programming, transpiler, code translation, modern C++, loop emulation, software development techniques, programming best practices&lt;br /&gt;
|robots=index, follow&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Range-based for loops in modern C++ offer a neat and intuitive way to iterate over the elements of a container or array. Compared to traditional loops where indices or iterators need to be explicitly managed, range-based for-loops offer a more concise and less error-prone syntax.&lt;br /&gt;
&lt;br /&gt;
Here&#039;s the substitute code for the loop that is written in the C++17 standard:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Substitute Code&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
for (range-declaration : range-expression) {&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
auto &amp;amp;&amp;amp; __range = range-expression;&lt;br /&gt;
auto __begin = begin-expr;&lt;br /&gt;
auto __end = end-expr;&lt;br /&gt;
for ( ; __begin != __end; ++__begin) {&lt;br /&gt;
  range-declaration = *__begin;&lt;br /&gt;
&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The following example showcases the C-code for a simple instance with `std::initializer_list`. The code follows the principle of the substitute code:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Range-based for loop&lt;br /&gt;
with std::initializer_list&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
#include &amp;lt;initializer_list&amp;gt;&lt;br /&gt;
&lt;br /&gt;
void work(int);&lt;br /&gt;
&lt;br /&gt;
void func() {&lt;br /&gt;
  for (int i : {1, 2, 3}) {&lt;br /&gt;
    work(i);&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
struct std::initializer_list&amp;lt;int&amp;gt; {&lt;br /&gt;
  const int *__begin_;&lt;br /&gt;
  size_t __size_;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
inline const int *std::initializer_list&amp;lt;int&amp;gt;::begin(const struct std::initializer_list&amp;lt;int&amp;gt; *this) {&lt;br /&gt;
  return this-&amp;gt;__begin_;&lt;br /&gt;
}&lt;br /&gt;
inline const int *std::initializer_list&amp;lt;int&amp;gt;::end(const struct std::initializer_list&amp;lt;int&amp;gt; *this) {&lt;br /&gt;
  return this-&amp;gt;__begin_ + this-&amp;gt;__size_;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void work(int);&lt;br /&gt;
&lt;br /&gt;
void func(void) {&lt;br /&gt;
  {&lt;br /&gt;
    const int init[3] = {1, 2, 3};&lt;br /&gt;
    struct std::initializer_list&amp;lt;int&amp;gt; tmpExpr1 = {init, 3};&lt;br /&gt;
    struct std::initializer_list&amp;lt;int&amp;gt; *__range1 = &amp;amp;tmpExpr1;&lt;br /&gt;
    const int *__begin1 = std::initializer_list&amp;lt;int&amp;gt;::begin(__range1);&lt;br /&gt;
    const int *__end1 = std::initializer_list&amp;lt;int&amp;gt;::end(__range1);&lt;br /&gt;
    for (; __begin1 != __end1; ++__begin1) {&lt;br /&gt;
      int i = *__begin1;&lt;br /&gt;
      work(i);&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This mechanism ensures that the correct lifespan of temporary variables is maintained, preserving the intended behavior of the original C++ code when translating to C.&lt;br /&gt;
[[Category:Demystifying C++|Range-Based_for_Loops]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Templates&amp;diff=495</id>
		<title>Demystifying C++ - Templates</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Templates&amp;diff=495"/>
		<updated>2024-02-28T07:57:43Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: seo block&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{#seo:&lt;br /&gt;
|title=Transpiling C++ Templates into C: Bridging Generic Programming Concepts&lt;br /&gt;
|description=Discover how C++ templates, a cornerstone of generic programming, are transpiled into C.&lt;br /&gt;
|keywords=C++ templates, generic programming, template instantiation, code duplication, C++ to C transpiler, data types, function templates, class templates, programming languages, software development&lt;br /&gt;
|robots=index, follow&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
Templates [https://en.cppreference.com/w/cpp/language/templates] are a fundamental feature of C++ essential for generic programming. They allow programmers to write code that is independent of the data type, enhancing the reusability of the code. Templates are not limited to simple data types; they can also be used with custom types such as classes and structures.&lt;br /&gt;
&lt;br /&gt;
In C++, a template is introduced with the keyword &amp;lt;code&amp;gt;template&amp;lt;/code&amp;gt;, followed by a template parameter list in angle brackets (&amp;lt;code&amp;gt;&amp;lt;&amp;gt;&amp;lt;/code&amp;gt;). The compiler then creates a new function or class from the template when it encounters a declaration with a specific type. This technique is called template instantiation. The C++ to C compiler realizes templates through the duplication of code. For each template instance of a function or class, a variant is also generated in the C code. If a template is not used, it completely disappears from the code.&lt;br /&gt;
&lt;br /&gt;
C++ templates enable the creation of functions that can work with any data type. Here&#039;s a simple example in C++:&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Template Function&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
template &amp;lt;typename T&amp;gt;&lt;br /&gt;
T max(T a, T b) {&lt;br /&gt;
    return (a &amp;gt; b) ? a : b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void func() {&lt;br /&gt;
  int a = max&amp;lt;int&amp;gt;(10, 20);&lt;br /&gt;
  float f = max&amp;lt;float&amp;gt;(10.0, 20.0);&lt;br /&gt;
&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
int max_int(int a, int b) {&lt;br /&gt;
    return (a &amp;gt; b) ? a : b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
float max_float(float a, float b) {&lt;br /&gt;
    return (a &amp;gt; b) ? a : b;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void func(void) {&lt;br /&gt;
  int a = max_int(10, 20);&lt;br /&gt;
  float f = max_float(10.0f, 20.0f);&lt;br /&gt;
&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!Template Class&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
template &amp;lt;class T, int N1, int N2&amp;gt;&lt;br /&gt;
class Matrix {&lt;br /&gt;
public:&lt;br /&gt;
    T data[N1][N2];&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
Matrix&amp;lt;int, 5, 5&amp;gt; m1;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
struct Matrix_int_3_5 {&lt;br /&gt;
    int data[3][5];&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
struct Matrix_int_3_5 m1;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[[Category:Demystifying C++|Templates]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Strict_Return&amp;diff=494</id>
		<title>Demystifying C++ - Strict Return</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Strict_Return&amp;diff=494"/>
		<updated>2024-02-28T07:55:01Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: deo block&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{#seo:&lt;br /&gt;
|title=Handling Missing Return Statements in C and C++: Compiler Strategies and Code Stability&lt;br /&gt;
|description=Explore how missing return statements in C and C++ functions can lead to undefined behavior, and how our transpiler addresses this issue.&lt;br /&gt;
|keywords=missing return statement, transpiler, undefined behavior, C programming, C++ programming, compiler warnings, __builtin_unreachable, clang compiler, GCC compiler, software development best practices&lt;br /&gt;
|robots=index, follow&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In C and C++ programming, ensuring that functions behave predictably and efficiently is paramount. One particular scenario that often requires attention is when a non-void function fails to return a value on all control paths, potentially leading to undefined behavior and compromising the stability of the program. This situation is commonly flagged by compilers, which provide warnings to alert developers of potential issues in the code.&lt;br /&gt;
&lt;br /&gt;
In the following example, a function with missing &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; statement is translated. Surprisingly, even that the input code only uses C constructs, the semantic equivalent C code contains &amp;lt;code&amp;gt;__builtin_unreachable&amp;lt;/code&amp;gt; as additional function call. &amp;lt;code&amp;gt;__builtin_unreachable&amp;lt;/code&amp;gt; is an builtin function available in clang and GCC compiler that marks unreachable code. If control flow reaches the point of the &amp;lt;code&amp;gt;__builtin_unreachable&amp;lt;/code&amp;gt;, the program is undefined. It is useful in situations where the compiler cannot deduce the unreachability of the code. (see https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html).&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Code with missing return&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
int func1(void* ptr);&lt;br /&gt;
&lt;br /&gt;
int func2(void* ptr) {&lt;br /&gt;
  if (ptr)&lt;br /&gt;
    return func1(ptr);&lt;br /&gt;
&lt;br /&gt;
  // warning: non-void function does not&lt;br /&gt;
  // return a value in all control paths&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
int func1(void* ptr);&lt;br /&gt;
&lt;br /&gt;
int func2(void* ptr) {&lt;br /&gt;
  if (ptr)&lt;br /&gt;
    return func1(ptr);&lt;br /&gt;
&lt;br /&gt;
  __builtin_unreachable();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
The reason for this is that C++ and C have different requirements regarding missing &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; within the standard. In C11 the standard says that&amp;lt;blockquote&amp;gt;&#039;&#039;&#039;If the &#039;}&#039; that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined. (6.9.1p12)&#039;&#039;&#039;&amp;lt;/blockquote&amp;gt;That means a function with missing &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is only undefined behavior if the return value is used. In C++ the standard says that&amp;lt;blockquote&amp;gt;&#039;&#039;&#039;Flowing off the end of a function [...] results in undefined behavior in a value-returning function. ([stmt.return]p2)&#039;&#039;&#039;&amp;lt;/blockquote&amp;gt;which means that in C++ any function with missing &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is undefined behavior independent if it is used or not. In contrast to C, the clang compiler marks the position within the source code as unreachable in case of a missing &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; in C++. Within the compiler frontend, that behavior is called &#039;&#039;&#039;strict return&#039;&#039;&#039;. To mimic strict &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; also in C, the &amp;lt;code&amp;gt;__builtin_unreachable&amp;lt;/code&amp;gt; must be added in the generated C source code.&lt;br /&gt;
&lt;br /&gt;
However, marking code paths as unreachable by the compiler frontend in case of missing &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; can lead to strange effects that every developer should be aware of. In the following code, the C code before and after optimization is shown:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!After frontend (before optimization)&lt;br /&gt;
!After optimization&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
int func1(void* ptr);&lt;br /&gt;
&lt;br /&gt;
int func2(void* ptr) {&lt;br /&gt;
  if (ptr)&lt;br /&gt;
    return func1(ptr);&lt;br /&gt;
&lt;br /&gt;
  __builtin_unreachable();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
int func1(void* ptr);&lt;br /&gt;
&lt;br /&gt;
int func2(void* ptr) {&lt;br /&gt;
  return func1(ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
During optimization the compiler recognizes two code path. If the condition is true, the result of func1 is returned. Otherwise, the code is unreachable. As code that is unreachable should never be reached, the optimizers thinks that the condition is meaningless and optimizes it away. That means for an end users, that a missing &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; cannot only lead to run-time errors at the location where the &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is missing but can even influence code before! This optimization is reproducible both for clang and gcc compiler.&lt;br /&gt;
&lt;br /&gt;
For that reason, missing &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; warnings should be taken seriously and either enable an error in that case with &amp;lt;code&amp;gt;-Werror=return-type&amp;lt;/code&amp;gt; or deactivate strict &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; within the compiler using &amp;lt;code&amp;gt;--no-strict-return&amp;lt;/code&amp;gt;.&lt;br /&gt;
[[Category:Demystifying C++]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Strict_Return&amp;diff=493</id>
		<title>Demystifying C++ - Strict Return</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Strict_Return&amp;diff=493"/>
		<updated>2024-02-28T07:53:11Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: code markup for return&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;In C and C++ programming, ensuring that functions behave predictably and efficiently is paramount. One particular scenario that often requires attention is when a non-void function fails to return a value on all control paths, potentially leading to undefined behavior and compromising the stability of the program. This situation is commonly flagged by compilers, which provide warnings to alert developers of potential issues in the code.&lt;br /&gt;
&lt;br /&gt;
In the following example, a function with missing &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; statement is translated. Surprisingly, even that the input code only uses C constructs, the semantic equivalent C code contains &amp;lt;code&amp;gt;__builtin_unreachable&amp;lt;/code&amp;gt; as additional function call. &amp;lt;code&amp;gt;__builtin_unreachable&amp;lt;/code&amp;gt; is an builtin function available in clang and GCC compiler that marks unreachable code. If control flow reaches the point of the &amp;lt;code&amp;gt;__builtin_unreachable&amp;lt;/code&amp;gt;, the program is undefined. It is useful in situations where the compiler cannot deduce the unreachability of the code. (see https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html).&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Code with missing return&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
int func1(void* ptr);&lt;br /&gt;
&lt;br /&gt;
int func2(void* ptr) {&lt;br /&gt;
  if (ptr)&lt;br /&gt;
    return func1(ptr);&lt;br /&gt;
&lt;br /&gt;
  // warning: non-void function does not&lt;br /&gt;
  // return a value in all control paths&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
int func1(void* ptr);&lt;br /&gt;
&lt;br /&gt;
int func2(void* ptr) {&lt;br /&gt;
  if (ptr)&lt;br /&gt;
    return func1(ptr);&lt;br /&gt;
&lt;br /&gt;
  __builtin_unreachable();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
The reason for this is that C++ and C have different requirements regarding missing &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; within the standard. In C11 the standard says that&amp;lt;blockquote&amp;gt;&#039;&#039;&#039;If the &#039;}&#039; that terminates a function is reached, and the value of the function call is used by the caller, the behavior is undefined. (6.9.1p12)&#039;&#039;&#039;&amp;lt;/blockquote&amp;gt;That means a function with missing &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is only undefined behavior if the return value is used. In C++ the standard says that&amp;lt;blockquote&amp;gt;&#039;&#039;&#039;Flowing off the end of a function [...] results in undefined behavior in a value-returning function. ([stmt.return]p2)&#039;&#039;&#039;&amp;lt;/blockquote&amp;gt;which means that in C++ any function with missing &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is undefined behavior independent if it is used or not. In contrast to C, the clang compiler marks the position within the source code as unreachable in case of a missing &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; in C++. Within the compiler frontend, that behavior is called &#039;&#039;&#039;strict return&#039;&#039;&#039;. To mimic strict &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; also in C, the &amp;lt;code&amp;gt;__builtin_unreachable&amp;lt;/code&amp;gt; must be added in the generated C source code.&lt;br /&gt;
&lt;br /&gt;
However, marking code paths as unreachable by the compiler frontend in case of missing &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; can lead to strange effects that every developer should be aware of. In the following code, the C code before and after optimization is shown:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!After frontend (before optimization)&lt;br /&gt;
!After optimization&lt;br /&gt;
|-&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
int func1(void* ptr);&lt;br /&gt;
&lt;br /&gt;
int func2(void* ptr) {&lt;br /&gt;
  if (ptr)&lt;br /&gt;
    return func1(ptr);&lt;br /&gt;
&lt;br /&gt;
  __builtin_unreachable();&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
int func1(void* ptr);&lt;br /&gt;
&lt;br /&gt;
int func2(void* ptr) {&lt;br /&gt;
  return func1(ptr);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
During optimization the compiler recognizes two code path. If the condition is true, the result of func1 is returned. Otherwise, the code is unreachable. As code that is unreachable should never be reached, the optimizers thinks that the condition is meaningless and optimizes it away. That means for an end users, that a missing &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; cannot only lead to run-time errors at the location where the &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; is missing but can even influence code before! This optimization is reproducible both for clang and gcc compiler.&lt;br /&gt;
&lt;br /&gt;
For that reason, missing &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; warnings should be taken seriously and either enable an error in that case with &amp;lt;code&amp;gt;-Werror=return-type&amp;lt;/code&amp;gt; or deactivate strict &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt; within the compiler using &amp;lt;code&amp;gt;--no-strict-return&amp;lt;/code&amp;gt;.&lt;br /&gt;
[[Category:Demystifying C++]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Cleanup_Code&amp;diff=492</id>
		<title>Demystifying C++ - Cleanup Code</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Cleanup_Code&amp;diff=492"/>
		<updated>2024-02-28T07:47:53Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{#seo:&lt;br /&gt;
|title=Transpiling C++ RAII cleanup code to C: Bridging Modern Resource Management&lt;br /&gt;
|description=Explore the transpilation of C++&#039;s RAII idiom into C, showcasing the capabilities of our C++ transpiler.&lt;br /&gt;
|keywords=RAII, Resource Acquisition Is Initialization, cleanup code, C++ to C transpiler, automatic cleanup, resource management software development tools&lt;br /&gt;
|robots=index, follow&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;RAII (Resource Acquisition Is Initialization)&#039;&#039;&#039; idiom is pivotal in modern C++. It ensures that resources are properly released and destructors are correctly called as variables and objects go out of their scope. In C++, leveraging the RAII idiom means the cleanup code is automatically generated and inserted as needed, especially in scenarios with complex control structures like loops and branches. This idiom guarantees that in the event of premature exits from a block due to statements like &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;break&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;continue&amp;lt;/code&amp;gt;, or via jumps with &amp;lt;code&amp;gt;goto&amp;lt;/code&amp;gt;, the cleanup code for the respective blocks will be executed correctly, ensuring resource and exception safety.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interestingly, the clang C++ frontend doesn&#039;t directly insert this cleanup code right before such statements. Instead, the cleanup code is generated only at one location and jumped to directly. A state variable and a switch block at the end of the cleanup code ensure that the control flow continues at the original location.&lt;br /&gt;
&lt;br /&gt;
At first glance, this approach might seem inefficient as it adds additional code for the state variable and &amp;lt;code&amp;gt;switch&amp;lt;/code&amp;gt; blocks. However, from optimization level -O1 onwards, in later compiler phases, this overhead gets eliminated by compiler optimizations like Jump Threading [https://beza1e1.tuxen.de/articles/jump_threading.html]. In this process, the cleanup code gets duplicated, and the state variable and the &amp;lt;code&amp;gt;switch&amp;lt;/code&amp;gt; blocks can be removed, making the generated code more efficient and straightforward.&lt;br /&gt;
&lt;br /&gt;
In the C implementation, as shown in the example, the state variable like &amp;lt;code&amp;gt;cleanupDestSlot&amp;lt;/code&amp;gt; is used to determine the jump destination after executing the cleanup code. The &amp;lt;code&amp;gt;goto&amp;lt;/code&amp;gt; mechanism is then used to jump to the relevant cleanup code and then onward to the final destination, based on the value of the state variable.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Cleanup Code&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void func() {&lt;br /&gt;
&lt;br /&gt;
  ClassWithDtor var1;&lt;br /&gt;
&lt;br /&gt;
  for (int i = 0; i &amp;lt; 10; ++i) {&lt;br /&gt;
    ClassWithDtor var2;&lt;br /&gt;
&lt;br /&gt;
    for (int j = 0; j &amp;lt; 10; ++j) {&lt;br /&gt;
      ClassWithDtor var3;&lt;br /&gt;
&lt;br /&gt;
      if (i == 5) {&lt;br /&gt;
&lt;br /&gt;
        continue;&lt;br /&gt;
      }&lt;br /&gt;
      if (i == 9) {&lt;br /&gt;
&lt;br /&gt;
        return;&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
      // ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void func(void) {&lt;br /&gt;
  unsigned int cleanupDestSlot;&lt;br /&gt;
  struct ClassWithDtor var1;&lt;br /&gt;
&lt;br /&gt;
  for (int i = 0; i &amp;lt; 10; ++i) {&lt;br /&gt;
    struct ClassWithDtor var2;&lt;br /&gt;
&lt;br /&gt;
    for (int j = 0; j &amp;lt; 10; ++j) {&lt;br /&gt;
      struct ClassWithDtor var3;&lt;br /&gt;
&lt;br /&gt;
      if (i == 5) {&lt;br /&gt;
        cleanupDestSlot = 1;&lt;br /&gt;
        goto __cleanup1;&lt;br /&gt;
      }&lt;br /&gt;
      if (i == 9) {&lt;br /&gt;
        cleanupDestSlot = 2;&lt;br /&gt;
        goto __cleanup1;&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
      // ...&lt;br /&gt;
&lt;br /&gt;
      cleanupDestSlot = 0;&lt;br /&gt;
    __cleanup1:&lt;br /&gt;
      ClassWithDtor::__ctor_complete(&amp;amp;var3);&lt;br /&gt;
      switch (cleanupDestSlot) {&lt;br /&gt;
        case 2: goto __cleanup3;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // ...&lt;br /&gt;
&lt;br /&gt;
    cleanupDestSlot = 0;&lt;br /&gt;
  __cleanup3:&lt;br /&gt;
    ClassWithDtor::__ctor_complete(&amp;amp;var2);&lt;br /&gt;
    switch (cleanupDestSlot) {&lt;br /&gt;
      case 2: goto __cleanup2;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // ...&lt;br /&gt;
&lt;br /&gt;
__cleanup2:&lt;br /&gt;
  ClassWithDtor::__ctor_complete(&amp;amp;var1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This cleanup mechanism ensures that even in complex control flow scenarios, all required cleanup operations are executed, maintaining the integrity and safety of the program.&lt;br /&gt;
[[Category:Demystifying C++|Cleanup_Code]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Cleanup_Code&amp;diff=491</id>
		<title>Demystifying C++ - Cleanup Code</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Cleanup_Code&amp;diff=491"/>
		<updated>2024-02-28T07:45:58Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: seo block&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{#seo:&lt;br /&gt;
|title=Transpiling C++ RAII to C: Bridging Modern Resource Management&lt;br /&gt;
|description=Explore the transpilation of C++&#039;s RAII idiom into C, showcasing the capabilities of our C++ transpiler.&lt;br /&gt;
|keywords=RAII, Resource Acquisition Is Initialization, cleanup code, C++ to C transpiler, automatic cleanup, resource management software development tools&lt;br /&gt;
|robots=index, follow&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
The &#039;&#039;&#039;RAII (Resource Acquisition Is Initialization)&#039;&#039;&#039; idiom is pivotal in modern C++. It ensures that resources are properly released and destructors are correctly called as variables and objects go out of their scope. In C++, leveraging the RAII idiom means the cleanup code is automatically generated and inserted as needed, especially in scenarios with complex control structures like loops and branches. This idiom guarantees that in the event of premature exits from a block due to statements like &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;break&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;continue&amp;lt;/code&amp;gt;, or via jumps with &amp;lt;code&amp;gt;goto&amp;lt;/code&amp;gt;, the cleanup code for the respective blocks will be executed correctly, ensuring resource and exception safety.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interestingly, the clang C++ frontend doesn&#039;t directly insert this cleanup code right before such statements. Instead, the cleanup code is generated only at one location and jumped to directly. A state variable and a switch block at the end of the cleanup code ensure that the control flow continues at the original location.&lt;br /&gt;
&lt;br /&gt;
At first glance, this approach might seem inefficient as it adds additional code for the state variable and &amp;lt;code&amp;gt;switch&amp;lt;/code&amp;gt; blocks. However, from optimization level -O1 onwards, in later compiler phases, this overhead gets eliminated by compiler optimizations like Jump Threading [https://beza1e1.tuxen.de/articles/jump_threading.html]. In this process, the cleanup code gets duplicated, and the state variable and the &amp;lt;code&amp;gt;switch&amp;lt;/code&amp;gt; blocks can be removed, making the generated code more efficient and straightforward.&lt;br /&gt;
&lt;br /&gt;
In the C implementation, as shown in the example, the state variable like &amp;lt;code&amp;gt;cleanupDestSlot&amp;lt;/code&amp;gt; is used to determine the jump destination after executing the cleanup code. The &amp;lt;code&amp;gt;goto&amp;lt;/code&amp;gt; mechanism is then used to jump to the relevant cleanup code and then onward to the final destination, based on the value of the state variable.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Cleanup Code&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void func() {&lt;br /&gt;
&lt;br /&gt;
  ClassWithDtor var1;&lt;br /&gt;
&lt;br /&gt;
  for (int i = 0; i &amp;lt; 10; ++i) {&lt;br /&gt;
    ClassWithDtor var2;&lt;br /&gt;
&lt;br /&gt;
    for (int j = 0; j &amp;lt; 10; ++j) {&lt;br /&gt;
      ClassWithDtor var3;&lt;br /&gt;
&lt;br /&gt;
      if (i == 5) {&lt;br /&gt;
&lt;br /&gt;
        continue;&lt;br /&gt;
      }&lt;br /&gt;
      if (i == 9) {&lt;br /&gt;
&lt;br /&gt;
        return;&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
      // ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void func(void) {&lt;br /&gt;
  unsigned int cleanupDestSlot;&lt;br /&gt;
  struct ClassWithDtor var1;&lt;br /&gt;
&lt;br /&gt;
  for (int i = 0; i &amp;lt; 10; ++i) {&lt;br /&gt;
    struct ClassWithDtor var2;&lt;br /&gt;
&lt;br /&gt;
    for (int j = 0; j &amp;lt; 10; ++j) {&lt;br /&gt;
      struct ClassWithDtor var3;&lt;br /&gt;
&lt;br /&gt;
      if (i == 5) {&lt;br /&gt;
        cleanupDestSlot = 1;&lt;br /&gt;
        goto __cleanup1;&lt;br /&gt;
      }&lt;br /&gt;
      if (i == 9) {&lt;br /&gt;
        cleanupDestSlot = 2;&lt;br /&gt;
        goto __cleanup1;&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
      // ...&lt;br /&gt;
&lt;br /&gt;
      cleanupDestSlot = 0;&lt;br /&gt;
    __cleanup1:&lt;br /&gt;
      ClassWithDtor::__ctor_complete(&amp;amp;var3);&lt;br /&gt;
      switch (cleanupDestSlot) {&lt;br /&gt;
        case 2: goto __cleanup3;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // ...&lt;br /&gt;
&lt;br /&gt;
    cleanupDestSlot = 0;&lt;br /&gt;
  __cleanup3:&lt;br /&gt;
    ClassWithDtor::__ctor_complete(&amp;amp;var2);&lt;br /&gt;
    switch (cleanupDestSlot) {&lt;br /&gt;
      case 2: goto __cleanup2;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // ...&lt;br /&gt;
&lt;br /&gt;
__cleanup2:&lt;br /&gt;
  ClassWithDtor::__ctor_complete(&amp;amp;var1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This cleanup mechanism ensures that even in complex control flow scenarios, all required cleanup operations are executed, maintaining the integrity and safety of the program.&lt;br /&gt;
[[Category:Demystifying C++|Cleanup_Code]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Cleanup_Code&amp;diff=490</id>
		<title>Demystifying C++ - Cleanup Code</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Cleanup_Code&amp;diff=490"/>
		<updated>2024-02-28T07:41:51Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: explicitly mention raii idiom and safety in beginning paragraph&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
The &#039;&#039;&#039;RAII (Resource Acquisition Is Initialization)&#039;&#039;&#039; idiom is pivotal in modern C++. It ensures that resources are properly released and destructors are correctly called as variables and objects go out of their scope. In C++, leveraging the RAII idiom means the cleanup code is automatically generated and inserted as needed, especially in scenarios with complex control structures like loops and branches. This idiom guarantees that in the event of premature exits from a block due to statements like &amp;lt;code&amp;gt;return&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;break&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;continue&amp;lt;/code&amp;gt;, or via jumps with &amp;lt;code&amp;gt;goto&amp;lt;/code&amp;gt;, the cleanup code for the respective blocks will be executed correctly, ensuring resource and exception safety.&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Interestingly, the clang C++ frontend doesn&#039;t directly insert this cleanup code right before such statements. Instead, the cleanup code is generated only at one location and jumped to directly. A state variable and a switch block at the end of the cleanup code ensure that the control flow continues at the original location.&lt;br /&gt;
&lt;br /&gt;
At first glance, this approach might seem inefficient as it adds additional code for the state variable and &amp;lt;code&amp;gt;switch&amp;lt;/code&amp;gt; blocks. However, from optimization level -O1 onwards, in later compiler phases, this overhead gets eliminated by compiler optimizations like Jump Threading [https://beza1e1.tuxen.de/articles/jump_threading.html]. In this process, the cleanup code gets duplicated, and the state variable and the &amp;lt;code&amp;gt;switch&amp;lt;/code&amp;gt; blocks can be removed, making the generated code more efficient and straightforward.&lt;br /&gt;
&lt;br /&gt;
In the C implementation, as shown in the example, the state variable like &amp;lt;code&amp;gt;cleanupDestSlot&amp;lt;/code&amp;gt; is used to determine the jump destination after executing the cleanup code. The &amp;lt;code&amp;gt;goto&amp;lt;/code&amp;gt; mechanism is then used to jump to the relevant cleanup code and then onward to the final destination, based on the value of the state variable.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Cleanup Code&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void func() {&lt;br /&gt;
&lt;br /&gt;
  ClassWithDtor var1;&lt;br /&gt;
&lt;br /&gt;
  for (int i = 0; i &amp;lt; 10; ++i) {&lt;br /&gt;
    ClassWithDtor var2;&lt;br /&gt;
&lt;br /&gt;
    for (int j = 0; j &amp;lt; 10; ++j) {&lt;br /&gt;
      ClassWithDtor var3;&lt;br /&gt;
&lt;br /&gt;
      if (i == 5) {&lt;br /&gt;
&lt;br /&gt;
        continue;&lt;br /&gt;
      }&lt;br /&gt;
      if (i == 9) {&lt;br /&gt;
&lt;br /&gt;
        return;&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
      // ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
  }&lt;br /&gt;
  &lt;br /&gt;
  // ...&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void func(void) {&lt;br /&gt;
  unsigned int cleanupDestSlot;&lt;br /&gt;
  struct ClassWithDtor var1;&lt;br /&gt;
&lt;br /&gt;
  for (int i = 0; i &amp;lt; 10; ++i) {&lt;br /&gt;
    struct ClassWithDtor var2;&lt;br /&gt;
&lt;br /&gt;
    for (int j = 0; j &amp;lt; 10; ++j) {&lt;br /&gt;
      struct ClassWithDtor var3;&lt;br /&gt;
&lt;br /&gt;
      if (i == 5) {&lt;br /&gt;
        cleanupDestSlot = 1;&lt;br /&gt;
        goto __cleanup1;&lt;br /&gt;
      }&lt;br /&gt;
      if (i == 9) {&lt;br /&gt;
        cleanupDestSlot = 2;&lt;br /&gt;
        goto __cleanup1;&lt;br /&gt;
      }&lt;br /&gt;
&lt;br /&gt;
      // ...&lt;br /&gt;
&lt;br /&gt;
      cleanupDestSlot = 0;&lt;br /&gt;
    __cleanup1:&lt;br /&gt;
      ClassWithDtor::__ctor_complete(&amp;amp;var3);&lt;br /&gt;
      switch (cleanupDestSlot) {&lt;br /&gt;
        case 2: goto __cleanup3;&lt;br /&gt;
      }&lt;br /&gt;
    }&lt;br /&gt;
&lt;br /&gt;
    // ...&lt;br /&gt;
&lt;br /&gt;
    cleanupDestSlot = 0;&lt;br /&gt;
  __cleanup3:&lt;br /&gt;
    ClassWithDtor::__ctor_complete(&amp;amp;var2);&lt;br /&gt;
    switch (cleanupDestSlot) {&lt;br /&gt;
      case 2: goto __cleanup2;&lt;br /&gt;
    }&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  // ...&lt;br /&gt;
&lt;br /&gt;
__cleanup2:&lt;br /&gt;
  ClassWithDtor::__ctor_complete(&amp;amp;var1);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
This cleanup mechanism ensures that even in complex control flow scenarios, all required cleanup operations are executed, maintaining the integrity and safety of the program.&lt;br /&gt;
[[Category:Demystifying C++|Cleanup_Code]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Initialization_of_Global_Variables&amp;diff=489</id>
		<title>Demystifying C++ - Initialization of Global Variables</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Initialization_of_Global_Variables&amp;diff=489"/>
		<updated>2024-02-28T07:37:21Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: seo&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{#seo:&lt;br /&gt;
|title=Global Variable Initialization in C vs. C++&lt;br /&gt;
|description=Uncover the differences in global variable initialization between C and C++ and how this is managed through special functions and attributes.&lt;br /&gt;
|keywords=global variable initialization, transpiling, C programming, C++ programming, runtime expressions, constructors, destructors, __attribute__((constructor)), __cxa_atexit, software development, programming languages&lt;br /&gt;
|robots=index, follow&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
In C, the initialization of global variables is restricted to constant expressions. This means that all global variables must be initialized at compile time, and runtime calculations or functions cannot be used. In contrast, C++ allows for more flexible initialization. When global objects or variables in C++ are created that require non-constant initialization or have a constructor, the compiler generates a special initialization function (often referred to as &amp;quot;global constructor&amp;quot;). This function is automatically called at the start of the program, even before the main() function is invoked. The clang or gcc compiler allows the specification of global constructors using the function attribute &amp;lt;code&amp;gt;__attribute__((constructor)).&amp;lt;/code&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Furthermore, the C++ compiler generates a special destructor function in the case of global objects with a destructor. This function is registered during the initialization of the global object using the &amp;quot;Itanium ABI&amp;quot; function &amp;lt;code&amp;gt;__cxa_atexit&amp;lt;/code&amp;gt; and is automatically called when the program ends normally (e.g., when return is called in the main() function or when exit() is invoked). Additionally, a DSO handle is passed to the function. DSO stands for Dynamic Shared Object, and it allows, for example, separate execution of all destructor functions for shared object (.so) files.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Initialization of Global Variable&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
double sqrt2 = sqrt(2.0);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
__attribute__((constructor)) static void __cxx_global_var_init_sqrt2(void) {&lt;br /&gt;
  sqrt2 = sqrt(2.0);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!Global Variable with Constructor/Destructor&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
C1 obj;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
extern char __dso_handle;&lt;br /&gt;
&lt;br /&gt;
__attribute__((constructor)) static void __cxx_global_var_init_obj(void) {&lt;br /&gt;
  C1_ctor_complete(&amp;amp;obj);&lt;br /&gt;
  __cxa_atexit(&amp;amp;C1_dtor_complete, &amp;amp;obj, &amp;amp;__dso_handle);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
[[Category:Demystifying C++|Initialization_of_Global_Variables]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Virtual_Classes_(Polymorphism)&amp;diff=488</id>
		<title>Demystifying C++ - Virtual Classes (Polymorphism)</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Virtual_Classes_(Polymorphism)&amp;diff=488"/>
		<updated>2024-02-28T07:33:54Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{#seo:&lt;br /&gt;
|title=Transpiling Polymorphism in C++ to C&lt;br /&gt;
|description=Delve into the concept of polymorphism in C++ and learn how virtual methods can be emulated in C.&lt;br /&gt;
|keywords=polymorphism, transpiling, object-oriented programming, C++, C programming, virtual methods, function pointers, software development, programming languages&lt;br /&gt;
|robots=index, follow&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Polymorphism, a core concept of object-oriented programming (OOP), allows different objects to be treated uniformly, typically through a common interface. C++ natively supports polymorphism through its class syntax, especially with the use of virtual methods.&lt;br /&gt;
&lt;br /&gt;
C does not support native polymorphism, as it lacks classes or virtual methods. However, polymorphism in C can be emulated using structures, function pointers, and manual initialization of virtual tables, much like a C++ compiler would implement in assembler. The following examples show a simplified C code by changing global variable names and omitting elements from the virtual function table relevant to multiple inheritances.&lt;br /&gt;
&lt;br /&gt;
To implement polymorphism in C, a __vptr is first inserted into the base class. This points to a table of function pointers. Depending on which class is derived from Base and which virtual methods are overridden, this pointer points to different tables. The derived class does not need its own __vptr, as it reuses the __vptr of the base class.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Virtual Classes&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base {&lt;br /&gt;
public:&lt;br /&gt;
  virtual void func1() {}&lt;br /&gt;
  virtual void func2() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived : public Base {&lt;br /&gt;
public:&lt;br /&gt;
  void func1() override {}&lt;br /&gt;
  virtual void func3() {}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
struct Base_vftable1 {&lt;br /&gt;
    void (*func1)(struct Base *);&lt;br /&gt;
    void (*func2)(struct Base *);&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
struct Derived_vftable1 {&lt;br /&gt;
    struct Base_vftable1 base1;&lt;br /&gt;
    void (*func3)(struct Derived *);&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
struct Base {&lt;br /&gt;
    const struct Base_vftable1 *__vptr;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
struct Derived {&lt;br /&gt;
    struct Base __base1;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
If a virtual class does not have a constructor, an implicit constructor is created. The constructor first calls the constructors of the base classes and then sets the __vptr. In this example, Derived_ctor_complete first calls Base_ctor_base. In both functions, the __vptr is sequentially set to different values. Although this may initially seem inefficient and unintuitive, it has a crucial rationale. Thus, code in the Base constructor can call virtual methods without them having already been modified. In practice, this is often unnecessary, and the first __vptr initialization can be optimized away in subsequent compiler phases, resulting in no disadvantages.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Constructor&lt;br /&gt;
|Implicit&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
const struct Base_vftable1 Base_vtable = {&amp;amp;Base_func1, &amp;amp;Base_func2};&lt;br /&gt;
const struct Derived_vftable1 Derived_vtable = {{&amp;amp;Derived_func1, &amp;amp;Base_func2}, &amp;amp;Derived_func3};&lt;br /&gt;
&lt;br /&gt;
inline void Base_ctor_base(struct Base *this) {&lt;br /&gt;
    this-&amp;gt;__vptr = &amp;amp;Base_vtable;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
inline void Derived_ctor_complete(struct Derived *this) {&lt;br /&gt;
  Base_ctor_base((struct Base*)this);&lt;br /&gt;
  this-&amp;gt;__base1.__vptr = &amp;amp;Derived_vtable;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The call of a virtual method is relatively simple. The respective function pointer in __vptr is used and an indirect function call is executed.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Virtual Method Call&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void call_virtual_method(Base* base) {&lt;br /&gt;
  base-&amp;gt;func1();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void call_virtual_method(struct Base *base) {&lt;br /&gt;
    (*base-&amp;gt;__vptr-&amp;gt;func1)(base);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Indirect function calls are not only computationally intensive in the processor but also prevent compiler optimizations like inlining. Therefore, the compiler tries to resolve these in advance whenever possible. This technique is called devirtualization. In C++11, the &#039;final&#039; keyword was introduced, which can be used for both virtual methods and classes. This keyword is not only a security feature, similar to &#039;public&#039;, &#039;private&#039;, and &#039;protected&#039;, but it also supports the compiler in devirtualization by clarifying that a method or class cannot be changed by a derived class.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Devirtualized Method Call&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void call_devirtualized_method() {&lt;br /&gt;
  Derived d;&lt;br /&gt;
&lt;br /&gt;
  d.func1();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void call_devirtualized_method() {&lt;br /&gt;
  Derived d;&lt;br /&gt;
&lt;br /&gt;
  Derived_ctor_complete(&amp;amp;d);&lt;br /&gt;
  Derived_func1(&amp;amp;d);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Dynamic casting from base classes to a derived class is enabled in C++ by the dynamic_cast operator. In the background, the compiler uses the helper function __dynamic_cast, which is implemented in libcxx. This receives type information (RTTI) of the two classes, which contain the entire class hierarchy. Within the class hierarchy of the derived class, the base class is then searched for, and if a match is found, the pointer is adjusted by an offset. The RTTI information is represented in the example via global variables typeinfo_*, without going into their content. Interested readers can view the implementation of the __dynamic_cast function from the LLVM libcxx at [https://github.com/llvm/llvm-project/blob/release/16.x/libcxxabi/src/private_typeinfo.cpp#L622].&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Dynamic Cast&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
Derived&amp;amp; dynamic_cast_ref(Base&amp;amp; base) {&lt;br /&gt;
  return dynamic_cast&amp;lt;Derived&amp;amp;&amp;gt;(base);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
struct Derived* dynamic_cast_ref(struct Base* base) {&lt;br /&gt;
    struct Derived* tmpPtr1 = __dynamic_cast(base, &amp;amp;typeinfo_Base, &amp;amp;typeinfo_Derived, 0);&lt;br /&gt;
    if (!tmpPtr1) {&lt;br /&gt;
        cxa_bad_cast();&lt;br /&gt;
    }&lt;br /&gt;
    return tmpPtr1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
[[Category:Demystifying C++|Virtual_Classes_(Polymorphism)]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_New_and_Delete_Operators&amp;diff=487</id>
		<title>Demystifying C++ - New and Delete Operators</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_New_and_Delete_Operators&amp;diff=487"/>
		<updated>2024-02-28T07:31:41Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: seo&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{#seo:&lt;br /&gt;
|title=Dynamic Memory Management in C++ vs. C: Understanding new and delete&lt;br /&gt;
|description=Explore the mechanisms of dynamic memory management in C++ with `new` and `delete`, and learn how similar functionality is achieved in C.&lt;br /&gt;
|keywords=dynamic memory management, transpiling, C++, C programming, new operator, delete operator, constructors, destructors, software development, programming languages&lt;br /&gt;
|robots=index, follow&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
In C++, the &amp;lt;code&amp;gt;new&amp;lt;/code&amp;gt; operator is used to dynamically allocate memory and call the object&#039;s constructor, while the &amp;lt;code&amp;gt;delete&amp;lt;/code&amp;gt; operator is used to call the destructor and subsequently free the memory. These operators significantly simplify memory management in C++ and reduce the likelihood of errors that often occur with manual memory management.&lt;br /&gt;
&lt;br /&gt;
In the equivalent C code, memory is allocated or released via the global &amp;lt;code&amp;gt;operator new&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;operator delete&amp;lt;/code&amp;gt; functions. The implementation of both functions is found in libc++ and uses malloc and free respectively.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!new&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
C1* obj = new C1();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
struct C1* obj = (struct C1*)::operator new(sizeof(C1));&lt;br /&gt;
if (obj) {&lt;br /&gt;
  C1_ctor_complete(obj);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!delete&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
delete obj;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
if (obj) {&lt;br /&gt;
  C1_dtor_complete(obj);&lt;br /&gt;
  ::operator delete(obj);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
The code generated by new/delete can become quite complex, for instance, when allocating or deallocating an array. In such cases, sometimes intricate loops for constructor and destructor handling are created.&lt;br /&gt;
[[Category:Demystifying C++|New_and_Delete_Operators]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Virtual_Classes_(Polymorphism)&amp;diff=486</id>
		<title>Demystifying C++ - Virtual Classes (Polymorphism)</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Virtual_Classes_(Polymorphism)&amp;diff=486"/>
		<updated>2024-02-28T07:26:59Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: seo block&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{#seo:&lt;br /&gt;
|title=Transpiling Polymorphism in C++ to C&lt;br /&gt;
|description=Delve into the concept of polymorphism in C++ and learn how virtual methods can be emulated in C.&lt;br /&gt;
|keywords=polymorphism, object-oriented programming, C++, C programming, virtual methods, function pointers, software development, programming languages&lt;br /&gt;
|robots=index, follow&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
Polymorphism, a core concept of object-oriented programming (OOP), allows different objects to be treated uniformly, typically through a common interface. C++ natively supports polymorphism through its class syntax, especially with the use of virtual methods.&lt;br /&gt;
&lt;br /&gt;
C does not support native polymorphism, as it lacks classes or virtual methods. However, polymorphism in C can be emulated using structures, function pointers, and manual initialization of virtual tables, much like a C++ compiler would implement in assembler. The following examples show a simplified C code by changing global variable names and omitting elements from the virtual function table relevant to multiple inheritances.&lt;br /&gt;
&lt;br /&gt;
To implement polymorphism in C, a __vptr is first inserted into the base class. This points to a table of function pointers. Depending on which class is derived from Base and which virtual methods are overridden, this pointer points to different tables. The derived class does not need its own __vptr, as it reuses the __vptr of the base class.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!Virtual Classes&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
class Base {&lt;br /&gt;
public:&lt;br /&gt;
  virtual void func1() {}&lt;br /&gt;
  virtual void func2() {}&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
class Derived : public Base {&lt;br /&gt;
public:&lt;br /&gt;
  void func1() override {}&lt;br /&gt;
  virtual void func3() {}&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
struct Base_vftable1 {&lt;br /&gt;
    void (*func1)(struct Base *);&lt;br /&gt;
    void (*func2)(struct Base *);&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
struct Derived_vftable1 {&lt;br /&gt;
    struct Base_vftable1 base1;&lt;br /&gt;
    void (*func3)(struct Derived *);&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
struct Base {&lt;br /&gt;
    const struct Base_vftable1 *__vptr;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
struct Derived {&lt;br /&gt;
    struct Base __base1;&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
If a virtual class does not have a constructor, an implicit constructor is created. The constructor first calls the constructors of the base classes and then sets the __vptr. In this example, Derived_ctor_complete first calls Base_ctor_base. In both functions, the __vptr is sequentially set to different values. Although this may initially seem inefficient and unintuitive, it has a crucial rationale. Thus, code in the Base constructor can call virtual methods without them having already been modified. In practice, this is often unnecessary, and the first __vptr initialization can be optimized away in subsequent compiler phases, resulting in no disadvantages.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Constructor&lt;br /&gt;
|Implicit&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
const struct Base_vftable1 Base_vtable = {&amp;amp;Base_func1, &amp;amp;Base_func2};&lt;br /&gt;
const struct Derived_vftable1 Derived_vtable = {{&amp;amp;Derived_func1, &amp;amp;Base_func2}, &amp;amp;Derived_func3};&lt;br /&gt;
&lt;br /&gt;
inline void Base_ctor_base(struct Base *this) {&lt;br /&gt;
    this-&amp;gt;__vptr = &amp;amp;Base_vtable;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
inline void Derived_ctor_complete(struct Derived *this) {&lt;br /&gt;
  Base_ctor_base((struct Base*)this);&lt;br /&gt;
  this-&amp;gt;__base1.__vptr = &amp;amp;Derived_vtable;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
The call of a virtual method is relatively simple. The respective function pointer in __vptr is used and an indirect function call is executed.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Virtual Method Call&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void call_virtual_method(Base* base) {&lt;br /&gt;
  base-&amp;gt;func1();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void call_virtual_method(struct Base *base) {&lt;br /&gt;
    (*base-&amp;gt;__vptr-&amp;gt;func1)(base);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Indirect function calls are not only computationally intensive in the processor but also prevent compiler optimizations like inlining. Therefore, the compiler tries to resolve these in advance whenever possible. This technique is called devirtualization. In C++11, the &#039;final&#039; keyword was introduced, which can be used for both virtual methods and classes. This keyword is not only a security feature, similar to &#039;public&#039;, &#039;private&#039;, and &#039;protected&#039;, but it also supports the compiler in devirtualization by clarifying that a method or class cannot be changed by a derived class.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Devirtualized Method Call&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void call_devirtualized_method() {&lt;br /&gt;
  Derived d;&lt;br /&gt;
&lt;br /&gt;
  d.func1();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void call_devirtualized_method() {&lt;br /&gt;
  Derived d;&lt;br /&gt;
&lt;br /&gt;
  Derived_ctor_complete(&amp;amp;d);&lt;br /&gt;
  Derived_func1(&amp;amp;d);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
Dynamic casting from base classes to a derived class is enabled in C++ by the dynamic_cast operator. In the background, the compiler uses the helper function __dynamic_cast, which is implemented in libcxx. This receives type information (RTTI) of the two classes, which contain the entire class hierarchy. Within the class hierarchy of the derived class, the base class is then searched for, and if a match is found, the pointer is adjusted by an offset. The RTTI information is represented in the example via global variables typeinfo_*, without going into their content. Interested readers can view the implementation of the __dynamic_cast function from the LLVM libcxx at [https://github.com/llvm/llvm-project/blob/release/16.x/libcxxabi/src/private_typeinfo.cpp#L622].&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Dynamic Cast&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
Derived&amp;amp; dynamic_cast_ref(Base&amp;amp; base) {&lt;br /&gt;
  return dynamic_cast&amp;lt;Derived&amp;amp;&amp;gt;(base);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
struct Derived* dynamic_cast_ref(struct Base* base) {&lt;br /&gt;
    struct Derived* tmpPtr1 = __dynamic_cast(base, &amp;amp;typeinfo_Base, &amp;amp;typeinfo_Derived, 0);&lt;br /&gt;
    if (!tmpPtr1) {&lt;br /&gt;
        cxa_bad_cast();&lt;br /&gt;
    }&lt;br /&gt;
    return tmpPtr1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
[[Category:Demystifying C++|Virtual_Classes_(Polymorphism)]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Classes&amp;diff=485</id>
		<title>Demystifying C++ - Classes</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Classes&amp;diff=485"/>
		<updated>2024-02-28T07:24:21Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: shortened seo blocks&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{#seo:&lt;br /&gt;
|title=Translating C++ to C: Methods, Inheritance, Constructors, and Destructors&lt;br /&gt;
|description=This guide explores the translation of C++ classes, including methods, inheritance, constructors, and destructors, into C, with a focus on understanding of object-oriented concepts in C.&lt;br /&gt;
|keywords=C++, C programming, transpiling, object-oriented programming, procedural programming, classes in C, C++ inheritance, C++ constructors, C++ destructors, software development, programming languages&lt;br /&gt;
|robots=index, follow&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;languages/&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:1--&amp;gt;&lt;br /&gt;
Classes are a cornerstone of C++ programming, embodying the object-oriented paradigm, which is essential for structuring modern, complex software applications. In C++, classes encapsulate data and behavior through attributes and methods. In contrast, Assembler and C primarily rely on data structures and functions.&lt;br /&gt;
&lt;br /&gt;
===Methods=== &amp;lt;!--T:2--&amp;gt;&lt;br /&gt;
Just as the C++ compiler does, the transpiler translates classes into C structures and functions. The methods of the class are turned into functions that take a &amp;quot;this&amp;quot; pointer to the structure representing the class. When accessing attributes, the this-pointer is used automatically, and when calling other methods, it is simply passed on.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!&amp;lt;translate&amp;gt;&amp;lt;!--T:24--&amp;gt; Methods&amp;lt;/translate&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
class C1 {&lt;br /&gt;
  int attr;&lt;br /&gt;
public:  &lt;br /&gt;
  void onChangeAttr();&lt;br /&gt;
&lt;br /&gt;
  void setAttr(int v) {&lt;br /&gt;
    attr = v;&lt;br /&gt;
    onChangeAttr();&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
struct C1 {&lt;br /&gt;
  int attr;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void C1_onChangeAttr(struct C1* this);&lt;br /&gt;
&lt;br /&gt;
void C1_setAttr(struct C1* this, int v) {&lt;br /&gt;
  this-&amp;gt;attr = v;&lt;br /&gt;
  C1_onChangeAttr(this);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
===Inheritance=== &amp;lt;!--T:6--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:7--&amp;gt;&lt;br /&gt;
In C++, inheritance is a central principle that allows classes to inherit attributes and methods from base classes. However, C does not naturally support inheritance. Therefore, the C++ to C transpiler uses special techniques to simulate inheritance relationships by embedding each base class as its own field in the derived structure.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:8--&amp;gt;&lt;br /&gt;
A crucial aspect of inheritance is the casting between base classes (Base) and derived classes (Derived):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:9--&amp;gt;&lt;br /&gt;
*&#039;&#039;&#039;Derived-to-Base Casts&#039;&#039;&#039;: In assembler, such a cast corresponds to an addition with a constant value. These are relatively simple to implement in C by accessing the respective field in the derived structure. This happens implicitly in C++ in many places, such as when calling base class methods or accessing attributes.&lt;br /&gt;
*&#039;&#039;&#039;Static Base-to-Derived Casts&#039;&#039;&#039;: In assembler, such a cast corresponds to a subtraction with a constant value. This is more complex in C and requires some C casting.&lt;br /&gt;
*&#039;&#039;&#039;Dynamic Base-to-Derived Casts&#039;&#039;&#039;: These are produced with the &amp;lt;code&amp;gt;dynamic_cast&amp;lt;/code&amp;gt; keyword and are more complex in implementation since they require type checking at runtime. Dynamic casts will be discussed in the chapter on virtual classes.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:10--&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!&amp;lt;translate&amp;gt;&amp;lt;!--T:25--&amp;gt; Representation of Base Classes&amp;lt;/translate&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
class Derived : public Base1, public Base2 {&lt;br /&gt;
  ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
struct Derived {&lt;br /&gt;
  struct Base1 base1;&lt;br /&gt;
  struct Base2 base2;&lt;br /&gt;
&lt;br /&gt;
  ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!&amp;lt;translate&amp;gt;&amp;lt;!--T:26--&amp;gt; Derived-to-Base Cast&amp;lt;/translate&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
static_cast&amp;lt;Base2*&amp;gt;(derived)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;derived-&amp;gt;base2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!&amp;lt;translate&amp;gt;&amp;lt;!--T:27--&amp;gt; Static Base-To-Derived Cast&amp;lt;/translate&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
static_cast&amp;lt;Derived*&amp;gt;(base2)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
(Derived*)((char*)base2 - offsetof(Derived, base2))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
===Constructors and Destructors=== &amp;lt;!--T:13--&amp;gt;&lt;br /&gt;
Constructors and destructors play a pivotal role in C++ programming as they manage automatic initialization and cleanup processes for objects. Constructors are special class methods that are automatically invoked when an object is created. They&#039;re often used to define initial values for object attributes and perform other initialization tasks. Destructors are the opposite of constructors; they&#039;re invoked when an object is destroyed and are ideal for cleanup tasks like releasing memory and other resources.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:14--&amp;gt;&lt;br /&gt;
In C, there&#039;s no direct counterpart for constructors and destructors. Instead, they are replaced by regular functions that are called upon object creation or destruction. For completeness, it should be mentioned that for the Itanium ABI, at least two constructor functions (&amp;lt;code&amp;gt;ctor_complete&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ctor_base&amp;lt;/code&amp;gt;) and destructor functions (&amp;lt;code&amp;gt;dtor_complete&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;dtor_base&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;dtor_delete&amp;lt;/code&amp;gt;) are generated. The complete variants are typically used, base variants are required for base classes in the constructor/destructor, and &amp;lt;code&amp;gt;dtor_delete&amp;lt;/code&amp;gt; is used when applying delete to a class with a virtual destructor.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:15--&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!&amp;lt;translate&amp;gt;&amp;lt;!--T:28--&amp;gt; Constructors and Destructors&amp;lt;/translate&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
class C1 {&lt;br /&gt;
  int a = 10;&lt;br /&gt;
public:&lt;br /&gt;
  C1() {&lt;br /&gt;
    // Initialization code&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  ~C1() {&lt;br /&gt;
    // Cleanup code&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
struct C1 {&lt;br /&gt;
  int a;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void C1_ctor_complete(struct C1* this) {&lt;br /&gt;
  this-&amp;gt;a = 10;&lt;br /&gt;
  // Initialization code&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void C1_dtor_complete(struct C1* this) {&lt;br /&gt;
  // Cleanup code&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!&amp;lt;translate&amp;gt;&amp;lt;!--T:28--&amp;gt; Constructors and Destructors&amp;lt;br&amp;gt;of Base Classes&amp;lt;/translate&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
class C1 : public Base1 {&lt;br /&gt;
  int a = 10;&lt;br /&gt;
public:&lt;br /&gt;
&lt;br /&gt;
  C1() {&lt;br /&gt;
    // Initialization code&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  ~C1() {&lt;br /&gt;
    // Cleanup code&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
struct C1 {&lt;br /&gt;
  Base1 base1;&lt;br /&gt;
  int a;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void C1_ctor_complete(struct C1* this) {&lt;br /&gt;
  Base1_ctor_base(&amp;amp;this-&amp;gt;base1);&lt;br /&gt;
  a = 10;&lt;br /&gt;
  // Initialization code&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void C1_dtor_complete(struct C1* this) {&lt;br /&gt;
  // Cleanup code&lt;br /&gt;
  Base1_dtor_base(&amp;amp;this-&amp;gt;base1);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!&amp;lt;translate&amp;gt;&amp;lt;!--T:29--&amp;gt; Stack Variables&amp;lt;/translate&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  C1 obj;&lt;br /&gt;
&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  struct C1 obj;&lt;br /&gt;
  C1_ctor_complete(&amp;amp;obj);&lt;br /&gt;
&lt;br /&gt;
  ...&lt;br /&gt;
&lt;br /&gt;
  C1_dtor_complete(&amp;amp;obj);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[[Category:Demystifying C++|Classes]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Classes&amp;diff=484</id>
		<title>Demystifying C++ - Classes</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Classes&amp;diff=484"/>
		<updated>2024-02-28T07:21:51Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: seo block&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{#seo:&lt;br /&gt;
|title=Translating C++ Classes to C: Methods, Inheritance, Constructors, and Destructors&lt;br /&gt;
|description=This guide explores the translation of C++ classes, including methods, inheritance, constructors, and destructors, into C, highlighting the practical application and understanding of object-oriented concepts in procedural programming.&lt;br /&gt;
|keywords=C++, C programming, transpiling, object-oriented programming, procedural programming, classes in C, C++ inheritance, C++ constructors, C++ destructors, software development, programming languages&lt;br /&gt;
|robots=index, follow&lt;br /&gt;
}}&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;languages/&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&amp;lt;!--T:1--&amp;gt;&lt;br /&gt;
Classes are a cornerstone of C++ programming, embodying the object-oriented paradigm, which is essential for structuring modern, complex software applications. In C++, classes encapsulate data and behavior through attributes and methods. In contrast, Assembler and C primarily rely on data structures and functions.&lt;br /&gt;
&lt;br /&gt;
===Methods=== &amp;lt;!--T:2--&amp;gt;&lt;br /&gt;
Just as the C++ compiler does, the transpiler translates classes into C structures and functions. The methods of the class are turned into functions that take a &amp;quot;this&amp;quot; pointer to the structure representing the class. When accessing attributes, the this-pointer is used automatically, and when calling other methods, it is simply passed on.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!&amp;lt;translate&amp;gt;&amp;lt;!--T:24--&amp;gt; Methods&amp;lt;/translate&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
class C1 {&lt;br /&gt;
  int attr;&lt;br /&gt;
public:  &lt;br /&gt;
  void onChangeAttr();&lt;br /&gt;
&lt;br /&gt;
  void setAttr(int v) {&lt;br /&gt;
    attr = v;&lt;br /&gt;
    onChangeAttr();&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
struct C1 {&lt;br /&gt;
  int attr;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void C1_onChangeAttr(struct C1* this);&lt;br /&gt;
&lt;br /&gt;
void C1_setAttr(struct C1* this, int v) {&lt;br /&gt;
  this-&amp;gt;attr = v;&lt;br /&gt;
  C1_onChangeAttr(this);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
===Inheritance=== &amp;lt;!--T:6--&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:7--&amp;gt;&lt;br /&gt;
In C++, inheritance is a central principle that allows classes to inherit attributes and methods from base classes. However, C does not naturally support inheritance. Therefore, the C++ to C transpiler uses special techniques to simulate inheritance relationships by embedding each base class as its own field in the derived structure.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:8--&amp;gt;&lt;br /&gt;
A crucial aspect of inheritance is the casting between base classes (Base) and derived classes (Derived):&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:9--&amp;gt;&lt;br /&gt;
*&#039;&#039;&#039;Derived-to-Base Casts&#039;&#039;&#039;: In assembler, such a cast corresponds to an addition with a constant value. These are relatively simple to implement in C by accessing the respective field in the derived structure. This happens implicitly in C++ in many places, such as when calling base class methods or accessing attributes.&lt;br /&gt;
*&#039;&#039;&#039;Static Base-to-Derived Casts&#039;&#039;&#039;: In assembler, such a cast corresponds to a subtraction with a constant value. This is more complex in C and requires some C casting.&lt;br /&gt;
*&#039;&#039;&#039;Dynamic Base-to-Derived Casts&#039;&#039;&#039;: These are produced with the &amp;lt;code&amp;gt;dynamic_cast&amp;lt;/code&amp;gt; keyword and are more complex in implementation since they require type checking at runtime. Dynamic casts will be discussed in the chapter on virtual classes.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:10--&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!&amp;lt;translate&amp;gt;&amp;lt;!--T:25--&amp;gt; Representation of Base Classes&amp;lt;/translate&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
class Derived : public Base1, public Base2 {&lt;br /&gt;
  ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
struct Derived {&lt;br /&gt;
  struct Base1 base1;&lt;br /&gt;
  struct Base2 base2;&lt;br /&gt;
&lt;br /&gt;
  ...&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!&amp;lt;translate&amp;gt;&amp;lt;!--T:26--&amp;gt; Derived-to-Base Cast&amp;lt;/translate&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
static_cast&amp;lt;Base2*&amp;gt;(derived)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
&amp;amp;derived-&amp;gt;base2&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!&amp;lt;translate&amp;gt;&amp;lt;!--T:27--&amp;gt; Static Base-To-Derived Cast&amp;lt;/translate&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
static_cast&amp;lt;Derived*&amp;gt;(base2)&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
(Derived*)((char*)base2 - offsetof(Derived, base2))&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
===Constructors and Destructors=== &amp;lt;!--T:13--&amp;gt;&lt;br /&gt;
Constructors and destructors play a pivotal role in C++ programming as they manage automatic initialization and cleanup processes for objects. Constructors are special class methods that are automatically invoked when an object is created. They&#039;re often used to define initial values for object attributes and perform other initialization tasks. Destructors are the opposite of constructors; they&#039;re invoked when an object is destroyed and are ideal for cleanup tasks like releasing memory and other resources.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:14--&amp;gt;&lt;br /&gt;
In C, there&#039;s no direct counterpart for constructors and destructors. Instead, they are replaced by regular functions that are called upon object creation or destruction. For completeness, it should be mentioned that for the Itanium ABI, at least two constructor functions (&amp;lt;code&amp;gt;ctor_complete&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ctor_base&amp;lt;/code&amp;gt;) and destructor functions (&amp;lt;code&amp;gt;dtor_complete&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;dtor_base&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;dtor_delete&amp;lt;/code&amp;gt;) are generated. The complete variants are typically used, base variants are required for base classes in the constructor/destructor, and &amp;lt;code&amp;gt;dtor_delete&amp;lt;/code&amp;gt; is used when applying delete to a class with a virtual destructor.&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:15--&amp;gt;&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
|-&lt;br /&gt;
!&amp;lt;translate&amp;gt;&amp;lt;!--T:28--&amp;gt; Constructors and Destructors&amp;lt;/translate&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
class C1 {&lt;br /&gt;
  int a = 10;&lt;br /&gt;
public:&lt;br /&gt;
  C1() {&lt;br /&gt;
    // Initialization code&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  ~C1() {&lt;br /&gt;
    // Cleanup code&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
struct C1 {&lt;br /&gt;
  int a;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void C1_ctor_complete(struct C1* this) {&lt;br /&gt;
  this-&amp;gt;a = 10;&lt;br /&gt;
  // Initialization code&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void C1_dtor_complete(struct C1* this) {&lt;br /&gt;
  // Cleanup code&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!&amp;lt;translate&amp;gt;&amp;lt;!--T:28--&amp;gt; Constructors and Destructors&amp;lt;br&amp;gt;of Base Classes&amp;lt;/translate&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
class C1 : public Base1 {&lt;br /&gt;
  int a = 10;&lt;br /&gt;
public:&lt;br /&gt;
&lt;br /&gt;
  C1() {&lt;br /&gt;
    // Initialization code&lt;br /&gt;
  }&lt;br /&gt;
&lt;br /&gt;
  ~C1() {&lt;br /&gt;
    // Cleanup code&lt;br /&gt;
  }&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
struct C1 {&lt;br /&gt;
  Base1 base1;&lt;br /&gt;
  int a;&lt;br /&gt;
};&lt;br /&gt;
&lt;br /&gt;
void C1_ctor_complete(struct C1* this) {&lt;br /&gt;
  Base1_ctor_base(&amp;amp;this-&amp;gt;base1);&lt;br /&gt;
  a = 10;&lt;br /&gt;
  // Initialization code&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
void C1_dtor_complete(struct C1* this) {&lt;br /&gt;
  // Cleanup code&lt;br /&gt;
  Base1_dtor_base(&amp;amp;this-&amp;gt;base1);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!&amp;lt;translate&amp;gt;&amp;lt;!--T:29--&amp;gt; Stack Variables&amp;lt;/translate&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  C1 obj;&lt;br /&gt;
&lt;br /&gt;
  ...&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
{&lt;br /&gt;
  struct C1 obj;&lt;br /&gt;
  C1_ctor_complete(&amp;amp;obj);&lt;br /&gt;
&lt;br /&gt;
  ...&lt;br /&gt;
&lt;br /&gt;
  C1_dtor_complete(&amp;amp;obj);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
[[Category:Demystifying C++|Classes]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Name_Mangling&amp;diff=483</id>
		<title>Demystifying C++ - Name Mangling</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Demystifying_C%2B%2B_-_Name_Mangling&amp;diff=483"/>
		<updated>2024-02-27T15:06:29Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;seo title=&amp;quot;Demystifying Name Mangling in C++&amp;quot;&lt;br /&gt;
     description=&amp;quot;Explore Name Mangling in C++, a crucial trick for function overloading, namespaces, and templates, enabling core functionalities unique to C++.&amp;quot;&lt;br /&gt;
     keywords=&amp;quot;C++ Name Mangling, function overloading, namespaces, C++ templates&amp;quot;&lt;br /&gt;
     robots=&amp;quot;index, follow&amp;quot;&lt;br /&gt;
     og:title=&amp;quot;Understanding Name Mangling in C++&amp;quot;&lt;br /&gt;
     og:type=&amp;quot;article&amp;quot;&lt;br /&gt;
     og:description=&amp;quot;Explore Name Mangling in C++, a crucial trick for function overloading, namespaces, and templates, enabling core functionalities unique to C++.&amp;quot;&lt;br /&gt;
     /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
One of the key features of C++ is &amp;quot;Name Mangling&amp;quot; [https://en.wikipedia.org/wiki/Name_mangling]. Although it may seem like a peripheral detail at first glance, it is a fundamental element that enables several core functionalities of C++. Name Mangling is the process by which the C++ compiler modifies the names of variables, functions, and other entities to ensure uniqueness and specific information about these entities. This concept becomes particularly important when considering function overloading, namespaces, class hierarchies, and templates - features not natively present in C.&lt;br /&gt;
&lt;br /&gt;
The process of Name Mangling ensures that each element is uniquely identifiable, even if it appears identical in C++ source code syntax. This is crucial for the linker, which must allocate the correct functions and variables across different files and libraries. As a result, developers often encounter mangled names within object files. Tools like &amp;lt;code&amp;gt;c++filt&amp;lt;/code&amp;gt; or websites can be used to demangle these names back into readable elements.&lt;br /&gt;
&lt;br /&gt;
The C++ standard does not prescribe a specific method for Name Mangling. This means that the implementation of Name Mangling is left to the discretion of the compiler manufacturer, leading to differences in Mangling conventions between different compilers. The Itanium C++ ABI, for instance, establishes Name Mangling conventions and is commonly used on Unix-based systems.&lt;br /&gt;
&lt;br /&gt;
The keyword &amp;lt;code&amp;gt;extern &amp;quot;C&amp;quot;&amp;lt;/code&amp;gt; can be used to disable Name Mangling for functions in C++. This facilitates interoperability between C++ and C, among other things.&lt;br /&gt;
&lt;br /&gt;
Examples of Name Mangling:&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Feature&lt;br /&gt;
!C++ Code&lt;br /&gt;
!C Code&lt;br /&gt;
|-&lt;br /&gt;
!Overloading&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void func1(char, short, int, long)&lt;br /&gt;
void func2(int, int&amp;amp;,  int&amp;amp;&amp;amp;, int*);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void _Z5func1csil(char, short, int, long);&lt;br /&gt;
void _Z5func2iRiOiPi(int, int *, int *, int *);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!Namespace&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
namespace ns1 {&lt;br /&gt;
  void func1();&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void _ZN3ns15func1Ev(void);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!Class&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
class c1 {&lt;br /&gt;
  static void func1();&lt;br /&gt;
};&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
static void _ZN2c15func1Ev(void);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!Special Functions&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void* operator new(size_t s);&lt;br /&gt;
void operator delete(void*);&lt;br /&gt;
bool operator &amp;lt; (c1, c1);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void * _Znwm(size_t s);&lt;br /&gt;
void _ZdlPv(void *ptr);&lt;br /&gt;
bool _Zlt2c1S_(struct c1, struct c1);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!External Statements&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
extern &amp;quot;C&amp;quot; void func1();&lt;br /&gt;
void func2();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void func1(void);&lt;br /&gt;
void _Z5func2v(void);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
!Template Functions&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
template&amp;lt;typename T&amp;gt;&lt;br /&gt;
void func() { /* ... */ }&lt;br /&gt;
&lt;br /&gt;
template void func&amp;lt;int&amp;gt;();&lt;br /&gt;
template void func&amp;lt;float&amp;gt;();&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|&amp;lt;pre&amp;gt;&lt;br /&gt;
void _Z4funcIiEvv(void);&lt;br /&gt;
void _Z4funcIfEvv(void);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
For simplicity, the C++ names of the functions will be used throughout the following paper, even if it results in technically incorrect C code.&lt;br /&gt;
&lt;br /&gt;
[[Category:Demystifying C++|Demystifying_C++]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Loop_Transformations&amp;diff=482</id>
		<title>Loop Transformations</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Loop_Transformations&amp;diff=482"/>
		<updated>2024-02-27T11:05:32Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;seo title=&amp;quot;Loop Transformations&amp;quot; &lt;br /&gt;
     description=&amp;quot;A comprehensive overview of loop transformations with examples.&amp;quot; &lt;br /&gt;
     keywords=&amp;quot;loop transformation, parallelization, locality&amp;quot;&lt;br /&gt;
     robots=&amp;quot;index, follow&amp;quot; &lt;br /&gt;
     og:title=&amp;quot;Loop Transformations&amp;quot; &lt;br /&gt;
     og:type=&amp;quot;website&amp;quot;&lt;br /&gt;
     og:description=&amp;quot;A comprehensive overview of loop transformations with examples.&amp;quot;&lt;br /&gt;
     /&amp;gt;&lt;br /&gt;
&#039;&#039;&#039;Loop transformation&#039;&#039;&#039; is a pivotal technique in optimizing the performance of computer programs, particularly those that heavily rely on loop structures. It plays a crucial role in enhancing the efficiency, speed, and parallelization of software applications, allowing developers to maximize hardware utilization and minimize execution time.&lt;br /&gt;
&lt;br /&gt;
=== Definition of Loop Transformation ===&lt;br /&gt;
Loop transformation, in the realm of computer science, refers to the process of modifying the structure, order, or execution of loops in a program, with the objective of optimizing performance. This can involve alterations to the loop&#039;s iteration space, data access patterns, or overall structure, to achieve improvements in runtime, energy consumption, or other performance metrics.&lt;br /&gt;
&lt;br /&gt;
=== Importance of Loop Transformation ===&lt;br /&gt;
The significance of loop transformation extends across various domains, including high-performance computing, embedded systems, and scientific computing. By optimizing loop structures, developers can ensure that applications run more efficiently, exploiting the capabilities of modern hardware architectures and parallel processing units. This is particularly crucial in domains where computational resources are a limiting factor, and optimal performance is paramount.&lt;br /&gt;
&lt;br /&gt;
=== Objective of the Article ===&lt;br /&gt;
This article aims to provide a comprehensive overview of loop transformation, exploring its principles, techniques, applications, and impact on the field of computing. It will delve into the various types of loop transformations, their implementation through compilers and manual techniques, and their application in real-world scenarios. The article will also discuss the challenges and future trends in loop transformation, offering insights into the evolving landscape of this essential optimization technique.&lt;br /&gt;
&lt;br /&gt;
== Background and Basics ==&lt;br /&gt;
&lt;br /&gt;
Before delving into the intricacies of loop transformation, it is essential to understand the foundational concepts of loops and why transforming them is necessary.&lt;br /&gt;
&lt;br /&gt;
=== Understanding Loops ===&lt;br /&gt;
&lt;br /&gt;
==== Definition and Structure ====&lt;br /&gt;
A &#039;&#039;&#039;loop&#039;&#039;&#039; in computer programming is a control flow statement that allows code to be executed repeatedly based on a given Boolean condition. The loop can be thought of as a repeating if statement. The basic types of loops include for loops, while loops, and do-while loops, each with its unique structure and use case.&lt;br /&gt;
&lt;br /&gt;
==== Types of Loops ====&lt;br /&gt;
* &#039;&#039;&#039;For Loop:&#039;&#039;&#039; Typically used when the number of iterations is known beforehand.&lt;br /&gt;
* &#039;&#039;&#039;While Loop:&#039;&#039;&#039; Used when the number of iterations is unknown, and continuation is based on a condition.&lt;br /&gt;
* &#039;&#039;&#039;Do-While Loop:&#039;&#039;&#039; Similar to the while loop but guarantees at least one execution of the loop body.&lt;br /&gt;
&lt;br /&gt;
=== The Need for Transformation ===&lt;br /&gt;
&lt;br /&gt;
==== Performance Optimization ====&lt;br /&gt;
Loop transformations are crucial for optimizing the performance of programs. By restructuring loops, developers can minimize the number of instructions executed, reduce cache misses, and improve the overall efficiency of the program.&lt;br /&gt;
&lt;br /&gt;
==== Parallelization ====&lt;br /&gt;
In the era of multi-core processors, loop transformation is vital for exploiting parallelism in code. It enables the concurrent execution of loop iterations, reducing the overall execution time and making optimal use of available processing units.&lt;br /&gt;
&lt;br /&gt;
==== Energy Efficiency ====&lt;br /&gt;
Optimized loops consume less energy, a critical consideration in battery-powered devices and large-scale computing environments. Through loop transformation, developers can achieve more energy-efficient code, prolonging battery life and reducing operational costs.&lt;br /&gt;
&lt;br /&gt;
In the subsequent sections, we will explore the principles, types, and techniques of loop transformation, shedding light on its applications, challenges, and future trends in the field of computing.&lt;br /&gt;
&lt;br /&gt;
== Principles of Loop Transformation ==&lt;br /&gt;
&lt;br /&gt;
Loop transformation is governed by a set of principles aimed at optimizing various aspects of program execution. Understanding these principles is crucial for implementing effective loop transformations.&lt;br /&gt;
&lt;br /&gt;
=== Data Locality Enhancement ===&lt;br /&gt;
Improving &#039;&#039;&#039;data locality&#039;&#039;&#039; is one of the primary goals of loop transformation. By optimizing the way data is accessed and utilized within loops, developers can reduce cache misses and improve cache utilization, leading to enhanced performance.&lt;br /&gt;
&lt;br /&gt;
=== Loop Nesting and Reordering ===&lt;br /&gt;
&#039;&#039;&#039;Loop nesting&#039;&#039;&#039; and &#039;&#039;&#039;reordering&#039;&#039;&#039; involve changing the order of nested loops and the sequence of statements within loops. These transformations can optimize the execution order of loop bodies, minimizing overhead and improving data access patterns.&lt;br /&gt;
&lt;br /&gt;
=== Reduction in Control Overhead ===&lt;br /&gt;
Minimizing the control overhead associated with loop structures is another essential principle of loop transformation. By reducing the number of branch instructions and loop control operations, developers can achieve more streamlined and efficient loop execution.&lt;br /&gt;
&lt;br /&gt;
In the following sections, we will delve deeper into the various types of loop transformations and explore how these principles are applied to optimize loop structures in different computing environments.&lt;br /&gt;
== Loop Transformation Types ==&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
! Transformation Type !! Example Before Transformation !! Example After Transformation&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Fusion&#039;&#039;&#039; &amp;lt;br&amp;gt; Combines two or more adjacent loops that have the same iteration space, reducing the overhead of loop control and improving data locality.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  b[i] = a[i] + i;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++) {&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
  b[i] = a[i] + i;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Fission or Distribution&#039;&#039;&#039; &amp;lt;br&amp;gt; Divides a loop with multiple statements into several smaller loops, each handling a subset of the statements, which can improve parallelism and cache utilization.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++) {&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
  b[i] = a[i] + i;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for(int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
for(int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  b[i] = a[i] + i;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Interchange&#039;&#039;&#039; &amp;lt;br&amp;gt; Swaps the order of nested loops, which can optimize data locality and access patterns.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  for (int j = 0; j &amp;lt; m; j++)&lt;br /&gt;
    a[i][j] = i + j;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int j = 0; j &amp;lt; m; j++)&lt;br /&gt;
  for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
    a[i][j] = i + j;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Skewing&#039;&#039;&#039; &amp;lt;br&amp;gt; Changes the iteration space of nested loops to optimize data access patterns, particularly useful for optimizing stencil computations.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 1; i &amp;lt; n; i++)&lt;br /&gt;
  for (int j = 1; j &amp;lt; m; j++)&lt;br /&gt;
    a[i][j] = a[i-1][j] + a[i][j-1];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int k = 2; k &amp;lt; m + n; k++) {&lt;br /&gt;
  for (int j = max(1, k - n + 1); j &amp;lt; min(k, m); j++) {&lt;br /&gt;
    int i = k - j;&lt;br /&gt;
    a[i][j] = a[i-1][j] + a[i][j-1];&lt;br /&gt;
  }&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Tiling&#039;&#039;&#039; &amp;lt;br&amp;gt; Breaks the iteration space of a loop into smaller chunks or tiles, improving cache locality and enabling parallel execution of tiles.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  for (int j = 0; j &amp;lt; m; j++)&lt;br /&gt;
    a[i][j] = i + j;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
int tileSize = 10;&lt;br /&gt;
for (int ii = 0; ii &amp;lt; n; ii += tileSize)&lt;br /&gt;
  for (int jj = 0; jj &amp;lt; m; jj += tileSize)&lt;br /&gt;
    for (int i = ii; i &amp;lt; min(ii + tileSize, n); i++)&lt;br /&gt;
      for (int j = jj; j &amp;lt; min(jj + tileSize, m); j++)&lt;br /&gt;
        a[i][j] = i + j;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Unrolling&#039;&#039;&#039; &amp;lt;br&amp;gt; Increases the number of operations in the loop body and decreases the control overhead by executing multiple iterations in a single loop iteration.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
int i;&lt;br /&gt;
for (i = 0; i + 3 &amp;lt; n; i+=4) {&lt;br /&gt;
  a[i    ] =  i      *  i;&lt;br /&gt;
  a[i + 1] = (i + 1) * (i + 1);&lt;br /&gt;
  a[i + 2] = (i + 2) * (i + 2);&lt;br /&gt;
  a[i + 3] = (i + 3) * (i + 3);&lt;br /&gt;
}&lt;br /&gt;
for ( ; i &amp;lt; n; i++)&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Inversion&#039;&#039;&#039; &amp;lt;br&amp;gt; Converts a while loop into a do-while loop inside an if statement, which can reduce the overhead of the loop control.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
while (condition) {&lt;br /&gt;
  // Loop Body&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
if (condition) {&lt;br /&gt;
  do {&lt;br /&gt;
    // Loop Body&lt;br /&gt;
  } while(condition);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Peeling&#039;&#039;&#039; &amp;lt;br&amp;gt; Separates the first or last iteration(s) from the loop, which can help in resolving dependencies and optimizing specific iterations.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  a[i] = b[i] + c[i];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
if (n &amp;gt; 0) {&lt;br /&gt;
  a[0] = b[0] + c[0];&lt;br /&gt;
  for (int i = 1; i &amp;lt; n; i++)&lt;br /&gt;
    a[i] = b[i] + c[i];&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Fusion&#039;&#039;&#039; &amp;lt;br&amp;gt; Combines two or more adjacent loops that have the same iteration space, reducing the overhead of loop control and improving data locality.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  b[i] = a[i] + i;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++) {&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
  b[i] = a[i] + i;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Reversal&#039;&#039;&#039; &amp;lt;br&amp;gt; Reverses the order in which the loop iterates over its iteration space.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = n-1; i &amp;gt;= 0; i--)&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Jamming&#039;&#039;&#039; &amp;lt;br&amp;gt; Combines two or more loops that have the same iteration space but are not adjacent, potentially improving data locality.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
// Some code in between&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  b[i] = a[i] + i;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++) {&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
  b[i] = a[i] + i;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Shifting&#039;&#039;&#039; &amp;lt;br&amp;gt; Alters the start and end of the loop iteration space, which can help in optimizing specific iterations.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 1; i &amp;lt;= n; i++)&lt;br /&gt;
  a[i-1] = (i-1) * (i-1);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Unswitching&#039;&#039;&#039; &amp;lt;br&amp;gt; Moves a conditional statement outside of the loop, reducing the overhead of evaluating the condition in each iteration.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++) {&lt;br /&gt;
  if (condition)&lt;br /&gt;
    a[i] = i * i;&lt;br /&gt;
  else&lt;br /&gt;
    b[i] = i + i;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
if (condition) {&lt;br /&gt;
  for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
    a[i] = i * i;&lt;br /&gt;
} else {&lt;br /&gt;
  for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
    b[i] = i + i;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Splitting&#039;&#039;&#039; &amp;lt;br&amp;gt; Divides the loop into segments, allowing for different optimizations in each segment.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  // Some operations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; k; i++)&lt;br /&gt;
  // Some operations&lt;br /&gt;
for (int i = k; i &amp;lt; n; i++)&lt;br /&gt;
  // Some operations&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Software Pipelining&#039;&#039;&#039; &amp;lt;br&amp;gt; Reorders instructions to optimize the utilization of processing units and improve the overall execution time.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
// n is a big number&lt;br /&gt;
for (i = 0; i &amp;lt; n; i++) {&lt;br /&gt;
  A(i);&lt;br /&gt;
  B(i);&lt;br /&gt;
  C(i);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
A(0);&lt;br /&gt;
A(1); B(0);&lt;br /&gt;
for (i = 0; i &amp;lt; n - 2; i++) {&lt;br /&gt;
  A(i+2);&lt;br /&gt;
  B(i+1);&lt;br /&gt;
  C(i);&lt;br /&gt;
}&lt;br /&gt;
B(i+1); C(i);&lt;br /&gt;
C(i+1);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Induction Variable Substitution&#039;&#039;&#039; &amp;lt;br&amp;gt; Replaces the loop induction variable with another variable, which can simplify the loop body and improve performance.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  a[i] = i * i + i;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
int j = 0;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++, j+=i)&lt;br /&gt;
  a[i] = j;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Rerolling&#039;&#039;&#039; &amp;lt;br&amp;gt; Transforms a loop with a small body and a large number of iterations into a loop with a larger body and fewer iterations, potentially reducing the overhead of loop control operations.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 4*n; i++)&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++) {&lt;br /&gt;
  a[4*i    ] = (4*i    ) * (4*i    );&lt;br /&gt;
  a[4*i + 1] = (4*i + 1) * (4*i + 1);&lt;br /&gt;
  a[4*i + 2] = (4*i + 2) * (4*i + 2);&lt;br /&gt;
  a[4*i + 3] = (4*i + 3) * (4*i + 3);&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Array Contraction&#039;&#039;&#039; &amp;lt;br&amp;gt; Converts a temporary array used in a loop into a scalar variable, reducing memory usage.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
float temp[10];&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt;
  temp[i] = i * i;&lt;br /&gt;
float sum = 0;&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt;
  sum += temp[i];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
float sum = 0;&lt;br /&gt;
for (int i = 0; i &amp;lt; 10; i++)&lt;br /&gt;
  sum += i * i;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Coalescing&#039;&#039;&#039; &amp;lt;br&amp;gt; Combines multiple loops with the same body but different iteration spaces into a single loop, reducing loop control overhead.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
for (int i = n; i &amp;lt; 2*n; i++)&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; 2*n; i++)&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Hoisting&#039;&#039;&#039; &amp;lt;br&amp;gt; Moves invariant computations outside of the loop, reducing the computational overhead within the loop.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  a[i] = log(2);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
float temp = log(2);&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  a[i] = temp;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Strengthening Reduction&#039;&#039;&#039; &amp;lt;br&amp;gt; Simplifies the computations within the loop, potentially improving performance.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  a[i] = i * (i + 1);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
int sum = 0;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++) {&lt;br /&gt;
  a[i] = sum;&lt;br /&gt;
  sum += i + 1;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Normalization&#039;&#039;&#039; &amp;lt;br&amp;gt; Transforms the loop variable and bounds to a canonical form, usually starting the loop variable from 0 and incrementing it by 1 in each iteration.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 1; i &amp;lt;= n; i++)&lt;br /&gt;
  a[i] = i * i;&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  a[i+1] = (i+1) * (i+1);&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Vectorization&#039;&#039;&#039; &amp;lt;br&amp;gt; Converts scalar operations to vector operations, allowing multiple data points to be processed in a single instruction.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  a[i] = b[i] + c[i];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| Assuming vectorization support and appropriate data alignment:&amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i+=4)&lt;br /&gt;
  a[i:i+3] = b[i:i+3] + c[i:i+3];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Parallelization&#039;&#039;&#039; &amp;lt;br&amp;gt; Modifies the loop to run its iterations in parallel, allowing for improved performance on multi-core systems.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  a[i] = b[i] + c[i];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
#pragma omp parallel for&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  a[i] = b[i] + c[i];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Loop Blocking&#039;&#039;&#039; &amp;lt;br&amp;gt; Breaks the loop&#039;s iteration space into blocks, allowing for improved cache utilization.&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
for (int i = 0; i &amp;lt; n; i++)&lt;br /&gt;
  for (int j = 0; j &amp;lt; m; j++)&lt;br /&gt;
    a[i][j] = b[i][j] + c[i][j];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
| &amp;lt;pre style=&amp;quot;white-space: pre;&amp;quot;&amp;gt;&lt;br /&gt;
int blockSize = 10;&lt;br /&gt;
for (int ii = 0; ii &amp;lt; n; ii += blockSize)&lt;br /&gt;
  for (int jj = 0; jj &amp;lt; m; jj += blockSize)&lt;br /&gt;
    for (int i = ii; i &amp;lt; min(ii + blockSize, n); i++)&lt;br /&gt;
      for (int j = jj; j &amp;lt; min(jj + blockSize, m); j++)&lt;br /&gt;
        a[i][j] = b[i][j] + c[i][j];&lt;br /&gt;
&amp;lt;/pre&amp;gt;&lt;br /&gt;
|}&lt;br /&gt;
&lt;br /&gt;
== Categories of Loop Transformations ==&lt;br /&gt;
&lt;br /&gt;
Loop transformations are crucial optimization techniques used to enhance the performance of programs by modifying the structure of loops. The table below provides a comprehensive overview of various loop transformations and their impact on different aspects of program execution. Each transformation is evaluated based on its effect on Data Locality, Control Overhead, Parallelization, Computations, Memory Usage, and Code Size.&lt;br /&gt;
&lt;br /&gt;
* &#039;&#039;&#039;Data Locality:&#039;&#039;&#039; Represents whether a transformation improves or decreases the efficiency of data access by optimizing the usage of cache memory.&lt;br /&gt;
* &#039;&#039;&#039;Control Overhead:&#039;&#039;&#039; Indicates whether a transformation increases or reduces the overhead associated with the control flow of loops, such as condition checks and branching.&lt;br /&gt;
* &#039;&#039;&#039;Parallelization:&#039;&#039;&#039; Denotes whether a transformation enables or improves the division of work among multiple processors to execute simultaneously.&lt;br /&gt;
* &#039;&#039;&#039;Computations:&#039;&#039;&#039; Specifies whether a transformation simplifies the computations inside the loop, reducing the computational load.&lt;br /&gt;
* &#039;&#039;&#039;Memory Usage:&#039;&#039;&#039; Represents whether a transformation optimizes or increases the memory footprint of the loop.&lt;br /&gt;
* &#039;&#039;&#039;Code Size:&#039;&#039;&#039; Indicates whether a transformation increases or reduces the amount of code.&lt;br /&gt;
&lt;br /&gt;
In the table:&lt;br /&gt;
* &#039;&#039;&#039;Improved&#039;&#039;&#039; denotes that the transformation generally improves the respective category.&lt;br /&gt;
* &#039;&#039;&#039;Reduced&#039;&#039;&#039; denotes that the transformation generally reduces the respective category.&lt;br /&gt;
* &#039;&#039;&#039;Increased&#039;&#039;&#039; denotes that the transformation generally increases the respective category.&lt;br /&gt;
* &#039;&#039;&#039;Can Decrease&#039;&#039;&#039; denotes that the transformation can potentially decrease the respective category depending on the specific characteristics of the code and the hardware it runs on.&lt;br /&gt;
* &#039;&#039;&#039;Can Increase&#039;&#039;&#039; denotes that the transformation can potentially increase the respective category depending on the specific characteristics of the code and the hardware it runs on.&lt;br /&gt;
* An empty cell denotes that the transformation typically does not have a direct impact on the respective category.&lt;br /&gt;
&lt;br /&gt;
{| class=&amp;quot;wikitable&amp;quot;&lt;br /&gt;
!Transformation!!Data Locality!!Control Overhead!!Parallelization!!Computations!!Memory Usage!!Code Size&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Fusion&#039;&#039;&#039;||Improved||Reduced ||  || || ||Increased&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Fission or Distribution&#039;&#039;&#039;||Can Decrease||Increased||Improved|| || ||Increased&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Interchange&#039;&#039;&#039;|| Can Decrease|| || || || ||&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Skewing&#039;&#039;&#039;||Improved||  || || || ||&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Tiling (Blocking)&#039;&#039;&#039;|| Can Decrease||Increased||Improved|| ||Can Increase||&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Unrolling&#039;&#039;&#039;|| ||Increased||  ||Simplified||Increased||Increased&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Inversion&#039;&#039;&#039;|| ||Reduced|| || || ||&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Peeling&#039;&#039;&#039;|| ||Reduced|| || || ||&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Reversal&#039;&#039;&#039;|| ||Reduced|| || || ||&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Jamming&#039;&#039;&#039;||Improved||Increased|| || || ||Increased&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Shifting&#039;&#039;&#039;||Improved|| || || || ||&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Unswitching&#039;&#039;&#039;||  ||Reduced|| || || ||&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Software Pipelining&#039;&#039;&#039;|| ||Reduced||Improved|| || || &lt;br /&gt;
|-&lt;br /&gt;
| &#039;&#039;&#039;Induction Variable Substitution&#039;&#039;&#039;|| || || ||Simplified|| ||&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Rerolling&#039;&#039;&#039;|| ||Increased|| || || || Increased&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Array Contraction&#039;&#039;&#039;|| || || || ||Reduced ||Reduced&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Coalescing&#039;&#039;&#039;||Can Decrease||Increased|| || || ||&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Hoisting&#039;&#039;&#039;|| || Reduced|| ||Simplified|| ||&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Strengthening Reduction&#039;&#039;&#039;||  || || ||Simplified|| ||&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Normalization&#039;&#039;&#039;|| ||Reduced|| ||Simplified|| ||&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Vectorization&#039;&#039;&#039;||Improved|| ||Improved|| || ||&lt;br /&gt;
|-&lt;br /&gt;
|&#039;&#039;&#039;Loop Parallelization&#039;&#039;&#039;|| || ||Improved|| || ||&lt;br /&gt;
|}&lt;br /&gt;
[[Category:Compiler Background]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=481</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=481"/>
		<updated>2024-02-27T10:50:56Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: seo&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;seo title=&amp;quot;Some things I learned about the alias attribute&amp;quot; &lt;br /&gt;
     description=&amp;quot;A deep dive into the pre- and post-C23 syntax and semantics of the alias attribute in GNU C, with fun examples for both GCC and clang.&amp;quot; &lt;br /&gt;
     keywords=&amp;quot;alias attribute, alias attribute on variables, GNU C attributes, C23 attributes, symbol table&amp;quot;&lt;br /&gt;
     robots=&amp;quot;index, follow&amp;quot; &lt;br /&gt;
     og:title=&amp;quot;Some things I learned about the alias attribute&amp;quot; &lt;br /&gt;
     og:type=&amp;quot;website&amp;quot;&lt;br /&gt;
     og:description=&amp;quot;A deep dive into the pre- and post-C23 syntax and semantics of the alias attribute in GNU C, with fun examples for both GCC and clang.&amp;quot;&lt;br /&gt;
     /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you’ve got some familiarity with C and are simply looking to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute, it’s advisable to skim through the [[#syntax|syntax]] and [[#semantics|semantics]] sections before diving into some more complicated [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll probably find some engaging content in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
Before delving in, it’s crucial to acknowledge (yet again) the portability issues associated with the alias attribute. While it offers some quite powerful capabilities, its usage should be approached with caution, especially in cross-platform projects.&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer]. Just make sure that you don&#039;t omit the flag &amp;lt;code&amp;gt;-std=c23&amp;lt;/code&amp;gt; when invoking clang.&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some address in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use the address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
The columns are also explained in &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;&#039;s [https://man7.org/linux/man-pages/man1/objdump.1.html manpage] and reading through &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt; &#039;s [https://man7.org/linux/man-pages/man1/nm.1.html manpage] might give some further information if you&#039;re really curious.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions and some tentative definitions such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt]. Funny enough, turning the tentative definition &amp;lt;code&amp;gt;int alias_var&amp;lt;/code&amp;gt; into a definition with external linkage (in the standard’s sense, see §6.9.2 in the latest [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf working draft]) as in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
reconciles clang with the code snippet. As already mentioned above, GCC is more forgiving and accepts both variants.&lt;br /&gt;
&lt;br /&gt;
So, what’s the upshot? I guess there are two points:&lt;br /&gt;
&lt;br /&gt;
* GCC and clang don’t necessarily agree on what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; actually is.&lt;br /&gt;
* If you’re using clang, an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute on some declaration is very close to a usual definition. Just make sure that either the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; is the first declaration or all other declarations are explicitly extern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her&amp;lt;ref&amp;gt;Actually, having an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; on a static symbol leads to that symbol being exposed in the corresponding object file’s symbol table, see [https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/--attribute----alias---variable-attribute ARM’s documentation] for instance. However, I did not find a way to modify the value through that entry in the symbol table as it is a non-global object.&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are highly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Links ===&lt;br /&gt;
&lt;br /&gt;
* [https://developers.redhat.com/blog/2020/06/02/the-joys-and-perils-of-c-and-c-aliasing-part-1 Series of very well-written articles] by Martin Sebor for RedHat Developer. The [https://developers.redhat.com/blog/2020/06/03/the-joys-and-perils-of-aliasing-in-c-and-c-part-2# second article] also deals with the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute from a slightly different perspective.&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html GCC&#039;s documentation on common variable attributes]&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html GCC&#039;s documentation on common function attributes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Compiler Background]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=426</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=426"/>
		<updated>2024-02-26T16:58:04Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: typo&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you’ve got some familiarity with C and are simply looking to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute, it’s advisable to skim through the [[#syntax|syntax]] and [[#semantics|semantics]] sections before diving into some more complicated [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll probably find some engaging content in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
Before delving in, it’s crucial to acknowledge (yet again) the portability issues associated with the alias attribute. While it offers some quite powerful capabilities, its usage should be approached with caution, especially in cross-platform projects.&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer]. Just make sure that you don&#039;t omit the flag &amp;lt;code&amp;gt;-std=c23&amp;lt;/code&amp;gt; when invoking clang.&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some address in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use the address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
The columns are also explained in &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;&#039;s [https://man7.org/linux/man-pages/man1/objdump.1.html manpage] and reading through &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt; &#039;s [https://man7.org/linux/man-pages/man1/nm.1.html manpage] might give some further information if you&#039;re really curious.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions and some tentative definitions such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt]. Funny enough, turning the tentative definition &amp;lt;code&amp;gt;int alias_var&amp;lt;/code&amp;gt; into a definition with external linkage (in the standard’s sense, see §6.9.2 in the latest [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf working draft]) as in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
reconciles clang with the code snippet. As already mentioned above, GCC is more forgiving and accepts both variants.&lt;br /&gt;
&lt;br /&gt;
So, what’s the upshot? I guess there are two points:&lt;br /&gt;
&lt;br /&gt;
* GCC and clang don’t necessarily agree on what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; actually is.&lt;br /&gt;
* If you’re using clang, an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute on some declaration is very close to a usual definition. Just make sure that either the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; is the first declaration or all other declarations are explicitly extern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her&amp;lt;ref&amp;gt;Actually, having an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; on a static symbol leads to that symbol being exposed in the corresponding object file’s symbol table, see [https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/--attribute----alias---variable-attribute ARM’s documentation] for instance. However, I did not find a way to modify the value through that entry in the symbol table as it is a non-global object.&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are highly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Links ===&lt;br /&gt;
&lt;br /&gt;
* [https://developers.redhat.com/blog/2020/06/02/the-joys-and-perils-of-c-and-c-aliasing-part-1 Series of very well-written articles] by Martin Sebor for RedHat Developer. The [https://developers.redhat.com/blog/2020/06/03/the-joys-and-perils-of-aliasing-in-c-and-c-part-2# second article] also deals with the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute from a slightly different perspective.&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html GCC&#039;s documentation on common variable attributes]&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html GCC&#039;s documentation on common function attributes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Compiler Background]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=425</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=425"/>
		<updated>2024-02-26T16:56:29Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you’ve got some familiarity with C and are simply looking to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute, it’s advisable to skim through the [[#syntax|syntax]] and [[#semantics|semantics]] sections before diving into some more complicated [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll probably find some engaging content in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
Before delving in, it’s crucial to acknowledge (yet again) the portability issues associated with the alias attribute. While it offers some quite powerful capabilities, its usage should be approached with caution, especially in cross-platform projects.&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer]. Just make sure that you don&#039;t omit the flag &amp;lt;code&amp;gt;-std=c23&amp;lt;/code&amp;gt; when invoking clang.&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some adress in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use the address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
The columns are also explained in &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;&#039;s [https://man7.org/linux/man-pages/man1/objdump.1.html manpage] and reading through &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt; &#039;s [https://man7.org/linux/man-pages/man1/nm.1.html manpage] might give some further information if you&#039;re really curious.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions and some tentative definitions such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt]. Funny enough, turning the tentative definition &amp;lt;code&amp;gt;int alias_var&amp;lt;/code&amp;gt; into a definition with external linkage (in the standard’s sense, see §6.9.2 in the latest [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf working draft]) as in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
reconciles clang with the code snippet. As already mentioned above, GCC is more forgiving and accepts both variants.&lt;br /&gt;
&lt;br /&gt;
So, what’s the upshot? I guess there are two points:&lt;br /&gt;
&lt;br /&gt;
* GCC and clang don’t necessarily agree on what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; actually is.&lt;br /&gt;
* If you’re using clang, an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute on some declaration is very close to a usual definition. Just make sure that either the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; is the first declaration or all other declarations are explicitly extern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her&amp;lt;ref&amp;gt;Actually, having an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; on a static symbol leads to that symbol being exposed in the corresponding object file’s symbol table, see [https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/--attribute----alias---variable-attribute ARM’s documentation] for instance. However, I did not find a way to modify the value through that entry in the symbol table as it is a non-global object.&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are highly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Links ===&lt;br /&gt;
&lt;br /&gt;
* [https://developers.redhat.com/blog/2020/06/02/the-joys-and-perils-of-c-and-c-aliasing-part-1 Series of very well-written articles] by Martin Sebor for RedHat Developer. The [https://developers.redhat.com/blog/2020/06/03/the-joys-and-perils-of-aliasing-in-c-and-c-part-2# second article] also deals with the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute from a slightly different perspective.&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html GCC&#039;s documentation on common variable attributes]&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html GCC&#039;s documentation on common function attributes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Compiler Background]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=424</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=424"/>
		<updated>2024-02-26T16:49:04Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;{{DISPLAYTITLE:Some things I learned about the alias attribute}}&lt;br /&gt;
&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you’ve got some familiarity with C and are simply looking to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute, it’s advisable to skim through the [[#syntax|syntax]] and [[#semantics|semantics]] sections before diving into some more complicated [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll probably find some engaging content in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
Before delving in, it’s crucial to acknowledge (yet again) the portability issues associated with the alias attribute. While it offers some quite powerful capabilities, its usage should be approached with caution, especially in cross-platform projects.&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer]. Just make sure that you don&#039;t omit the flag &amp;lt;code&amp;gt;-std=c23&amp;lt;/code&amp;gt; when invoking clang.&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some adress in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use the address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
The columns are also explained in &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;&#039;s [https://man7.org/linux/man-pages/man1/objdump.1.html manpage] and reading through &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt; &#039;s [https://man7.org/linux/man-pages/man1/nm.1.html manpage] might give some further information if you&#039;re really curious.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions and some tentative definitions such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt]. Funny enough, turning the tentative definition &amp;lt;code&amp;gt;int alias_var&amp;lt;/code&amp;gt; into a definition with external linkage (in the standard’s sense, see §6.9.2 in the latest [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf working draft]) as in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
reconciles clang with the code snippet. As already mentioned above, GCC is more forgiving and accepts both variants.&lt;br /&gt;
&lt;br /&gt;
So, what’s the upshot? I guess there are two points:&lt;br /&gt;
&lt;br /&gt;
* GCC and clang don’t necessarily agree on what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; actually is.&lt;br /&gt;
* If you’re using clang, an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute on some declaration is very close to a usual definition. Just make sure that either the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; is the first declaration or all other declarations are explicitly extern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her&amp;lt;ref&amp;gt;Actually, having an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; on a static symbol leads to that symbol being exposed in the corresponding object file’s symbol table, see [https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/--attribute----alias---variable-attribute ARM’s documentation] for instance. However, I did not find a way to modify the value through that entry in the symbol table as it is a non-global object.&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are highly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Links ===&lt;br /&gt;
&lt;br /&gt;
* [https://developers.redhat.com/blog/2020/06/02/the-joys-and-perils-of-c-and-c-aliasing-part-1 Series of very well-written articles] by Martin Sebor for RedHat Developer. The [https://developers.redhat.com/blog/2020/06/03/the-joys-and-perils-of-aliasing-in-c-and-c-part-2# second article] also deals with the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute from a slightly different perspective.&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html GCC&#039;s documentation on common variable attributes]&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html GCC&#039;s documentation on common function attributes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Compiler Background]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=366</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=366"/>
		<updated>2024-02-23T09:23:53Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you’ve got some familiarity with C and are simply looking to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute, it’s advisable to skim through the [[#syntax|syntax]] and [[#semantics|semantics]] sections before diving into some more complicated [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll probably find some engaging content in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
Before delving in, it’s crucial to acknowledge (yet again) the portability issues associated with the alias attribute. While it offers some quite powerful capabilities, its usage should be approached with caution, especially in cross-platform projects.&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer]. Just make sure that you don&#039;t omit the flag &amp;lt;code&amp;gt;-std=c23&amp;lt;/code&amp;gt; when invoking clang.&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some adress in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use the address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
The columns are also explained in &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;&#039;s [https://man7.org/linux/man-pages/man1/objdump.1.html manpage] and reading through &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt; &#039;s [https://man7.org/linux/man-pages/man1/nm.1.html manpage] might give some further information if you&#039;re really curious.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions and some tentative definitions such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt]. Funny enough, turning the tentative definition &amp;lt;code&amp;gt;int alias_var&amp;lt;/code&amp;gt; into a definition with external linkage (in the standard’s sense, see §6.9.2 in the latest [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf working draft]) as in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
reconciles clang with the code snippet. As already mentioned above, GCC is more forgiving and accepts both variants.&lt;br /&gt;
&lt;br /&gt;
So, what’s the upshot? I guess there are two points:&lt;br /&gt;
&lt;br /&gt;
* GCC and clang don’t necessarily agree on what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; actually is.&lt;br /&gt;
* If you’re using clang, an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute on some declaration is very close to a usual definition. Just make sure that either the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; is the first declaration or all other declarations are explicitly extern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her&amp;lt;ref&amp;gt;Actually, having an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; on a static symbol leads to that symbol being exposed in the corresponding object file’s symbol table, see [https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/--attribute----alias---variable-attribute ARM’s documentation] for instance. However, I did not find a way to modify the value through that entry in the symbol table as it is a non-global object.&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are highly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Links ===&lt;br /&gt;
&lt;br /&gt;
* [https://developers.redhat.com/blog/2020/06/02/the-joys-and-perils-of-c-and-c-aliasing-part-1 Series of very well-written articles] by Martin Sebor for RedHat Developer. The [https://developers.redhat.com/blog/2020/06/03/the-joys-and-perils-of-aliasing-in-c-and-c-part-2# second article] also deals with the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute from a slightly different perspective.&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html GCC&#039;s documentation on common variable attributes]&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html GCC&#039;s documentation on common function attributes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Compiler Background]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=344</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=344"/>
		<updated>2024-02-22T17:11:46Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: fixed typo and inserted links to nm/objdump manpage&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you’ve got some familiarity with C and are simply looking to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute, it’s advisable to skim through the [[#syntax|syntax]] and [[#semantics|semantics]] sections before diving into some more complicated [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll probably find some engaging content in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
Before delving in, it’s crucial to acknowledge (yet again) the portability issues associated with the alias attribute. While it offers some quite powerful capabilities, its usage should be approached with caution, especially in cross-platform projects.&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer].&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some adress in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use the address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
The columns are also explained in &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;&#039;s [https://man7.org/linux/man-pages/man1/objdump.1.html manpage] and reading through &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt; &#039;s [https://man7.org/linux/man-pages/man1/nm.1.html manpage] might give some further information if you&#039;re really curious.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions and some tentative definitions such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt]. Funny enough, turning the tentative definition &amp;lt;code&amp;gt;int alias_var&amp;lt;/code&amp;gt; into a definition with external linkage (in the standard’s sense, see §6.9.2 in the latest [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf working draft]) as in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
reconciles clang with the code snippet. As already mentioned above, GCC is more forgiving and accepts both variants.&lt;br /&gt;
&lt;br /&gt;
So, what’s the upshot? I guess there are two points:&lt;br /&gt;
&lt;br /&gt;
* GCC and clang don’t necessarily agree on what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; actually is.&lt;br /&gt;
* If you’re using clang, an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute on some declaration is very close to a usual definition. Just make sure that either the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; is the first declaration or all other declarations are explicitly extern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her&amp;lt;ref&amp;gt;Actually, having an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; on a static symbol leads to that symbol being exposed in the corresponding object file’s symbol table, see [https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/--attribute----alias---variable-attribute ARM’s documentation] for instance. However, I did not find a way to modify the value through that entry in the symbol table as it is a non-global object.&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are highly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Links ===&lt;br /&gt;
&lt;br /&gt;
* [https://developers.redhat.com/blog/2020/06/02/the-joys-and-perils-of-c-and-c-aliasing-part-1 Series of very well-written articles] by Martin Sebor for RedHat Developer. The [https://developers.redhat.com/blog/2020/06/03/the-joys-and-perils-of-aliasing-in-c-and-c-part-2# second article] also deals with the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html GCC&#039;s documentation on common variable attributes]&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html GCC&#039;s documentation on common function attributes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Compiler Background]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=340</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=340"/>
		<updated>2024-02-21T12:56:14Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;span id=&amp;quot;some-things-i-learned-about-the-alias-attribute&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
= Some things I learned about the alias attribute =&lt;br /&gt;
&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you’ve got some familiarity with C and are simply looking to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute, it’s advisable to skim through the [[#syntax|syntax]] and [[#semantics|semantics]] sections before diving into some more complicated [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll probably find some engaging content in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
Before delving in, it’s crucial to acknowledge (yet again) the portability issues associated with the alias attribute. While it offers some quite powerful capabilities, its usage should be approached with caution, especially in cross-platform projects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntax&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer].&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some adress in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use the address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions and some tentative definitions such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt]. Funny enough, turning the tentative definition &amp;lt;code&amp;gt;int alias_var&amp;lt;/code&amp;gt; into a definition with external linkage (in the standard’s sense, see §6.9.2 in the latest [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf working draft]) as in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
reconciles clang with the code snippet. As already mentioned above, GCC is more forgiving and accepts both variants.&lt;br /&gt;
&lt;br /&gt;
So, what’s the upshot? I guess there are two points:&lt;br /&gt;
&lt;br /&gt;
* GCC and clang don’t necessarily agree on what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; actually is.&lt;br /&gt;
* If you’re using clang, an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute on some declaration is very close to a usual definition. Just make sure that either the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; is the first declaration or all other declarations are explicitly extern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her&amp;lt;ref&amp;gt;Actually, having an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; on a static symbol leads to that symbol being exposed in the corresponding object file’s symbol table, see [https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/--attribute----alias---variable-attribute ARM’s documentation] for instance. However, I did not find a way to modify the value through that entry in the symbol table as it is a non-global object.&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are hightly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Links ===&lt;br /&gt;
&lt;br /&gt;
* [https://developers.redhat.com/blog/2020/06/02/the-joys-and-perils-of-c-and-c-aliasing-part-1 Series of very well-written articles] by Martin Sebor for RedHat Developer. The [https://developers.redhat.com/blog/2020/06/03/the-joys-and-perils-of-aliasing-in-c-and-c-part-2# second article] also deals with the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html GCC&#039;s documentation on common variable attributes]&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html GCC&#039;s documentation on common function attributes]&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
[[Category:Compiler Background]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=339</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=339"/>
		<updated>2024-02-21T12:55:44Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;span id=&amp;quot;some-things-i-learned-about-the-alias-attribute&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
= Some things I learned about the alias attribute =&lt;br /&gt;
&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you’ve got some familiarity with C and are simply looking to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute, it’s advisable to skim through the [[#syntax|syntax]] and [[#semantics|semantics]] sections before diving into some more complicated [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll probably find some engaging content in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
Before delving in, it’s crucial to acknowledge (yet again) the portability issues associated with the alias attribute. While it offers some quite powerful capabilities, its usage should be approached with caution, especially in cross-platform projects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntax&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer].&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some adress in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use the address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions and some tentative definitions such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt]. Funny enough, turning the tentative definition &amp;lt;code&amp;gt;int alias_var&amp;lt;/code&amp;gt; into a definition with external linkage (in the standard’s sense, see §6.9.2 in the latest [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf working draft]) as in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
reconciles clang with the code snippet. As already mentioned above, GCC is more forgiving and accepts both variants.&lt;br /&gt;
&lt;br /&gt;
So, what’s the upshot? I guess there are two points:&lt;br /&gt;
&lt;br /&gt;
* GCC and clang don’t necessarily agree on what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; actually is.&lt;br /&gt;
* If you’re using clang, an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute on some declaration is very close to a usual definition. Just make sure that either the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; is the first declaration or all other declarations are explicitly extern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her&amp;lt;ref&amp;gt;Actually, having an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; on a static symbol leads to that symbol being exposed in the corresponding object file’s symbol table, see [https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/--attribute----alias---variable-attribute ARM’s documentation] for instance. However, I did not find a way to modify the value through that entry in the symbol table as it is a non-global object.&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are hightly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Links ===&lt;br /&gt;
&lt;br /&gt;
* [https://developers.redhat.com/blog/2020/06/02/the-joys-and-perils-of-c-and-c-aliasing-part-1 Series of very well-written articles] by Martin Sebor for RedHat Developer. The [https://developers.redhat.com/blog/2020/06/03/the-joys-and-perils-of-aliasing-in-c-and-c-part-2# second article] also deals with the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html GCC&#039;s documentation on common variable attributes]&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html GCC&#039;s documentation on common function attributes]&lt;br /&gt;
&lt;br /&gt;
-----&lt;br /&gt;
&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
[[Category:Compiler Background]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=338</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=338"/>
		<updated>2024-02-21T12:54:58Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;span id=&amp;quot;some-things-i-learned-about-the-alias-attribute&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
= Some things I learned about the alias attribute =&lt;br /&gt;
&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you’ve got some familiarity with C and are simply looking to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute, it’s advisable to skim through the [[#syntax|syntax]] and [[#semantics|semantics]] sections before diving into some more complicated [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll probably find some engaging content in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
Before delving in, it’s crucial to acknowledge (yet again) the portability issues associated with the alias attribute. While it offers some quite powerful capabilities, its usage should be approached with caution, especially in cross-platform projects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntax&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer].&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some adress in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use the address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions and some tentative definitions such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt]. Funny enough, turning the tentative definition &amp;lt;code&amp;gt;int alias_var&amp;lt;/code&amp;gt; into a definition with external linkage (in the standard’s sense, see §6.9.2 in the latest [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf working draft]) as in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
reconciles clang with the code snippet. As already mentioned above, GCC is more forgiving and accepts both variants.&lt;br /&gt;
&lt;br /&gt;
So, what’s the upshot? I guess there are two points:&lt;br /&gt;
&lt;br /&gt;
* GCC and clang don’t necessarily agree on what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; actually is.&lt;br /&gt;
* If you’re using clang, an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute on some declaration is very close to a usual definition. Just make sure that either the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; is the first declaration or all other declarations are explicitly extern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her&amp;lt;ref&amp;gt;Actually, having an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; on a static symbol leads to that symbol being exposed in the corresponding object file’s symbol table, see [https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/--attribute----alias---variable-attribute ARM’s documentation] for instance. However, I did not find a way to modify the value through that entry in the symbol table as it is a non-global object.&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are hightly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Links ===&lt;br /&gt;
&lt;br /&gt;
* [https://developers.redhat.com/blog/2020/06/02/the-joys-and-perils-of-c-and-c-aliasing-part-1 Series of very well-written articles] by Martin Sebor for RedHat Developer. The [https://developers.redhat.com/blog/2020/06/03/the-joys-and-perils-of-aliasing-in-c-and-c-part-2# second article] also deals with the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html GCC&#039;s documentation on common variable attributes]&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html GCC&#039;s documentation on common function attributes]&lt;br /&gt;
&lt;br /&gt;
====== Footnotes ======&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
[[Category:Compiler Background]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=337</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=337"/>
		<updated>2024-02-21T12:52:48Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: added some relevant links&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;span id=&amp;quot;some-things-i-learned-about-the-alias-attribute&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
= Some things I learned about the alias attribute =&lt;br /&gt;
&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you’ve got some familiarity with C and are simply looking to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute, it’s advisable to skim through the [[#syntax|syntax]] and [[#semantics|semantics]] sections before diving into some more complicated [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll probably find some engaging content in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
Before delving in, it’s crucial to acknowledge (yet again) the portability issues associated with the alias attribute. While it offers some quite powerful capabilities, its usage should be approached with caution, especially in cross-platform projects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntax&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer].&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some adress in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use the address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions and some tentative definitions such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt]. Funny enough, turning the tentative definition &amp;lt;code&amp;gt;int alias_var&amp;lt;/code&amp;gt; into a definition with external linkage (in the standard’s sense, see §6.9.2 in the latest [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf working draft]) as in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
reconciles clang with the code snippet. As already mentioned above, GCC is more forgiving and accepts both variants.&lt;br /&gt;
&lt;br /&gt;
So, what’s the upshot? I guess there are two points:&lt;br /&gt;
&lt;br /&gt;
* GCC and clang don’t necessarily agree on what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; actually is.&lt;br /&gt;
* If you’re using clang, an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute on some declaration is very close to a usual definition. Just make sure that either the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; is the first declaration or all other declarations are explicitly extern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her&amp;lt;ref&amp;gt;Actually, having an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; on a static symbol leads to that symbol being exposed in the corresponding object file’s symbol table, see [https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/--attribute----alias---variable-attribute ARM’s documentation] for instance. However, I did not find a way to modify the value through that entry in the symbol table as it is a non-global object.&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are hightly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Links ===&lt;br /&gt;
&lt;br /&gt;
* [https://developers.redhat.com/blog/2020/06/02/the-joys-and-perils-of-c-and-c-aliasing-part-1 Series of very well-written articles] by Martin Sebor for RedHat Developer. The [https://developers.redhat.com/blog/2020/06/03/the-joys-and-perils-of-aliasing-in-c-and-c-part-2# second article] also deals with the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html GCC&#039;s documentation on common variable attributes]&lt;br /&gt;
* [https://gcc.gnu.org/onlinedocs/gcc/Common-Function-Attributes.html GCC&#039;s documentation on common function attributes]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;br /&gt;
[[Category:Compiler Background]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=332</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=332"/>
		<updated>2024-02-19T16:49:05Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;span id=&amp;quot;some-things-i-learned-about-the-alias-attribute&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
= Some things I learned about the alias attribute =&lt;br /&gt;
&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you’ve got some familiarity with C and are simply looking to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute, it’s advisable to skim through the [[#syntax|syntax]] and [[#semantics|semantics]] sections before diving into some more complicated [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll probably find some engaging content in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
Before delving in, it’s crucial to acknowledge (yet again) the portability issues associated with the alias attribute. While it offers some quite powerful capabilities, its usage should be approached with caution, especially in cross-platform projects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntax&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer].&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some adress in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use the address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions and some tentative definitions such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt]. Funny enough, turning the tentative definition &amp;lt;code&amp;gt;int alias_var&amp;lt;/code&amp;gt; into a definition with external linkage (in the standard’s sense, see §6.9.2 in the latest [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n3047.pdf working draft]) as in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
reconciles clang with the code snippet. As already mentioned above, GCC is more forgiving and accepts both variants.&lt;br /&gt;
&lt;br /&gt;
So, what’s the upshot? I guess there are two points:&lt;br /&gt;
&lt;br /&gt;
* GCC and clang don’t necessarily agree on what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; actually is.&lt;br /&gt;
* If you’re using clang, an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute on some declaration is very close to a usual definition. Just make sure that either the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; is the first declaration or all other declarations are explicitly extern.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her&amp;lt;ref&amp;gt;Actually, having an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; on a static symbol leads to that symbol being exposed in the corresponding object file’s symbol table, see [https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/--attribute----alias---variable-attribute ARM’s documentation] for instance. However, I did not find a way to modify the value through that entry in the symbol table as it is a non-global object.&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are hightly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=331</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=331"/>
		<updated>2024-02-19T14:56:40Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;span id=&amp;quot;some-things-i-learned-about-the-alias-attribute&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
= Some things I learned about the alias attribute =&lt;br /&gt;
&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you’ve got some familiarity with C and are simply looking to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute, it’s advisable to skim through the [[#syntax|syntax]] and [[#semantics|semantics]] sections before diving into some more complicated [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll probably find some engaging content in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
Before delving in, it’s crucial to acknowledge (yet again) the portability issues associated with the alias attribute. While it offers some quite powerful capabilities, its usage should be approached with caution, especially in cross-platform projects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntax&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer].&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some adress in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use the address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her&amp;lt;ref&amp;gt;Actually, having an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; on a static symbol leads to that symbol being exposed in the corresponding object file’s symbol table, see [https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/--attribute----alias---variable-attribute ARM’s documentation] for instance. However, I did not find a way to modify the value through that entry in the symbol table as it is a non-global object.&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are hightly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=330</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=330"/>
		<updated>2024-02-19T14:56:07Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;span id=&amp;quot;some-things-i-learned-about-the-alias-attribute&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
= Some things I learned about the alias attribute =&lt;br /&gt;
&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you’ve got some familiarity with C and are simply looking to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute, it’s advisable to skim through the [[#syntax|syntax]] and [[#semantics|semantics]] sections before diving into some more complicated [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll probably find some engaging content in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
Before delving in, it’s crucial to acknowledge (yet again) the portability issues associated with the alias attribute. While it offers some quite powerful capabilities, its usage should be approached with caution, especially in cross-platform projects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntax&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer].&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some adress in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use the address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memeory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her&amp;lt;ref&amp;gt;Actually, having an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; on a static symbol leads to that symbol being exposed in the corresponding object file’s symbol table, see [https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/--attribute----alias---variable-attribute ARM’s documentation] for instance. However, I did not find a way to modify the value through that entry in the symbol table as it is a non-global object.&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are hightly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=329</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=329"/>
		<updated>2024-02-19T14:55:02Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;span id=&amp;quot;some-things-i-learned-about-the-alias-attribute&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
= Some things I learned about the alias attribute =&lt;br /&gt;
&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you’ve got some familiarity with C and are simply looking to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute, it’s advisable to skim through the [[#syntax|syntax]] and [[#semantics|semantics]] sections before diving into some more complicated [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll probably find some engaging content in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
Before delving in, it’s crucial to acknowledge (yet again) the portability issues associated with the alias attribute. While it offers some quite powerful capabilities, its usage should be approached with caution, especially in cross-platform projects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntax&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer].&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some adress in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memeory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her&amp;lt;ref&amp;gt;Actually, having an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; on a static symbol leads to that symbol being exposed in the corresponding object file’s symbol table, see [https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/--attribute----alias---variable-attribute ARM’s documentation] for instance. However, I did not find a way to modify the value through that entry in the symbol table as it is a non-global object.&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are hightly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=328</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=328"/>
		<updated>2024-02-19T14:48:32Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;span id=&amp;quot;some-things-i-learned-about-the-alias-attribute&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
= Some things I learned about the alias attribute =&lt;br /&gt;
&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you’ve got some familiarity with C and are simply looking to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute, it’s advisable to skim through the [[#syntax|syntax]] and [[#semantics|semantics]] sections before diving into some more complicated [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll probably find some engaging content in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
Before delving in, it’s crucial to acknowledge (yet again) the portability issues associated with the alias attribute. While it offers some quite powerful capabilities, its usage should be approached with caution, especially in cross-platform projects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntax&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer].&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some adress in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memeory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her&amp;lt;ref&amp;gt;Actually, having an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; on a static symbol leads to that symbol being exposed in the corresponding object file’s symbol table, see [https://developer.arm.com/documentation/dui0491/i/Compiler-specific-Features/--attribute----alias---variable-attribute ARM’s documentation] for instance. However, I did not find a way to modify the value through that entry in the symbol table as it is a non-global object.&amp;lt;/ref&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are hightly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=327</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=327"/>
		<updated>2024-02-19T13:14:36Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: &lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;span id=&amp;quot;some-things-i-learned-about-the-alias-attribute&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
= Some things I learned about the alias attribute =&lt;br /&gt;
&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you just want to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in some real world code and are aware of the portability issues, you’ll probably want to skim the sections on &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;’s [[#syntax|syntax]] and [[#semantics|semantics]]. After that, I’d concentrate on the first two [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll almost certainly want to have a look at some bits in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntax&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer].&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some adress in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memeory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own and keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance espressif’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;essentially_const&amp;quot;)]]&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her.&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;a&amp;quot;)]]&lt;br /&gt;
extern uint64_t b;&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]]&lt;br /&gt;
extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are hightly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;s&amp;quot;)]]&lt;br /&gt;
extern uint64_t c;&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=326</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=326"/>
		<updated>2024-02-19T12:17:35Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: fixed lists&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;span id=&amp;quot;some-things-i-learned-about-the-alias-attribute&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
= Some things I learned about the alias attribute =&lt;br /&gt;
&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes:&lt;br /&gt;
&lt;br /&gt;
* If you just want to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in some real world code and are aware of the portability issues, you’ll probably want to skim the sections on &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;’s [[#syntax|syntax]] and [[#semantics|semantics]]. After that, I’d concentrate on the first two [[#examples|examples]].&lt;br /&gt;
* If you enjoy torturing your compiler, you’ll almost certainly want to have a look at some bits in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntax&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer].&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some adress in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memeory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that&lt;br /&gt;
&lt;br /&gt;
* the first column contains the address of a symbol in its segment,&lt;br /&gt;
* the fourth column contains the segment that contains the symbol,&lt;br /&gt;
* the last column contains the name of the symbol.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own. Keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules:&lt;br /&gt;
&lt;br /&gt;
* An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion.&lt;br /&gt;
* If you can, stick to the syntax introduced in C2x.&lt;br /&gt;
* The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance &amp;lt;code&amp;gt;espressif&amp;lt;/code&amp;gt;’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS] for instance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
extern const int const_view [[gnu::alias(&amp;quot;essentially_const&amp;quot;)]];&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her.&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
extern uint64_t b [[gnu::alias(&amp;quot;a&amp;quot;)]];&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
extern uint64_t b [[gnu::alias(&amp;quot;a&amp;quot;)]];&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]] extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are hightly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
extern uint64_t c [[gnu::alias(&amp;quot;s&amp;quot;)]];&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=325</id>
		<title>The alias Attribute - Some things I learned about the alias attribute</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=The_alias_Attribute_-_Some_things_I_learned_about_the_alias_attribute&amp;diff=325"/>
		<updated>2024-02-19T12:07:46Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: initial version&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;span id=&amp;quot;some-things-i-learned-about-the-alias-attribute&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
= Some things I learned about the alias attribute =&lt;br /&gt;
&lt;br /&gt;
Recently, I had to read and modify some code that was responsible for handling &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes in C sources. Although I knew the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute existed, I never had a closer look and thus needed to do some research. This escalated quite a bit and as documentation on attributes in general is rather scarce and the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute [https://clang.llvm.org/docs/AttributeReference.html#alias poses no exception], I’d like to share my findings with you.&lt;br /&gt;
&lt;br /&gt;
These notes begin with some probably rather dull remarks on the syntax of &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes and a subsequent explanation of how the compiler handles these. After having the basics up our sleeve, we have a closer look at some syntactic and semantic peculiarities. The remainder of these notes is then concerned with some cool or fun examples of what one might actually do with aliases. Readers who are easily bored or prefer to learn by example are well-advised to jump directly to these examples and return to the earlier paragraphs only when needed.&lt;br /&gt;
&lt;br /&gt;
So, who’s the intended target audience? To be honest, I don’t exactly know. Obviously, these notes apply to C only and involve some rather low level hackery. Moreover, if you’re not using GCC or clang on some more or less common platform like &amp;lt;code&amp;gt;x86_64&amp;lt;/code&amp;gt;, &amp;lt;code&amp;gt;ARM&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;RISCV&amp;lt;/code&amp;gt;, the probabilities are high that your toolchain does not support the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute at all. Now, if that didn’t put you off already, I can think of two possible ways of reading these notes: * If you just want to use the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in some real world code and are aware of the portability issues, you’ll probably want to skim the sections on &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;’s [[#syntax|syntax]] and [[#semantics|semantics]]. After that, I’d concentrate on the first two [[#examples|examples]]. * If you enjoy torturing your compiler, you’ll almost certainly want to have a look at some bits in the [[#testing-out-the-limits|section on testing the limits]] and some of the later [[#examples|examples]].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntax&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Syntax ==&lt;br /&gt;
&lt;br /&gt;
Owing to the fact that attributes like the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute have their origin in vendor-specific extensions to the C language, there are several ways to actually define an alias. We’ll only deal with two variants but the interested reader may find a third variant by looking [https://releases.llvm.org/7.0.1/tools/clang/docs/LanguageExtensions.html?highlight=apply_to#specifying-an-attribute-for-multiple-declarations-pragma-clang-attribute here]. Moreover, not every syntax is compatible with every compiler or even compiler settings, and different compilers do not necessarily agree upon the way in which certain attributes interoperate with standard C features. We’ll see examples below, so stay tuned.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;gnu-c&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== GNU C ===&lt;br /&gt;
&lt;br /&gt;
The GCC compiler originally introduced the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute in GNU C using its &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt; syntax. According to [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation], an attribute specifier is of the form&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;__attribute__(( attribute specifier list ))&amp;lt;/pre&amp;gt;&lt;br /&gt;
where the attribute specifier list is going to consist of the single attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt&amp;amp;quot;)&amp;lt;/code&amp;gt; for the greater part of these notes. In principle, such attributes could be attached to anything in your sources. However, the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute applies only to variable and function declarations. In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt1&amp;quot;)))&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
__attribute__((alias(&amp;quot;alias_tgt2&amp;quot;)))&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt1&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; whereas the attribute &amp;lt;code&amp;gt;alias(&amp;amp;quot;alias_tgt2&amp;amp;quot;)&amp;lt;/code&amp;gt; applies to the declaration of &amp;lt;code&amp;gt;alias_fun()&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
We’ll look into what exactly these &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes mean, but for the moment it suffices to think of these declarations as introducing an additional name &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; for the previously defined variable &amp;lt;code&amp;gt;alias_tgt1&amp;lt;/code&amp;gt; and an additional name &amp;lt;code&amp;gt;alias_fun&amp;lt;/code&amp;gt; for the previously defined function &amp;lt;code&amp;gt;alias_tgt2&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;c2x&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== C2x ===&lt;br /&gt;
&lt;br /&gt;
Now, the reader might know very well that the attribute syntax&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ namespace::attribute ]]&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
introduced in C++11 is to be included in the upcoming C2x standard. If you want to have a look, the draft on attributes can be found [https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2335.pdf here].&lt;br /&gt;
&lt;br /&gt;
As the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute originated in GNU C, the &amp;lt;code&amp;gt;namespace&amp;lt;/code&amp;gt; is going to be &amp;lt;code&amp;gt;gnu&amp;lt;/code&amp;gt; in our case of interest. Hence, in C2x our first example of aliases from above could also be written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;[[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]]&lt;br /&gt;
extern void alias_fun();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and both clang and GCC happily accept this syntax, as witnessed by [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJ0hwBk8DTAA5dwAjTGIQAHZSAAdUBUJbBmc3Dy9Y%2BMSBPwDgljCI6MtMayShAiZiAhT3TykLTCsbAXLKghyg0PCoiwqqmrT6hT72/0787siASgtUV2Jkdg5/AgBqMTwmBWMCYAIuAFIAZgAhA40AQQA3VDx0ddpN7d2CACYIKdWDyLPIgBFzhdAQcAKxnMGrYAMVwgEAbLYQA6vV7w557Q7Iz6ggEggGXTCqAjhBirFYPJ7GK6VY5nS7AsGgk6Q6Gw1GI5GonZ7V5I15Y3HYwEEonEEk3O7krbGKiuBgfGkcGa0Tgg3ieDhaUioTiOVYKOYLTBfV5HHikAiaRUzADWICORwAdAAOABsGi4GkirxdTq4TqdkRd%2Bk4kjVlq1nF4ChAGnNlpmcFgMEQKFQLBidHC5EoaHTmYiyGQBiMO2IsutfDoIujEBC4ZC/kqAE9OGaG8xiE2APIhbQlC3cXi5tiCLsMWgtjW8LAhVzARxiWjRwekLAsQzAcRT1d4Yj9vBXTDLzUEkquImt3grRrhx4hYjN5xYcMEYh4FiXmZUAzABQANTwTAAHcuxiRhLxkQQRDEdh6n4QRFBUdRt10Lh9A3EBTGMcw72jSAZlQGJmgYZcAFpkFWUjhnQY4/mQE1eFQQ9iDfLA8I%2BBomiSewGCcFxaj0XwxjyAo9DiBJiIGOoMgkpIOhE7o0OKUoWhGKS9GU4jWiqeSugiJS1P4wZejaXSJn0mZ9XmRYJCVFUw23bUOFWVRXVIl1JFWIsN1WCBX3LT4IFwQgSGNU0pl4ActCmG07VeZ0NBdEEQQATiOFKnUkSJIhS15gw4UNSA/JKHS4P0Uq4V4cq9J08vVTUnKjGM4ynBNkwgJA5gIGJz2zCBcwzehiECVgllcl13M87yjFWLhIgdDRSt4TB8CIVi9Hg4RRHEODZEQtRw1Q0ggIfGJP3y1VSHqxjOC7c8erWVAqBctyPK84tgF8/yGGtQLnDzIawq4CKWuimYEEwJgsAiDjlQK3gPyOSRnV9DQA1eDQqr9d0rvDRqLGaqKrVIW0jnip1EuStKMqynK8rho4HIayNQcVUhEygdqkAG/M%2Bp5oaUGALgycrWhq0oOtt3bZsIOlzsez7awIOHRgCDHCdwxnOcF1oJcILXDct01fA91KQ9j2W1QzwvFdrzhzU7wfTsnyWTVX3fc7vyYX8AOA0DwJXTboJ26RNv25DNV0emMKwnC8BCdiCKIpIyOQRjmNYo94Esxp9zsCAHHUtChNyPSxMySSjOk8SsgYMzRKU3OVIYbTqirjSm60kZ68Ukz%2BnbgzTOEsvgdmazYLsjhLuuiNnPGybIULWbHVeXzgrWoGQaJmKSbtR17QPw%2Bj/ywriq4B0vSkDRJCODQjhBD1stxxyWejWNt/ZrmQC6h6%2BbTQbwgjTYJweeb1gBL2Fg6V4y1VokDuBtWQwdYKhz2koA6KEQB5ROkwM6g5J7Tzxrde655VhPRehNMBECV6%2BX%2BgA4gYU%2BSRXjODSG0NKCT1PnaJ0UDIhOlSiCF0KUPJOlykcZ%2BzMOBNXfsw3e9oHRHwUQffKjNxE3UkazHecNoGqNnkw1qMxmIJDsJIIAA compiler explorer].&lt;br /&gt;
&lt;br /&gt;
What is more, the proposal comes with clear and precise rules, where attributes can appear. It tells us that C2x will&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;[...] allow an attribute specifier to appear to the left of a&lt;br /&gt;
declaration so that the attributes appertain to all of the declarators in&lt;br /&gt;
the declaration list, or to appear to the right of all declaration&lt;br /&gt;
specifiers so that the attributes appertain to the type determined by the&lt;br /&gt;
specifier sequence.&lt;br /&gt;
&lt;br /&gt;
[...]&lt;br /&gt;
&lt;br /&gt;
Similarly, an attribute specifier can appear to the right of a type in a&lt;br /&gt;
declarator to appertain to the type, or to the right of an identifier in a&lt;br /&gt;
declarator to appertain to the identifier declared.&amp;lt;/pre&amp;gt;&lt;br /&gt;
So, our above example might have also been written as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int alias_var [[ gnu::alias(&amp;quot;alias_tgt1&amp;quot;) ]];&lt;br /&gt;
&lt;br /&gt;
extern void alias_fun [[ gnu::alias(&amp;quot;alias_tgt2&amp;quot;) ]] ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and, again, both clang and GCC happily accept this syntax. This can also be checked on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQAVgDspBwAyeAyYAHLuAEaYxCAAbKQADqgKhLYMzm4e3n5JKTYCQSHhLFEx8ZaY1mlCBEzEBBnunr4WmFb5DDV1BIVhkdFxFrX1jVktCsM9wX0lA7EAlBaorsTI7BzBBADUYnhMCsYEwARcAKQAzABCpxoAggBuqHjoO7R7B0cEAEwQ81unPmuPgAIjdbmDMKoCNEGFtNq93sZ7nV/l5rmitsAGK4QCBdvsIKcvl98R9jmdiX9Tl5QTSLtc7hCoTCto9ngj9sYqK5YdT0ZdMdjcaTCcTSYdjl8iV8qXSaVtfvSOItaJwvLxPBwtKRUJxHFsFMtVph/l9zjxSARNMrFgBrEDnc4AOgAHLENFwND4vrEXVwXS6fPFVRxJBrrTrOLwFCANJbrYs4LAYIgUKgWAk6NFyJQ0BmszFkMgDEZDsQeba%2BHRocQYxAIhGIsE6gBPTgWpvMYgtgDyEW0lSt3F4ebYgh7DFoba1vCwEVcwEcYloMeHpCwLEMwHEM/XeGIg7w90wq%2B1kMqrmh7d4mzaEbeEWIrecWAjBGIeBY18WVAMwAUABqeCYAA7j2CSMNeMiCCIYjsFI0HyEoagRroXD6FuICmMY5gPjGkCLKgCQdKuAC0yBbKREzoBcwLIGavCoMexAflg%2BG/K07RpPYDBOC4TR6IE0zFKUei5KkAijJ46HiR0vQiQM6EVFUAhdCM/FZEpbSHqpkzyf0MRKZMUl6BM3T6bMhmLIaKxrBIKpquGu66hwWyqG6pGxJIWzFluCrvhWfwQLghAkKa5rzLwQ5aPMdoOl8roaLEXheAAnOcqUupIPg%2BKlXz6JwYakF%2ByVOlw/qpVwXy5d6Lr5Zq2oudGsbxjOiYphASDLAQCSXjmEB5pm9DEKErDrO5sSed5vlGFsXA%2BE6GhlbwmD4EQrF6PwMGiOICFbUhKjqLuaGkCBT4JN%2BBUcOqpANYxnA9pevXbKgVBuR5Xk%2BSWwD%2BeWDC2kFzj5sN4VcJFrUxYsCCYEwWAxBxIZFV%2B5ySK6foaIGXwaNV/oerdEZNRYLXRTapD2ucCUuklKXpZl2W5flIbnE5jVRhDyqkEmUAdUgg0Fv1fPDSgwBcBTVa0DWdYNrunatlBsvdn2A7WFBo6MAQE5ThGc4LkutArlBG5bju2r4AeVTHqeK2qBeV5rreIbag%2BT7di%2B6zau%2Bn6Xb%2BTD/kBoHgZBa77bBu3SPtiiHahICM5h2G4XgETsYRxFpGRyCMcxrEnvA1naSpngQA4JnoUJRQGWJyQSekGnSYkVdycJFdaVxundCXnE6Z0elN5ZpnGbX/fmb3olg0stnwQ510s/drkTVNmJFnNzpfAqIXraD4Mk7FZMOs6joH4fR9XUjcRcE63pSBokjnBo5xeJ6OX485bMxnG2%2BczzIDdc9AvpkN0RRpsE4PPT6wAl6iydF8Faa0SDPE2rIUO8Fw6yEjihY6MdTrnUuiGG6d1IwcEej1S8WxXrvUmmAiBK8FRAwAcQcKMoooJihjDOGlAp6n3OC6KBPgXRpS8LEVKXkXR5XOM/VmHBmrv2YbvR0Toj4KIPldZm4jZ5MLalPaBqiCHqMhqQZiKQ7CSCAA%3D%3D compiler explorer].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;semantics&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Semantics ==&lt;br /&gt;
&lt;br /&gt;
In this section, we explain what the compiler makes of an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute and give some first working examples. However, in order to explain how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute really works, we first need to recall some simple facts about symbol tables in object files.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;symbol-tables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Symbol tables ===&lt;br /&gt;
&lt;br /&gt;
&#039;&#039;&#039;Disclaimer&#039;&#039;&#039;: While preparing these notes, the author was working on a GNU/Linux machine and while the specifics given below do not necessarily apply verbatim to your toolchain, the underlying mechanisms are almost certainly the same.&lt;br /&gt;
&lt;br /&gt;
Whenever you declare a global variable or a function in one of your C sources, it ends up as an entry in the corresponding object file’s symbol tables, so that other parts of your program may use it. There is a symbol table for executable code, i.e. functions, called &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; and there are symbol tables for initialized and uninitialized data called &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
An entry in the symbol table simply tells the linker what a specific name in your program means. That is, an entry in the symbol tables either maps an identifier to a specific address in one of the segments of your object file, or is marked as &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; to tell the linker that this symbol must be defined in some other object file or library.&lt;br /&gt;
&lt;br /&gt;
If you have one module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; defining a global variable &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; and another module &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; using that global variable like in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
int global = 23;&lt;br /&gt;
&lt;br /&gt;
// b.c&lt;br /&gt;
extern int global;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
then the symbol table of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt; will contain an entry mapping &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; to some adress in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment and the symbol table of &amp;lt;code&amp;gt;b.o&amp;lt;/code&amp;gt; will contain an &amp;lt;code&amp;gt;UNDEFINED&amp;lt;/code&amp;gt; entry for &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt;. The linker is then responsible for merging the symbol tables and making the code in &amp;lt;code&amp;gt;b.c&amp;lt;/code&amp;gt; actually use address of &amp;lt;code&amp;gt;global&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.data&amp;lt;/code&amp;gt; segment of &amp;lt;code&amp;gt;a.o&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
The main point I want to make here is that names of functions and global variables are merely entries in some symbol tables that eventually map to specific addresses in memeory. This means in particular that two names mapping to the same address will be indistinguishable after compilation and linking because a processor does not know of any names but works with memory addresses only.&lt;br /&gt;
&lt;br /&gt;
Actually, this last paragraph fully explains how the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute works.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-variables&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with variables ===&lt;br /&gt;
&lt;br /&gt;
Let’s get our hands dirty and try to understand the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;-attributes by investigating the following simple example:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// simple.c&lt;br /&gt;
int alias_tgt;&lt;br /&gt;
extern int alias_var [[gnu::alias(&amp;quot;alias_tgt&amp;quot;)]];&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Compiling these two lines and looking at the symbol table of the resulting object file by invoking &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;nm&amp;lt;/code&amp;gt;, reveals what happens inside your compiler:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms simple.o&lt;br /&gt;
&lt;br /&gt;
simple.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 simple.c&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_tgt&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000004 alias_var&amp;lt;/pre&amp;gt;&lt;br /&gt;
Don’t worry, if you don’t know how to read the output of &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;. For our purposes, you only need to know that * the first column contains the address of a symbol in its segment, * the fourth column contains the segment that contains the symbol, * the last column contains the name of the symbol.&lt;br /&gt;
&lt;br /&gt;
In our simple example, we thus find two symbols &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; at address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt; of the segment &amp;lt;code&amp;gt;.bss&amp;lt;/code&amp;gt; for uninitialized data. Once loaded into memory by your operating system or a boot loader, the variables &amp;lt;code&amp;gt;alias_tgt&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from &amp;lt;code&amp;gt;simple.c&amp;lt;/code&amp;gt; will hence refer to the same actual address in memory and are indistinguishable from your computer’s point of view.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-simple-example-with-functions&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A simple example with functions ===&lt;br /&gt;
&lt;br /&gt;
The alias attribute does not only apply to variable symbols but also to function symbols. Compiling the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// fun.c&lt;br /&gt;
&lt;br /&gt;
int fn(int a) {&lt;br /&gt;
  return a  + a;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;fn&amp;quot;)]] int twice(int);&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
and investigating the resulting object file with &amp;lt;code&amp;gt;objdump&amp;lt;/code&amp;gt;, we get the result&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; objdump --syms fun.o&lt;br /&gt;
fun.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 fun.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e fn&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e twice&amp;lt;/pre&amp;gt;&lt;br /&gt;
Again, we have two symbols &amp;lt;code&amp;gt;fn&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;twice&amp;lt;/code&amp;gt; in the &amp;lt;code&amp;gt;.text&amp;lt;/code&amp;gt; section of our object file that share the single address &amp;lt;code&amp;gt;0&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;testing-out-the-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Testing out the limits ==&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;what-exactly-is-an-alias&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== What exactly is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;? ===&lt;br /&gt;
&lt;br /&gt;
We now know more or less what the compiler does when it encounters an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. However, it is not so clear what an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute means in terms of the C language. Essentially the only official documentation on the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute is GCC’s [https://gcc.gnu.org/onlinedocs/gcc/Common-Variable-Attributes.html documentation] of which I’ll quote the relevant first half:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;The alias variable attribute causes the declaration to be emitted as an alias for another symbol known as an alias target. Except for top-level qualifiers the alias target must have the same type as the alias. For instance, the following&lt;br /&gt;
&lt;br /&gt;
int var_target;&lt;br /&gt;
extern int __attribute__ ((alias (&amp;amp;quot;var_target&amp;amp;quot;))) var_alias;&lt;br /&gt;
&lt;br /&gt;
defines var_alias to be an alias for the var_target variable. &lt;br /&gt;
&lt;br /&gt;
It is an error if the alias target is not defined in the same translation unit as the alias. &amp;lt;/pre&amp;gt;&lt;br /&gt;
One question that remains unanswered by this explanation is the question whether an alias is a definition in the standard committee’s sense. In fact, this is the question that originally initiated my investigations into the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute.&lt;br /&gt;
&lt;br /&gt;
Without further ado, let’s see what GCC and clang have to say. The snippet&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int target;&lt;br /&gt;
&lt;br /&gt;
[[ gnu::alias(&amp;quot;target&amp;quot;)]]&lt;br /&gt;
extern int alias_var;&lt;br /&gt;
&lt;br /&gt;
int alias_var;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is happily accepted by both GCC and clang. However, if we turn the second declaration of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; from a tentative definition into a certain definition by changing the second declaration to&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int alias_var = 0;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
the two compilers start to disagree. In fact, clang complains about a redefinition of symbol &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt; while GCC tells us nothing. See for [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMJAVlIOAMngMmABy7gBGmMQgAOykAA6oCoS2DM5uHt7xickCAUGhLBFRsZaY1ilCBEzEBGnunlw%2BpeUCldUEeSHhkTEWVTV1GY197Z0FRTEAlBaorsTI7BwApBoAgoEEANT9wJgESwDMAEIrq6dLXieXm8AMriAgYnhMChBLAEzvO3sf75MXABFAadMKoCJEGJsNpsni9Dic1qdobCFJtDgDNhp4RxprROF5eJ4OFpSKhOI5NgpZvNMGj3gceKQCJocdMANYgA4HAB0AA4AGwaLgaaLvfm8ri83nRfn6TiSQks0mcXgKEAaJks6ZwWAwRAoVAsOJ0SLkShoI0mqLIZAGIzGAjEVwMNl8Ojg4hqiBhJVhQLVACenEZfuYxADAHkwtoyszuLwLWxBBGGLQg8TeFgwq5gI4xLQ1fHSFgWIZgOIM8W8MRY3gAG6YQsk0FlVzg4O8DaYPGV2h4MLEQPOLBKx14Fgd6ZUAzABQANTwmAA7hG4owOzJBCIxOwpJv5Eo1ErdFx9GWQKZjOY%2B2E1ZBpqg4jYBIWALTITavhQEdDo5D03hUAbYhiDwLA7wgaZmmfTwIAcQYGl8Bh0DGboolPBIkhghC9EwnIGFQwoelPaCKn6WoXHqPRSNacjCImEjyJwxjRkCLoiPQqDqQWCRcXxRVKzJDhNlUAVX35SRNltMtNggR1nTZSZZNwQgSDpBlJl4OMtEmdlOXePkNH5LwvAATgOUzeUkaJolM945Q4BVSAnYzuS4SVTK4d5bNFXl7KJEkhNVdVNQzbU9QgJBZgIOI2zNCALWNehiGCVhFlE/lxMk6SjE2Lhom5DQ3N4TB8CIUD0D0fgt1EcQ92qg8VHUSsT1IJdBziScHIJUgAsAzgIzbWKtlQKgRLEiSpLtYBZPkl0lIgZxLWS9SuE00KdOmBBMCYLAokghynInA5JD5CUNGld4NG8yUhV6pUgosELtNZUgOQOAzeSMkzzMs6zbPsnsDgEwKVQ2nFSB1KAIqQRKrXiuHkpQYAuA%2Bt1aA9L0fUrUNAw3XHwyjGNrA3RNGAIFM0yVLMczzWgCw3EsywrEl8BrcoGybErVFbdsiy7HsSRvQdw2HRYSTHCd4ynGd50XFc1yJRkGu3OrpAaxQmuPEBAfPS9r37CCHyfFI32QQDgIqxt4Cg7tazsODkOYpCULY8ZiKyLCUmdvCYPoj2aIYNoBkooYLDtlog7ot20OopjQ8Q79WPyWO1pmOYeLTnser65VhIyrKbhtPKeXeZSyrUj4NK0rU9K5bkuUbpvm8O3gXK4blRSkDRJAODQDi8YUbPuwSwbVDUXvC%2BBIpAaLhoRw0ksiVK2E4AvJuAYvUe5d4SoriqqtkVXd3V2RNaPFqdbajquuzkH%2Bo4QaYrbTZRvGzKN630vZKWpfiHUv4NcwpbR2ntSgfFHJt05LyHe0ReRmS8PyUyEleR2QOCPUGHBgoT1rm9TkPJm6EMbg5YGGCH5AM2g5XeZC84UNesBJIdhJBAA%3D%3D yourself]. When you’re at it, you may check that GCC also accepts the alias after a definition of &amp;lt;code&amp;gt;alias_var&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Now, it seems that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition and thus rightly complains about a symbol redefinition. But what does GCC do? Well, a quick inspection of the symbol table tells us that the alias seems to override any other variable definition that exists. There’s also this old [https://gcc.gnu.org/bugzilla/show_bug.cgi?id=91765 bug report] for GCC that seems related but never got any activity.&lt;br /&gt;
&lt;br /&gt;
By the way, when it comes to functions, both clang and GCC seem to treat an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute as a function definition and therefore complain about redefinitions.&lt;br /&gt;
&lt;br /&gt;
Returning to variables, there’s one last surprise. Above, we noted that it &#039;&#039;seems&#039;&#039; that clang treats the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; as a symbol definition. However, that’s not completely true. Whereas a definition may appear after any number of tentative definitions, clang does not allow this for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definitions as can also be seen on [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYM9DgDJ4GmADl3ACNMYhAAVlIAB1QFQlsGZzcPPVj4mwFffyCWUPCoy0xrRKECJmICZPdPLgtMK0yGMoqCbMCQsMiLcsrq1LqFXra/DryuiIBKC1RXYmR2DgBSDQBBPwIAal7gTAIlgGYAIRXV043NsTwmBWMANwrDk7XTpYiT983gBlcQECubhAlgAmYE7PYg4GTN4AEVhp0wqgIYQYmwuANuD2ITw401onAivE8HC0pFQnEcmwUs3mmE2IIOPFIBE0uOmAGsQAcDgA6CIATgAbAB2YEaA4aDTCwVcDRRfEcSRE1lkzi8BQgDTM1nTOCwGCIFCoFjROhhciUNAms3hZDIAxGYwEYiuBjsvh0ZHEDUQYIq4J%2BCoAT04TIDzGIQYA8sFtMUWdxeFa2IIowxaCGSbwsMFXMBHGJaBrE6QsCxDMBxFnS3hiPG8HdMMXSYjiq5kaHeBsGiraHhgsRg84sCrnXgWJ3plQDMAFAA1PCYADuUeijE7MkEIjE7Ckm/kSjUKt0dQdJjM%2Bn7Gsg01Q0SaxYAtMhNo%2BhuhDjDkMCDrxUI3iGIPAsGvCBpiKEo7AgBx%2BlqUgfFGXJ8jSOIEgEWCUIyRJ2iQrpBgaesBBaPoXBqPQIKaYiRhyTpwkGYYMPo1ocNoiRwJpBY2P0AllWrckOE2VQAA5BUfQVJE2e0K02CBnVddlJhk3BCBIekfy4SZeATLRJg5LlgR5ISNEFCIBQOfkhMkYVhX5YFuMVXgJwiQUeSkSQImFITGQOYEIglPdiVJfj1U1bUs11A0ICQWYCGidsLQgK1TXoYgAlYRZhNE8TJLPTYuGFHkNFc3hMHwIggPQPR%2BC3URxD3aqDxUdRqxPUgl0HaJJ3swlSECv9OCjds4q2VAqEEkSxIkqSjBkuS3UUiBnGtFK1MZTSwp06YEEwJgsHCMD7KVUgJ0kXkhNMyRgS4QUROFSRBQswVepVYKLFC7S2VITluUKh7%2BXug4TP5OVBUB%2Bzf2evi1Q23FSD1KBIqQJKbQS5GUpQYAuB8j1aC9H0/WrcNgw3InIxjONrA3ZNGAINMMxVHM8wLWgiw3MsKyrUl8DrEpG2bErVDbDsS27BVST7AchwwRZSTHCdEynGd50XFc12JJkGu3OrpAaxQmuPEA7LPEBTGMcwJdA2970SYs/wAiqm3gcCCMgzxoIYJxSIGIT4I9ljxnCOz0jQpIvdqH3g6af3kLsijSgYsOJB9uOiOGaOuljhOUnDnpmMQ1ioRmOZOI07reKCzhxqyiTgDtPLeWBJSytUhkNK0nU9J%2B7lu573vDscyItT61UOBCrUPoi%2BAopAGLhtR41krCNK2E4TLJq%2BOusZ5YESubiqqtkLXdx12Q9aPFrDbajquoVHrh/4wbYvbTZRqr9fa5fLfG8WhebVWqF27hS2jtPalA8ScCOk5IeL1oYanHh3L6XJeS9xQd3cG5d%2BqjxhrpeyO9IYVywRPaYAF4h2EkEAA godbolt].&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;syntactic-limits&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Syntactic limits ===&lt;br /&gt;
&lt;br /&gt;
The reader may have observed that while the clear rules for the attribute syntax in C2x were [[#c2x|highlighted above]], there was no mention of any rules in the case of GCC’s original syntax. There’s a reason for that and in order to set the stage, let me quote from [https://gcc.gnu.org/onlinedocs/gcc/Attribute-Syntax.html GCC’s documentation] on its attribute syntax:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;For compatibility with existing code written for compiler versions that did not implement attributes on nested declarators, some laxity is allowed in the placing of attributes.&amp;lt;/pre&amp;gt;&lt;br /&gt;
Even though we’re not dealing with any nested declarators here, let’s see what GCC and clang are able to swallow. In fact, all four declarations in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern int alias_var1;&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) int alias_var2;&lt;br /&gt;
extern int __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_var3;&lt;br /&gt;
extern int alias_var4 __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
are happily accepted by both GCC and clang and turn out to be semantically equivalent. For function aliases there are even more positions where one could place the &amp;lt;code&amp;gt;__attribute__&amp;lt;/code&amp;gt;. Of these, the correct C2x variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) ();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is rejected by both GCC and clang, and the variant&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern void alias_fun (__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is of course interpreted as an attribute for parameter declarations. Thus, just as in the variable case, we are left with four valid and semantically equivalent variants to define a function alias:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) extern void alias_fun1();&lt;br /&gt;
extern __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) void alias_fun2();&lt;br /&gt;
extern void __attribute__((alias(&amp;quot;alias_tgt&amp;quot;))) alias_fun3();&lt;br /&gt;
extern void alias_fun4() __attribute__((alias(&amp;quot;alias_tgt&amp;quot;)));&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Note that for functions like &amp;lt;code&amp;gt;int* fn()&amp;lt;/code&amp;gt; returning a pointer, there are even more variations of a function &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; definition. Rest assured that in this case, too, GCC and clang swallow whatever you may come up with except for the two cases already excluded above.&lt;br /&gt;
&lt;br /&gt;
However, one step further, GCC and clang start to disagree on what monstrosities are still acceptable. The alias &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt; in the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;int** ptr = 0;&lt;br /&gt;
int** target() {&lt;br /&gt;
  return ptr;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
extern int * __attribute__((alias(&amp;quot;target&amp;quot;))) * inbetween();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
is still accepted by clang while GCC has problems parsing the corresponding line and consequently does not export a function called &amp;lt;code&amp;gt;inbetween&amp;lt;/code&amp;gt;. You can see the warning in [https://godbolt.org/#z:OYLghAFBqd5QCxAYwPYBMCmBRdBLAF1QCcAaPECAMzwBtMA7AQwFtMQByARg9KtQYEAysib0QXACx8BBAKoBnTAAUAHpwAMvAFYTStJg1AB9U8lJL6yAngGVG6AMKpaAVxYMQANi6kHAGTwGTAA5dwAjTGIQAHZSAAdUBUJbBmc3D29fROSbAUDgsJZI6LjLTGtUoQImYgJ0908fC0wrPIZq2oIC0Iio2IsauobM5oUh7qDe4v6YgEoLVFdiZHYOIIIAKk2AaniCYh2AUgBmABEdjVOAISONAEE7%2B43tnaHgTAIIOeOY24edjtiJ9lgw9gcbk8jjEzlCHphVAQomCNjtdqYmAQDnhwq4kaYoGI8EwFBAjgAmcnvT4U8lzelonZBSIEADumEY3xuHAWtE4AFZeJ4OFpSKhOI4dgolitMMdySceKQCJoeQsANYgE4nAB0AA4vBouBoYuSvHquHq9TEvPpOJIhaqxZxeAoQBplaqFnBYDBEChUCx4nQouRKGggyHoshkAYjMYDq4GOq%2BHQkcQ3RBwk7wkFagBPThK3PMYj5gDy4W0FRV3F4EbYgnLDFohZFvCwuOAjjEtDdddIWBYhmA4nbg7wwMqADdMP3RQiKni1kqNq0nbQccQC84sE7sSwizy%2BAZgAoAGp4TCs8vxRhHmSCERidhSR/yJRqJ26XxxkxmfQcTdSAFlQfZUn7ABaZAdkg8Z0FOM5kAVXhUFnYhiDwLBgO%2BFo2lSewGCcFxGj0AIpiKEo9ByFIBBGTxsiSWiGB6Sj%2Bl8cpKgETphhIzIONaGsqgmVi%2BmiDiJnovRxi6USZnEhZpWWVYJF5AVHXHcUOB2VQDUgrxJB2WMRx2CBE2TH4IFwQgSHlRU5l4WstHpUhNROcl9Q0Lx%2BX5ABOE5fL1SQYhiXzyTtDgHVIQ9vJ1LhLV8rhyVC009XC4VRS0113U9dtvT9CAkCWAh4jxMMIAjYN6GIEJWDWXSvH0wzjKMHYuBiHUNDi3hMHwIhMPQPR%2BCfURxDfYaPxUdRxx/UhWW3eIjzUjhBVIDLUM4cs8VKggdlQKgdL0gyjL/UzzPVSznEjaq7K4BzcuchYEEwJgsGiXC%2BUi3hDxOSR9QtDRrXJDRkstI01qdLKLBypy1VcrUPL1LyfP8wLgtC8LPpODTMpdB7jx9KACqQSqo3K0nqpQYAuHc1NaHTTNs3HEsCwfFmy0ratrAfBtGAIZtWydTtXG7Xt%2ByVIcRzHUV8CnGxZ3nHrVCXJEHzXT7RU3cJtzLXc1lFA8lpPJgz0va9b3vAcJufMbpAmxQpu/EBMZHEBTGMcwtZw0DwIEKDkFQ9CBrneBFMErjPAgBwpN8cjCjE6imPaGOEiT1I5KogT8O4yS%2BIYvChJz2SKITiSuhTmS6gz9jFJlFS7oi1b1udbSGqanZgBjNrdXJUzrP6277thly3N1bVx4nyeIqimKuB1U0pA0SQTg0E5%2BWNEKIc0vG3Q9YfSEJwqQGKnbycDKqolqthODb47O5gmmdXJHq%2BpILChtkG3Xzt2QHa/GbnZzQWkbT6TdIabW2niPaB1b6GXvt3J%2BpkroX0OBSdyQ8vRPRem9Sgy0Z5aj1E/GIeo/L8i8L5AyeowonC3rjDg2U96YPhtqHUk82HjwitjWhG16H4xcp9Z%2B3CW6OSYehZIdhJBAA%3D%3D compiler explorer] and either investigate the symbol tables on your own or have a look at the output on my machine:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; clang -c -std=c2x between.c&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e target&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000e inbetween&amp;lt;/pre&amp;gt;&lt;br /&gt;
and&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; gcc -c -std=c2x between.c&lt;br /&gt;
between.c:6:1: warning: ‘alias’ attribute does not apply to types [-Wattributes]&lt;br /&gt;
6 | extern int * __attribute__((alias(&amp;amp;quot;target&amp;amp;quot;))) * inbetween();&lt;br /&gt;
  | ^~~~~~&lt;br /&gt;
&amp;amp;gt; objdump --syms between.o&lt;br /&gt;
between.o:     file format elf64-x86-64&lt;br /&gt;
SYMBOL TABLE:&lt;br /&gt;
0000000000000000 l    df *ABS*  0000000000000000 between.c&lt;br /&gt;
0000000000000000 l    d  .text  0000000000000000 .text&lt;br /&gt;
0000000000000000 g     O .bss   0000000000000008 ptr&lt;br /&gt;
0000000000000000 g     F .text  000000000000000d target&amp;lt;/pre&amp;gt;&lt;br /&gt;
&amp;lt;span id=&amp;quot;a-word-of-warning&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== A Word of Warning ===&lt;br /&gt;
&lt;br /&gt;
Even though the beginning of the preceding paragraph might have left you with the impression that we may place &amp;lt;code&amp;gt;__attribute__((alias=&amp;amp;quot;target&amp;amp;quot;))&amp;lt;/code&amp;gt; essentially wherever we want, the placement does matter for a declaration list. In&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;extern int a, b __attribute__((alias(&amp;quot;target&amp;quot;))) ;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
only &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; is an &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt; while in&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;__attribute__((alias(&amp;quot;target&amp;quot;)))&lt;br /&gt;
extern int a, b;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
both &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; are &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt;es for &amp;lt;code&amp;gt;target&amp;lt;/code&amp;gt;. If you don’t believe me, just have a look at the symbol tables on your own. Keep in mind that this remark applies to the new C2x syntax, too.&lt;br /&gt;
&lt;br /&gt;
So, if I’d be pressed to write guidelines for &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attributes, I’d suggest the following simple rules: * An &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; declaration should consist of a single declarator to avoid any confusion. * If you can, stick to the syntax introduced in C2x. * The attribute annotations have to be placed in front of the declaration like in our very first examples for the [[#gnu-c|GNU]] and [[#c2x|C2x syntax]] as this syntax seems to reliably work with both GCC and clang.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;examples&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
== Examples ==&lt;br /&gt;
&lt;br /&gt;
We’ll give several slightly more involved examples of the &amp;lt;code&amp;gt;alias&amp;lt;/code&amp;gt; attribute. The first two of these examples are real-world examples. The other examples again more or less classify as &#039;&#039;testing the limits&#039;&#039;.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;aliases-as-portability-and-linking-hacks&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Aliases as portability and linking hacks ===&lt;br /&gt;
&lt;br /&gt;
One problem where aliases might come handy is offering a specific API without renaming all your functions or offering different implementations of an API and switching between these implementations by changing aliases, e.g. with guarding &amp;lt;code&amp;gt;#ifdef&amp;lt;/code&amp;gt;s. Among other examples for this use case, searching on github for &amp;lt;code&amp;gt;__attribute__((alias&amp;lt;/code&amp;gt; yields for instance &amp;lt;code&amp;gt;espressif&amp;lt;/code&amp;gt;’s [https://github.com/espressif/esp-idf/blob/f68c131e5603feca21659e92ad85f0c3369692fe/components/vfs/vfs.c#L1316 implementation of VFS] for instance.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;software-engineering-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Software engineering with aliases ===&lt;br /&gt;
&lt;br /&gt;
Another nice trick that crucially employs aliases is more related to software engineering than to linking problems: Imagine a situation, where one module of your software owns a global variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; that is computed once during startup but constant afterwards. If &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is exposed as a global variable, then - rather sooner than later - some rogue developer on your team will introduce a function that modifies &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; and thereby introduce a hard-to-find bug&amp;lt;ref&amp;gt;With some non-negligible probablity that rogue developer is you!&amp;lt;/ref&amp;gt;. After all, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is a non-constant global variable and modifying it should be OK, right?&lt;br /&gt;
&lt;br /&gt;
Let us indicate a neat solution for this problem that makes good use of aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// a.c&lt;br /&gt;
static int essentially_const;&lt;br /&gt;
extern const int const_view [[gnu::alias(&amp;quot;essentially_const&amp;quot;)]];&lt;br /&gt;
&lt;br /&gt;
void init() {&lt;br /&gt;
  essentially_const = 42;&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
// a.h&lt;br /&gt;
extern const int const_view;&lt;br /&gt;
&lt;br /&gt;
void init();&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Let’s see what’s happening here:&lt;br /&gt;
&lt;br /&gt;
* The variable &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; in &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; is declared non-constant and the function &amp;lt;code&amp;gt;init()&amp;lt;/code&amp;gt; at the bottom of &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; may consequently modify it as its author pleases. Moreover, &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; is declared &amp;lt;code&amp;gt;static&amp;lt;/code&amp;gt; and it therefore has internal linkage. That means in particular that our rogue developer cannot modify &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt; as the symbol is not exposed to her.&lt;br /&gt;
* In addition to &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;, the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; also defines a variable &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;const int&amp;lt;/code&amp;gt; as an alias of &amp;lt;code&amp;gt;essentially_const&amp;lt;/code&amp;gt;. This global symbol is then exposed in &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; to all other modules of the project. What have we gained? The interface &amp;lt;code&amp;gt;a.h&amp;lt;/code&amp;gt; of the module &amp;lt;code&amp;gt;a.c&amp;lt;/code&amp;gt; now clearly states that &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is constant and any compiler will complain if said rogue developer tries to modify its value.&lt;br /&gt;
&lt;br /&gt;
In fact, trying to compile&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// rogue.c&lt;br /&gt;
#include &amp;quot;a.h&amp;quot;&lt;br /&gt;
&lt;br /&gt;
void fn() {&lt;br /&gt;
  init();&lt;br /&gt;
  const_view = 13; &lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
results in an error message such as&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;rogue.c: In function ‘fn’:&lt;br /&gt;
rogue.c:6:14: error: assignment of read-only variable ‘const_view’&lt;br /&gt;
6 |   const_view = 13;&lt;br /&gt;
  |              ^      &amp;lt;/pre&amp;gt;&lt;br /&gt;
That clearly tells our rogue developer that modifying &amp;lt;code&amp;gt;const_view&amp;lt;/code&amp;gt; is an evil thing to do.&lt;br /&gt;
&lt;br /&gt;
Actually, this trick is not something the author came up with by himself. Rather, while doing some preliminary research for these notes, the author learned this technique from a [https://github.com/simbricks/qemu/blob/c247fb0c7c5fd4e24b5f08f65857b7bf08ce20fe/exec-vary.c#L28 source file in the qemu repositories], where the reader may also find some complementary explanations.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As mentioned earlier, aliases are essentially just a way to have distinct names for the same memory address. As already exploited in the previous example, this means in particular that the aliasing mechanism bypasses C’s already quite weak type system and we might very well have symbols of different types sharing a single memory address.&lt;br /&gt;
&lt;br /&gt;
In the example&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double1.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
extern uint64_t b [[gnu::alias(&amp;quot;a&amp;quot;)]];&lt;br /&gt;
&lt;br /&gt;
#define SIGN_MASK     0x8000000000000000UL&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  a = 0.5;&lt;br /&gt;
  b |= SIGN_MASK;&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
we have a symbol &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; and a symbol &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; sharing the same memory address. Running this example on a computer, where a &amp;lt;code&amp;gt;double&amp;lt;/code&amp;gt; occupies 64 bits and where the sign of doubles is stored in the most significant bit, will result in an output like&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double1&lt;br /&gt;
-0.500000 &amp;lt;/pre&amp;gt;&lt;br /&gt;
The reason is that the line&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;  b |= SIGN_MASK;&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
sets the most significant bit of the value stored at the address referred to by &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;, i.e. the very same address that &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; refers to. This most significant bit of &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; thus happens to be the sign bit of our double &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
As the reader might guess and as witnessed by the following example, one is of course not confined to messing around with the sign bit of floating point numbers. If you know the format of your floating point types, you might freely modify the exponent or the mantissa:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// double2.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
double a;&lt;br /&gt;
extern uint64_t b [[gnu::alias(&amp;quot;a&amp;quot;)]];&lt;br /&gt;
&lt;br /&gt;
#define EXPONENT_MASK 0x7FF0000000000000UL&lt;br /&gt;
#define EXPONENT_SHIFT 52&lt;br /&gt;
&lt;br /&gt;
int main() {&lt;br /&gt;
  // messing with the exponent&lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = (b ^ EXPONENT_MASK) | ((b &amp;amp; EXPONENT_MASK) + (2UL &amp;lt;&amp;lt; EXPONENT_SHIFT));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  // messing with the mantissa &lt;br /&gt;
  a = 1;&lt;br /&gt;
  b = b | (1UL &amp;lt;&amp;lt; (EXPONENT_SHIFT - 2));&lt;br /&gt;
  printf(&amp;quot;%f\n&amp;quot;, a);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
On common consumer hardware, the output of the program looks as follows:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./double2&lt;br /&gt;
4.000000&lt;br /&gt;
1.250000&amp;lt;/pre&amp;gt;&lt;br /&gt;
If you don’t know why, the Wikipedia article on [https://en.wikipedia.org/wiki/Double-precision_floating-point_format double precision floating point numbers] might be a good starting point for finding an explanation on your own.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;doing-more-weird-stuff-with-aliases&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== Doing more weird stuff with aliases ===&lt;br /&gt;
&lt;br /&gt;
As you might have guessed by now, there are very few safeguards in place to prevent us from defining function aliases with different signatures. In combination with other attributes like &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; or &amp;lt;code&amp;gt;gnu::aligned&amp;lt;/code&amp;gt; this may be used for tricks like the following:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;// signatures.c&lt;br /&gt;
#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
void f(S s) {&lt;br /&gt;
  printf(&amp;quot;a = %u, b = %u\n&amp;quot;, s.a, s.b);&lt;br /&gt;
}&lt;br /&gt;
&lt;br /&gt;
[[gnu::alias(&amp;quot;f&amp;quot;)]] extern void g(uint64_t ab);&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  S s = {2,3};&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling f: &amp;quot;);&lt;br /&gt;
  f(s);&lt;br /&gt;
&lt;br /&gt;
  printf(&amp;quot;Calling g: &amp;quot;);&lt;br /&gt;
  g(0x0000000200000001UL);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&lt;br /&gt;
&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
Before showing you what this program does, let us examine it a bit more closely:&lt;br /&gt;
&lt;br /&gt;
* The file begins with the declaration of a struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with two members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint32_t&amp;lt;/code&amp;gt;. The attribute &amp;lt;code&amp;gt;gnu::packed&amp;lt;/code&amp;gt; tells the compiler to not insert any padding bytes, so that the layout of any instance of the struct &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; in memory is exactly as we as C programmers see it: 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; followed by 4 bytes of memory occupied by the member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt;.&lt;br /&gt;
* The function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; takes an instance &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt; of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; and simply prints its members to the standard output.&lt;br /&gt;
* What follows is an alias &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; with a different signature. The function &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; takes a single argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt;. Being an alias of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;, however, any call of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; will execute the exact same code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to. Luckily, values of type &amp;lt;code&amp;gt;uint64_t&amp;lt;/code&amp;gt; occupy 8 bytes of memory, which is the exact same amount of memory occupied by instances of the struct type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt;. The machine code that &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; compiles to will thus interpret the lower and higher 4 bytes of &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;ab&amp;lt;/code&amp;gt; as members &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; and &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; of &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt;’s argument &amp;lt;code&amp;gt;s&amp;lt;/code&amp;gt;, respectively.&lt;br /&gt;
&lt;br /&gt;
Now, let’s see what this program does:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;pre&amp;gt;&amp;amp;gt; ./signatures&lt;br /&gt;
Calling f: a = 2, b = 3&lt;br /&gt;
Calling g: a = 1, b = 2&amp;lt;/pre&amp;gt;&lt;br /&gt;
As explained above, the function &amp;lt;code&amp;gt;f&amp;lt;/code&amp;gt; indeed interprets the 8 bytes &amp;lt;code&amp;gt;0x0000000200000001UL&amp;lt;/code&amp;gt; given as argument to &amp;lt;code&amp;gt;g&amp;lt;/code&amp;gt; as an instance of type &amp;lt;code&amp;gt;S&amp;lt;/code&amp;gt; with member &amp;lt;code&amp;gt;a&amp;lt;/code&amp;gt; being the lower four bytes &amp;lt;code&amp;gt;0x00000001&amp;lt;/code&amp;gt; and member &amp;lt;code&amp;gt;b&amp;lt;/code&amp;gt; being the upper four bytes &amp;lt;code&amp;gt;0x00000002&amp;lt;/code&amp;gt;.&lt;br /&gt;
&lt;br /&gt;
Let me finish this example with a word of warning: Although being great fun, tricks like this one are hightly non-portable as they depend among other things on memory alignment, padding, calling conventions and the concrete hardware that your binary is going to be deployed on. So, &amp;lt;s&amp;gt;if you don’t know exactly what you’re doing&amp;lt;/s&amp;gt;, they should never be used in any production code.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;span id=&amp;quot;an-invitation&amp;quot;&amp;gt;&amp;lt;/span&amp;gt;&lt;br /&gt;
=== An invitation ===&lt;br /&gt;
&lt;br /&gt;
If you’ve come this far, there’s not much more I want to tell you for now. Get a cup of coffee, fire up your favourite editor and have some fun with aliases. If you don’t know where to start, let me give you one last hint: One may use the alias attribute for variables of &amp;lt;code&amp;gt;struct&amp;lt;/code&amp;gt; type, too. This can be seen in the following program that is merely a slight variation of our previous example of function aliases:&lt;br /&gt;
&lt;br /&gt;
&amp;lt;syntaxhighlight lang=&amp;quot;c&amp;quot;&amp;gt;#include &amp;lt;stdio.h&amp;gt;&lt;br /&gt;
#include &amp;lt;stdint.h&amp;gt;&lt;br /&gt;
&lt;br /&gt;
typedef struct [[gnu::packed]] {&lt;br /&gt;
  uint32_t a;&lt;br /&gt;
  uint32_t b;&lt;br /&gt;
} S;&lt;br /&gt;
&lt;br /&gt;
S s;&lt;br /&gt;
extern uint64_t c [[gnu::alias(&amp;quot;s&amp;quot;)]];&lt;br /&gt;
&lt;br /&gt;
int main(int argc, char **argv) {&lt;br /&gt;
  s.b = 0x42;&lt;br /&gt;
  printf(&amp;quot;%lx\n&amp;quot;, c);&lt;br /&gt;
&lt;br /&gt;
  return 0;&lt;br /&gt;
}&amp;lt;/syntaxhighlight&amp;gt;&lt;br /&gt;
&amp;lt;references /&amp;gt;&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
	<entry>
		<id>https://www.emmtrix.com/w139/index.php?title=Main_Page&amp;diff=324</id>
		<title>Main Page</title>
		<link rel="alternate" type="text/html" href="https://www.emmtrix.com/w139/index.php?title=Main_Page&amp;diff=324"/>
		<updated>2024-02-19T11:53:11Z</updated>

		<summary type="html">&lt;p&gt;Tobias.columbus: /* Compiler Background */&lt;/p&gt;
&lt;hr /&gt;
&lt;div&gt;&amp;lt;languages/&amp;gt;&lt;br /&gt;
&amp;lt;translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:1--&amp;gt;&lt;br /&gt;
Welcome to the emmtrix Technologies Wiki. As a company with a deep-rooted passion for compilers, we specialize in source-to-source compilers designed to analyze, optimize and transform your code. This Wiki aims to offer detailed, technical background information that complements the tools and resources available on our official website. Here, you&#039;ll find in-depth explanations, usage guidelines, and insights into the engineering behind our specialized software solutions. Whether you&#039;re a developer or a technically-inclined enthusiast, this space is designed to deepen your understanding of what makes our tools essential for your projects.&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:6--&amp;gt;&lt;br /&gt;
=== emmtrix Products ===&lt;br /&gt;
&lt;br /&gt;
&amp;lt;!--T:7--&amp;gt;&lt;br /&gt;
* [[emmtrix Studio Release Notes]]&lt;br /&gt;
* emmtrix Parallel Studio&lt;br /&gt;
* [[emmtrix C++ to C Compiler]]&lt;br /&gt;
** [https://online-ecpp2c.emmtrix.com Live C++ to C compiler demo]&lt;br /&gt;
&lt;br /&gt;
&amp;lt;/translate&amp;gt;&lt;br /&gt;
&lt;br /&gt;
=== Compiler Background ===&lt;br /&gt;
&lt;br /&gt;
* [[Loop Transformations]]&lt;br /&gt;
*[[Demystifying C++]]&lt;br /&gt;
*[[The alias Attribute|The alias attribute]]&lt;/div&gt;</summary>
		<author><name>Tobias.columbus</name></author>
	</entry>
</feed>