Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6056965
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T08:26:10+00:00 2026-05-23T08:26:10+00:00

I’ll start with the ultimate question: In C with gcc, is it possible to

  • 0

I’ll start with the ultimate question: In C with gcc, is it possible to get the value(s) of __func__ (or equivalently, __FUNCTION__) stored in a section other than .rodata (or wherever -mrodata= points) or subsection thereof?

The full explanation:

Say I have a logging macro:

#define LOG(fmt, ...) log_internal(__FILE__, __LINE__, __func__, fmt, ##__VA_ARGS__)

(The string concatenation operator ## used in that unary context consumes the preceding comma if and only if the __VA_ARGS__ list is empty, thereby allowing use of a format string with or without arguments.)

I can then use the macro normally:

void my_function(void) {
    LOG("foo!");
    LOG("bar: %p", &bar);
}

might print (obviously depending on the implementation of log_internal):

foo.c:201(my_function) foo!
foo.c:202(my_function) bar: 0x12345678

In this case, the format strings ("foo" and "bar: %p") and the preprocessor strings ("foo.c" and "my_function") are anonymous read only data, and they get placed into the .rodata section automatically.

But say I want them to go to a different place (I’m on an embedded platform running almost everything from RAM for speed, but memory constraints are pushing for moving some things into ROM). It’s “easy” to move __FILE__ and the format string:

#define ROM_STR(str) (__extension__({static const __attribute__((__section__(".rom_data"))) char __c[] = (str); (const char *)&__c;}))
#define LOG(fmt, ...) log_internal(ROM_STR(__FILE__), __LINE__, __func__, ROM_STR(fmt), ##__VA_ARGS__)

You can’t put an __attribute__ on an anonymous string, so the ROM_STR macro gives it a transient name, affixes it to a specific section, then evaluates to the starting address, so it can substitute cleanly. This doesn’t work if you try to pass a char * variable to LOG as your format string, but I’m willing to exclude that use case.

Normally, anonymous strings that happen to be identical get combined by the compiler into a single storage location, so every instance of __FILE__ in one file would share the same runtime address. With the explicit naming in ROM_STR, each instance will get its own storage location, so it probably doesn’t actually make sense to use it on __FILE__.

However, I would like to use it on __func__. The problem is that __func__ is not the same kind of magic as __FILE__. From the gcc manual, “Function Names as Strings”:

The identifier __func__ is implicitly declared by the translator as if, immediately following the opening brace of each function definition, the declaration

static const char __func__[] = "function-name";

appeared, where function-name is the name of the lexically-enclosing function. This name is the unadorned name of the function.
…
These identifiers are not preprocessor macros. In GCC 3.3 and earlier, and in C only, __FUNCTION__ and __PRETTY_FUNCTION__ were treated as string literals; they could be used to initialize char arrays, and they could be concatenated with other string literas. GCC 3.4 and later treat them as variables, like __func__.

Thus, if you wrap __func__ with ROM_STR, you get

error: invalid initializer

and if you try to put a section attribute before or after the use of __func__, you get

error: expected expression before ‘__attribute__’

or

error: expected ‘)’ before ‘__attribute__’

And thus we loop back to the opening question: Is it possible to get __func__ stored in a section of my choosing? Maybe I can use -fdata-sections and do some linker script magic to get .rodata.__func__.* excluded from the rest of .rodata? If so, what is the syntax for globbing with exclusion in a linker script? In other words, somewhere you have a *(.rodata*) – I could put a *(.rodata.__func__*) somewhere else, but I would need to modify the original glob to exclude it so I don’t get two copies.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-23T08:26:10+00:00Added an answer on May 23, 2026 at 8:26 am

    It looks like I answered my own question at the end with the -fdata-sections business, I just didn’t understand the GNU Linker enough to see it. I don’t actually need the globbing with exclusion as long as I specify the *(.rodata.__func__*) bit first. Any sections that glob matches will get marked as used, so a later glob for *(.rodata*) won’t double count them and copy them somewhere else. I don’t need to tag them with ROM_STR at all. Cool!

    It’s important to note that -fdata-sections does actually put each function string into its own .rodata.__func__.1234 section (I’m not sure what pattern the numbers follow). I don’t know if anonymous strings also get their own sections; if so, I could use the same linker tricks to capture all of the anonymous strings instead of the ROM_STR section attribute macro, but it would probably be a bad idea. ROM_STR gets used in the LOG macro, so it’s guaranteed only to be applied to logging format strings. If I forced all anonymous strings into ROM with a linker trick, that would include normal message data, and I would pay a runtime performance penalty to access it from flash. So I don’t know if it’s even possible, but its advisability would depend on your specific system requirements.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
Specifically, suppose I start with the string string =hello \'i am \' me And
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I have a bunch of posts stored in text files formatted in yaml/textile (from
Is it possible to replace javascript w/ HTML if JavaScript is not enabled on
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have just tried to save a simple *.rtf file with some websites and
I want to count how many characters a certain string has in PHP, but

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.