Hi to all. A question on C preprocessor directives. I came across the following #define in a program header file associated with the ELF file format.
define PF_X (1<<0)
When PF_X is encountered in the program text, what is the value that is actually inserted in its place.........in other words.......what does (1<<0) mean???
Here are some more examples of the syntax......One of the definitions below includes & which has an obvious meaning......but << and >> are not so obvious.
//Manipulation of st_info values
define ELF32_ST_BIND(i) ((i)>>4)
define ELF32_ST_TYPE(i) ((i)&0xf)
define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf))
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
It means exactly what it says, 1 shifted by zero bits. Whether the compiler actually generates code to perform a zero bit shift is probably down to thw compiler and the processor instruction set. I would imagine that most compilers are smart enough to simply replace that with a literal constant 1, since it is not hard for teh pre-processor to determine that it is a constant expression and so evaluate it at runtime.
The code may seem pointless, but such code is often written thus merely for consistency with similar and adjacently defined bit-mask macros that may not be bit zero, and also to support code maintenance - so for example the bit mask can be easily changes simply by changing the right-hand bit position operand.
Without knowing the context, for thw following:
define ELF32_ST_BIND(i) ((i)>>4)
define ELF32_ST_TYPE(i) ((i)&0xf)
define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf))
I would say that an ELF32 INFO byte comprises of a BIND field and a TYPE field, where each field is a four bit value, and BIND is packed into the upper nibble, while TYPE is in the lower nibble, so for example, an INFO byte 0x5a has a BIND = 5, and TYPE= 10 (0xa), the macros provide methods of extracting the fields and also for creating an INFO byte from the separate field values. So for example:
ELF32_ST_INFO(10, 5 ) == 0xa5
Clifford
If you would like to refer to this comment somewhere else in this project, copy and paste the following link:
Hi to all. A question on C preprocessor directives. I came across the following #define in a program header file associated with the ELF file format.
define PF_X (1<<0)
When PF_X is encountered in the program text, what is the value that is actually inserted in its place.........in other words.......what does (1<<0) mean???
Here are some more examples of the syntax......One of the definitions below includes & which has an obvious meaning......but << and >> are not so obvious.
//Manipulation of st_info values
define ELF32_ST_BIND(i) ((i)>>4)
define ELF32_ST_TYPE(i) ((i)&0xf)
define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf))
It means exactly what it says, 1 shifted by zero bits. Whether the compiler actually generates code to perform a zero bit shift is probably down to thw compiler and the processor instruction set. I would imagine that most compilers are smart enough to simply replace that with a literal constant 1, since it is not hard for teh pre-processor to determine that it is a constant expression and so evaluate it at runtime.
The code may seem pointless, but such code is often written thus merely for consistency with similar and adjacently defined bit-mask macros that may not be bit zero, and also to support code maintenance - so for example the bit mask can be easily changes simply by changing the right-hand bit position operand.
Without knowing the context, for thw following:
define ELF32_ST_BIND(i) ((i)>>4)
define ELF32_ST_TYPE(i) ((i)&0xf)
define ELF32_ST_INFO(b,t) (((b)<<4)+((t)&0xf))
I would say that an ELF32 INFO byte comprises of a BIND field and a TYPE field, where each field is a four bit value, and BIND is packed into the upper nibble, while TYPE is in the lower nibble, so for example, an INFO byte 0x5a has a BIND = 5, and TYPE= 10 (0xa), the macros provide methods of extracting the fields and also for creating an INFO byte from the separate field values. So for example:
ELF32_ST_INFO(10, 5 ) == 0xa5
Clifford
For completeness, more examples:
ELF32_ST_BIND( 0xab ) == 0xa (10 decimal)
ELF32_ST_TYPE( 0xab ) == 0xb (11 decimal)
Thank you Clifford.
A very well explained answer to my question.