3389 views|14 replies

419

Posts

0

Resources
The OP
 

How to assign a separate segment to the bss of a C file in the link file? [Copy link]

 

I want to put the bbs of a C file in a separate segment. In this way, when the program is running, all the RAM in this segment will be cleared to restore a module to its initial state. The compiler I use is GCC. I modified the link script and added the following segment.

.keyscan_bss :
{
            . = (((. + 3) / 4)*4);
            PROVIDE(_keyscan_bss_start_ = .);
            ./proj/drivers/keyboard.o(.sbss)
            ./proj/drivers/keyboard.o(.sbss.*)
            ./proj/drivers/keyboard.o(.bss)
            ./proj/drivers/keyboard.o(.bss.*)
}
PROVIDE(_keyscan_bss_end_ = .);

But I found that only static variables are put in this segment. Global variables are still in the original BSS segment. Don't global variables belong to keyboard.o? Please give me some advice?

This post is from Programming Basics

Latest reply

It may have been optimized, and the KEEP command should be added.   Details Published on 2023-11-4 16:18
 

661

Posts

18

Resources
2
 

@辛昕[/url] @chenzhufly[/url] , both of them are experts in this field.

This post is from Programming Basics
 
 
 

7422

Posts

2

Resources
3
 

It's pattern matching. Check if there is a problem with the order of keyboard.o and the * wildcard.

This post is from Programming Basics

Comments

