Skip to contentSkip to conversation

Can grub2 support c++ module?

DISCUSSION y.volta 4 participants 4 messages Jun 18, 2008 – Jun 25, 2008 1 message not in the archive

lore
Share & tools
lore

y.volta

author
#1 lore raw
In reply to a message not in the archive

Hi,

I just wondering, can grub2 support module coded with c++? Let's suppose we are trying to apply the fancy menu. ;-)

If we can use the object oriented, the menu items, the pictures, the controls are all objects. The programming interface is easier for us. And, I know the XOSL used c++ for its graphic window system; But, if we can not do it as this, we have to do things like Allegro. P.S. if we can use OO, there is maybe possibility of using the FOX toolkit in grub2, even.

To the fancy menu, i think, if we can make the fancy menu module support scripts ( such as lua, or lite-c ) that's will be more interesting for people to produce much more 'theme' files. This will just like the free game deveopment platform - the 3D GameStudio: our engine is the fancy menu module, the script is the theme core file ( for better experience, we can make all of the related file of this theme into a romfs file, so we can call this romfs file 'the theme file' ).

If so, the traditional menu interface is just a theme for us, and we can do much more on the menu interface than ever. the MVC framework is also considered in this thought.

This is just a thought, do you have any other idea?

#2 lore raw
Quoted 3 lines · y.volta

Hi,

I just wondering, can grub2 support module coded with c++? Let's suppose we are trying to apply the fancy menu. ;-)

I think it can be done, but you'll need to disable exception handling and RTTI. You won't be able to use any C++ libraries. The build system will need to be changed to call C++ compiler when needed.

Still, I think it would be an overkill. C is quite good for large projects if used properly. Linux is fine with C. C also has checkers such as sparse, but I'm not aware of C++ equivalents.

Signature · 2 lines
Regards,
Pavel Roskin
#3 lore raw
Quoted 6 lines · On Wednesday 18 June 2008 06:09:10 Pavel Roskin wrote:
Quoted 4 lines · y.volta

Hi,

I just wondering, can grub2 support module coded with c++? Let's
suppose we are trying to apply the fancy menu. ;-)

Quoted 7 lines

I think it can be done, but you'll need to disable exception handling and RTTI. You won't be able to use any C++ libraries. The build system will need to be changed to call C++ compiler when needed.

Still, I think it would be an overkill. C is quite good for large projects if used properly. Linux is fine with C. C also has checkers such as sparse, but I'm not aware of C++ equivalents.

When Vesa tried C++, the binary size got bloated, so it was abandoned. I am afraid that C++ adds too much overhead.

Okuji

#4 lore raw

Re: Can grub2 support c++ module? (Patch)

Quoted 13 lines · Yoshinori K. Okuji
Quoted 11 lines · On Wednesday 18 June 2008 06:09:10 Pavel Roskin wrote:
Quoted 5 lines · y.volta

Hi,

I just wondering, can grub2 support module coded with c++? Let's
suppose we are trying to apply the fancy menu. ;-)

I think it can be done, but you'll need to disable exception handling and RTTI. You won't be able to use any C++ libraries. The build system will need to be changed to call C++ compiler when needed.

Quoted 8 lines
Quoted 4 lines

Still, I think it would be an overkill. C is quite good for large projects if used properly. Linux is fine with C. C also has checkers such as sparse, but I'm not aware of C++ equivalents.

When Vesa tried C++, the binary size got bloated, so it was abandoned. I am afraid that C++ adds too much overhead.

Hi,

With exceptions/RTTI turned off and careful use of C++ features, C++ size overhead is close to zero. Even some overhead does not matter for modules which are typically not included into core.img.

The problem using C++ in portable stand-alone programs like grub are the 'object machine' runtime support functions which differ between platforms and gcc releases.

But if new/delete are replaced in the standard way and calls to other runtime support (like global/static ctors/dtors) are avoided, a reasonable C++ subset remains.

The following C++ subset can be used:
+ classes, inheritance, virtual functions, virtual inheritance
+ objects allocated on stack, by new, and by placement new.
+ templates

The following cannot be used:
- global or static objects
- try/catch/throw, dynamic_cast, RTTI
- most of STL
- nested functions (gcc supports these for C only)

See attached patch for a working example. It adds some tests for basic C++ functionality to hello/hello.c. Some C++ support is provided by new includes mm.hpp and dl.hpp. No additions to kernel should be necessary (there might be some minor issues with kern/dl.c due to new symbol types).

For testing purposes, hello.c can be compiled as a C++ source using:

$ make hello_mod-hello_hello.o \
   hello_mod_CFLAGS='-x c++ -fno-exceptions -fno-rtti $(COMMON_CFLAGS)' \
  && make

I also would appreciate support for grub modules written in C++.

Christian

hello/hello.c +149 -3
diff -ruN grub2.orig/hello/hello.c grub2/hello/hello.c
@@ -18,25 +18,164 @@
  *  along with GRUB.  If not, see <http://www.gnu.org/licenses/>.
  */
 
