Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use noreturn with function pointer?

Tags:

c

noreturn

c11

I am writing a bootloader in C11. When the bootloader needs to transfer the control to the firmware, it reads a function pointer at a predefined memory address and calls it. The code looks like this:

typedef void (FirmwareBootFn)(void);

typedef struct
{
    uint32_t stackPointer;
    FirmwareBootFn* programCounter;
}
FirmwareBootControl;

static FirmwareBootControl g_bootControl __attribute__ ((section (".boot_control")));

void
Firmware_boot( void )
{
    setStackPointer( g_bootControl.stackPointer );
    g_bootControl.programCounter();
}

Function Firmware_boot() never returns, so it made sense to declare it as noreturn:

#include <stdnoreturn.h>

noreturn void
Firmware_boot( void );

But I need to declare FirmwareBootFn as noreturn as well to avoid the compiler complaining that Firmware_boot() may return.

I tried (possibly) every permutation of the noreturn in the typedef without any result. Also I understood that the attribute can't be set in the typedef because it is not part of the type.

Is there a way to tag my Firmware_boot() as noreturn avoiding the warning (well without cheating with warning suppression :-))?

like image 754
MaxP Avatar asked Feb 26 '15 09:02

MaxP


People also ask

How do you call a function from a function pointer?

Calling a Function Through a Function Pointer in C Calling a function using a pointer is similar to calling a function in the usual way using the name of the function. int length = 5; // Different ways to call the function // 1. using function name int area = areaSquare(length); // 2.

Can we increment function pointer?

When a pointer is incremented, it actually increments by the number equal to the size of the data type for which it is a pointer. For Example: If an integer pointer that stores address 1000 is incremented, then it will increment by 2(size of an int) and the new address it will points to 1002.

Can you cast function pointers?

Yes, it can. This is purpose of casting function pointers, just like usual pointers. We can cast a function pointer to another function pointer type but cannot call a function using casted pointer if the function pointer is not compatible with the function to be called.

What is the use of [noreturn]] in Python?

Note that the [ [noreturn]] is mostly used in void functions. However, this is not a requirement, allowing the functions to be used in generic programming: The following standard library functions have this attribute:

When to use [noreturn]] in C++?

Note that the [ [noreturn]] is mostly used in void functions. However, this is not a requirement, allowing the functions to be used in generic programming:

What are the differences between function pointers and normal pointers?

Typically a function pointer stores the start of executable code. 2) Unlike normal pointers, we do not allocate de-allocate memory using function pointers. 3) A function’s name can also be used to get functions’ address. For example, in the below program, we have removed address operator ‘&’ in assignment.

How do you declare a function without the noreturn attribute?

The first declaration of the function must specify this attribute if any declaration specifies it. If a function is declared with [ [noreturn]] in one translation unit, and the same function is declared without [ [noreturn]] in another translation unit, the program is ill-formed; no diagnostic required.


1 Answers

_Noreturn in C11 can only be applied to function definitions or declarations. The fact that the function doesn't return is not part of the prototype, unfortunately.

Since you seem to have gcc you could use an extension

typedef struct
{
    uint32_t stackPointer;
    __attribute__((__noreturn__)) FirmwareBootFn* programCounter;
}
FirmwareBootControl;

to mark the function pointer as not returning. Unfortunately though, there doesn't seem to be a way to ensure that the function that you assign to that really has that property by syntax, allone.

like image 58
Jens Gustedt Avatar answered Oct 05 '22 23:10

Jens Gustedt