-4

Is it possible to allocate same address to two different global variables in C in same build? I have tried several ways using linker but I was not successful. If it is not allowed, can someone please elaborate why? And if it is allowed, how to do it.

Edit 1

For example in {file1.c} I have a global variable {int a} and in {file2.c} I have a global variable {int b}. Is possible to link them to same address? I am not providing the detail here, because I have already analyzed other options of achieving this. The objective of this questions is know if same memory linking is possible.

  • 7
    Can you elaborate, the question is not very clear? What problem are you _actually_ trying to resolve? What is your platform? – Jabberwocky Mar 18 '20 at 10:57
  • 1
    If you manipulate the link to put two objects at the same place, they will be the same, not different. – Eric Postpischil Mar 18 '20 at 11:16
  • 2
    Unionize them?? – Martin James Mar 18 '20 at 13:33
  • There are many possible answers, union, pointer, linker, ... You need to clarify what you want to do exactly – Julien Mar 18 '20 at 14:04
  • It's possible, BUT very likely you try to solve a problem with the wrong solution. See also [XY problem](https://meta.stackexchange.com/a/66378) – jeb Mar 18 '20 at 16:24
  • 2
    Re “I am not providing the detail here, because I have already analyzed other options…”: This is hubris. The participants in this site cumulatively have millenia of experience and the knowledge that comes with it. Solutions for all normal problems, including uncommon ones, are already known and mostly built into tools. In spite of your purported analysis, it is overwhelmingly likely there is a better way to achieve your ultimate goal. Failing to provide details is selfish: Stack Overflow is not a site to serve you personally; it is intended to be a durable repository of information for all. – Eric Postpischil Mar 18 '20 at 16:25
  • Of the "other options" you "analysed" I imagine one was: `#define global_alias global`? One option perhaps not considered is to use C++ and a reference variable `int& global_alias = global;`. Either seems far simpler that a linker based solution. – Clifford Mar 18 '20 at 18:40
  • @EricPostpischil I was not showing attitude, I am sorry if it felt that way. This is my second question on stack overflow, I would be more careful next time. But in my defense, what I meant is I needed specific solution using linker only. Hence, I did not want to consume other people's time. Anyway message received. :) – Rachit Kumar Mar 29 '20 at 13:06

2 Answers2

1

If you use binutils ld you can use overlays https://ftp.gnu.org/old-gnu/Manuals/ld-2.9.1/html_node/ld_22.html

I use it very often myself.

0___________
  • 34,740
  • 4
  • 19
  • 48
0

I found the solution by using linker level symbols. So it was as simple as using section attribute for a variable. And in linker script, right before dumping the section create the symbol to be used in another C file using PROVIDE.

So in my example above:

file1.c
int __attribute__((section (".config_value"))) a = 10;

file2.h
extern int b;

linker.ld (in the section you want to dump)
PROVIDE(b = . );
file1.o(.config_value)

And then variable b in file2.c also has value 10.