+
+// Include C++ support first
+#include <grub/dl.hpp>
+#include <grub/mm.hpp>
+
+extern "C" { // TODO: add extern "C" to all includes [__cplusplus]
 #include <grub/types.h>
 #include <grub/misc.h>
 #include <grub/mm.h>
 #include <grub/err.h>
 #include <grub/dl.h>
 #include <grub/normal.h>
+}
+
+
+// Class to set and restore grub_mm_debug in a block
+
+class do_mm_debug
+{
+#ifdef MM_DEBUG
+public:
+  explicit do_mm_debug (bool debug = true)
+    : m_debug_old (grub_mm_debug)
+    { grub_mm_debug = (int)debug; }
+  
+  ~do_mm_debug ()
+    { grub_mm_debug = m_debug_old; }
+
+private:
+  int m_debug_old;
+#else
+
+public:
+  do_mm_debug () { }
+  explicit do_mm_debug (bool) { }
+#endif
+};
+
+
+
+// Classes to test single inheritance and vtable
+
+class A
+{
+public:
+  explicit A (const char * name);
+  virtual ~A ();
+
+  virtual void hello () const;
+
+protected:
+  char * m_name;
+};
+
+class B : public A
+{
+public:
+  explicit B (const char * name);
+  virtual ~B ();
+
+  virtual void hello () const;
+};
+
+
+
+A::A (const char * name)
+: m_name (new char[grub_strlen (name) + 1]) // calls grub_fatal on failure
+{
+  grub_strcpy (m_name, name);
+  grub_printf ("%sA::A ()\n", m_name);
+}
+
+A::~A ()
+{
+  grub_printf ("%sA::~A ()\n", m_name);
+  delete [] m_name;
+}
+
+void A::hello () const
+{
+  grub_printf ("%sA::hello (): Something wrong!\n", m_name);
+}
+
+
+B::B (const char * name)
+: A (name)
+{
+  grub_printf ("%sB::B ()\n", m_name);
+}
+
+B::~B ()
+{
+  grub_printf ("%sB::~B ()\n", m_name);
+}
+
+void B::hello () const
+{
+  grub_printf ("%sB::hello (): Hello C++ World\n", m_name);
+}
+
+
+// Globals
+
+//static B gb ("gb."); // NOT SUPPORTED
+
+static char gb_mem[sizeof (B)];
+B & gb = *(B *)gb_mem;  // ctor called by placement new
+
+A * pa = 0;
+
+
+static void
+say_hello (const A & a)
+{
+  a.hello ();
+}
+
 
 static grub_err_t
 grub_cmd_hello (struct grub_arg_list *state __attribute__ ((unused)),
 		int argc __attribute__ ((unused)),
 		char **args __attribute__ ((unused)))
 {
-  grub_printf ("Hello World\n");
-  return 0;
+  do_mm_debug here;
+
+  // call virtual function B::hello () for all objects
+
+  say_hello (gb); // global object
+
+  if (pa)
+    {
+      say_hello (*pa); // global pointer (actually B *)
+      delete pa;
+      pa = 0;
+    }
+
+  //static B sb ("sb."); // NOT SUPPORTED
+  //say_hello (&sb);
+
+  B vb ("vb.");
+  say_hello (vb); // local object
+
+  say_hello ( B("_temp_B.") ); // temporary object
+
+  return GRUB_ERR_NONE;
 }
 
 GRUB_MOD_INIT(hello)
 {
-  (void)mod;			/* To stop warning. */
+  {
+    do_mm_debug here;
+
+    pa = new (std::nothrow) B ("pa->");
+    if (! pa)
+      return;
+
+    new (gb_mem) B ("gb."); // returns &gb
+  }
   grub_register_command ("hello", grub_cmd_hello, GRUB_COMMAND_FLAG_BOTH,
 			 "hello", "Say hello", 0);
 }
@@ -44,4 +183,11 @@
 GRUB_MOD_FINI(hello)
 {
   grub_unregister_command ("hello");
+
+  do_mm_debug here;
+
+  gb.~B();
+
+  delete pa;
+  pa = 0;
 }
include/grub/dl.hpp +37 -0
diff -ruN grub2.orig/include/grub/dl.hpp grub2/include/grub/dl.hpp
@@ -0,0 +1,37 @@
+// GRUB2 C++ GRUB_MOD_INIT/FINI
+
+#ifndef GRUB_DL_HPP
+#define GRUB_DL_HPP 1
+
+extern "C" {
+  #include <grub/dl.h> // GRUB_MOD_INIT/FINI
+}
+
+
+// TODO: Add this to dl.h [__cplusplus]
+
+#undef GRUB_MOD_INIT
+#undef GRUB_MOD_FINI
+
+#define GRUB_MOD_INIT(name)	\
+extern "C" { \
+  static void grub_mod_init (grub_dl_t mod __attribute__ ((unused))) __attribute__ ((used)); \
+  void grub_##name##_init (void); \
+} \
+void \
+grub_##name##_init (void) { grub_mod_init (0); } \
+static void \
+grub_mod_init (grub_dl_t mod __attribute__ ((unused)))
+
+#define GRUB_MOD_FINI(name)	\
+extern "C" { \
+  static void grub_mod_fini (void) __attribute__ ((used)); \
+  void grub_##name##_fini (void); \
+} \
+void \
+grub_##name##_fini (void) { grub_mod_fini (); } \
+static void \
+grub_mod_fini (void)
+
+#endif
+
include/grub/mm.hpp +102 -0

102 lines not on this page · raw message