I copied it by example. The original BSS segment is like this. .bss : { . = (((. + 3) / 4)*4); PROVIDE(_start_bss_ = .); *(.sbss) *(.sbss.*) *(.bss) *(.bss.*) } PROVIDE(_e  Details Published on 2020-3-4 10:03
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

419

Posts

0

Resources
4
 
freebsder posted on 2020-3-3 22:45 Pattern matching, check if there is a problem with the order of keyboard.o and the * wildcard.

I copied the original BSS segment like this.

.bss :
{
	    . = (((. + 3) / 4)*4);
	    PROVIDE(_start_bss_ = .);
        *(.sbss)
        *(.sbss.*)
        *(.bss)
        *(.bss.*)
}
PROVIDE(_end_bss_ = .);

I will replace * with keyboard.o

This post is from Programming Basics
 
 
 

7422

Posts

2

Resources
5
 
.keyscan_bss :
{
            . = (((. + 3) / 4)*4);
            PROVIDE(_keyscan_bss_start_ = .);
            ./proj/drivers/keyboard.o(.sbss)
            ./proj/drivers/keyboard.o(.sbss.*)
            ./proj/drivers/keyboard.o(.bss)
            ./proj/drivers/keyboard.o(.bss.*)
}
PROVIDE(_keyscan_bss_end_ = .);

.bss :
{
	    . = (((. + 3) / 4)*4);
	    PROVIDE(_start_bss_ = .);
        *(.sbss)
        *(.sbss.*)
        *(.bss)
        *(.bss.*)
}
PROVIDE(_end_bss_ = .);

Try it.

This post is from Programming Basics

Comments

I wrote it like this. But the global variables in keyboard.o are allocated to the bss below. Only the static variables are in keyscan_bss.  Details Published on 2020-3-4 12:39
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

419

Posts

0

Resources
6
 
freebsder published on 2020-3-4 10:18 .keyscan_bss : { . = (((. + 3) / 4)*4); PROVIDE(_keyscan_bss_start_ = . ...

I wrote it like this. But the global variables in keyboard.o are allocated to the bss below. Only the static variables are in keyscan_bss.

This post is from Programming Basics
 
 
 

7422

Posts

2

Resources
7
 

Then provide the complete script file. I don’t have the energy to guess how to write it.

This post is from Programming Basics
 
Personal signature

默认摸鱼,再摸鱼。2022、9、28

 
 

295

Posts

1

Resources
8
 

You are using the gcc compiler, use the __attribute__ (section) attribute directly in the code, see the gcc manual for details,

This post is from Programming Basics

Comments

This is a little more troublesome. You need to add this attribute before each variable.  Details Published on 2020-3-6 10:00
 
Personal signature

我的小站 我的博客

 
 

419

Posts

0

Resources
9
 
hotsauce1861 posted on 2020-3-4 22:13 Your compiler is gcc, use the __attribute__ (section) attribute directly in the code, please refer to the gcc manual for details,

This is a little more troublesome. You need to add this attribute before each variable.

This post is from Programming Basics
 
 
 

22

Posts

0

Resources
10
 

1. Reason: By default, for GCC, static variables without static modifiers belong to the common section, not the BSS section. GCC does this to make some optimizations (for specific optimizations, see the description of -fno-common in the GCC documentation). On the other hand, most link files will put the common section in the BSS section.

2. A solution: add -fno-common to the compilation option

This post is from Programming Basics

Comments

Great! Thank you for your explanation. I will try it. 1. Static variables without static modifier belong to common section 2. Most link files will put common section in BSS section. Isn't this just taking off pants to fart, redundant?  Details Published on 2020-4-13 10:42
 
 
 

419

Posts

0

Resources
11
 
Chengfeng published on 2020-4-10 17:49 1. Reason: By default, for GCC, static variables without static modifiers belong to the common section, not the BSS section. GCC...

Great! Thank you for your explanation. I will try it.

1. Static variables without static modifier belong to the common section

2. Most linker files will put the common section in the BSS section

Isn't this just like taking off your pants to fart, a waste of time?

This post is from Programming Basics

Comments

1: Global variables are just a type of static variables, but for the sake of convenience, "global variables" are used instead of "static variables". 2: Sorry, the description that "static variables without static modifier belong to the common section" is wrong.  Details Published on 2020-4-13 21:45
 
 
 

22

Posts

0

Resources
12
 
This post was last edited by Chengfeng on 2020-4-13 21:49
woody_chen posted on 2020-4-13 10:42 Great! Thank you for your explanation. I will try it. 1. Static variables without static modifier belong to common section 2. Most links...

1: Global variables are just a type of static variables, but for the sake of convenience, "global variables" are used instead of "static variables" below.

Second: Sorry, the description of "static variables without static modifier belong to common section" is wrong. It should be "global variables without static modifier and uninitialized (or initialized to 0) belong to common section".

1. int a;

2. int b = 0;

The following do not belong to the common section

1. static int c;

2. int d = 1;

3. static int e = 0;

4. static int f = 2;

3: Placing global variables in the common section is for optimization, and placing the common section in the bss section is to clear the common section.

1. Assuming that those global variables are not placed in the common section but directly in the bss section, then they are likely to be scattered in the bss section and cannot be optimized.

2. Assuming that the common section is not placed in the bss section, do we need to first write a code to clear the bss section, and then write another code to clear the common section? Wouldn't it be better to put the common section in the bss section and clear them together?

This post is from Programming Basics

Comments

I have tested it and it works. After adding -fno-common, duplicate defined variables will report an error. Without this option, no error will be reported.  Details Published on 2020-4-14 11:21
 
 
 

22

Posts

0

Resources
13
 

If -fno-common doesn't work, try using the KEEP keyword in the link file.

This post is from Programming Basics
 
 
 

419

Posts

0

Resources
14
 
Chengfeng posted on 2020-4-13 21:45 woody_chen posted on 2020-4-13 10:42 Great God! Thank you for your explanation. I will try it. 1. Static variables without static modifier belong to co ...

Pro-tested and available.

After adding -fno-common, duplicate definition of variables will cause errors. Without this option, no errors will be reported.

This post is from Programming Basics
 
 
 

16

Posts

1

Resources
15
 

It may have been optimized, and the KEEP command should be added.

This post is from Programming Basics
 
 
 

Guess Your Favourite
Find a datasheet?

EEWorld Datasheet Technical Support

EEWorld
subscription
account

EEWorld
service
account

Automotive
development
circle

Copyright © 2005-2024 EEWORLD.com.cn, Inc. All rights reserved 京B2-20211791 京ICP备10001474号-1 电信业务审批[2006]字第258号函 京公网安备 11010802033920号
快速回复 返回顶部 Return list