Home | History | Annotate | Download | only in libs
      1 /*
      2  * Copyright (C) 2014 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 class A {
     18 public:
     19   virtual int getRandomNumber() {
     20     return 4;  // chosen by fair dice roll.
     21                // guaranteed to be random.
     22   }
     23 
     24   virtual ~A() {}
     25 };
     26 
     27 A a;
     28 
     29 // nested macros to make it easy to define a large amount of read-only data
     30 // which will require relocation.
     31 #define A_16 &a, &a, &a, &a, &a, &a, &a, &a, &a, &a, &a, &a, &a, &a, &a, &a,
     32 #define A_128 A_16 A_16 A_16 A_16 A_16 A_16 A_16 A_16
     33 #define A_1024 A_128 A_128 A_128 A_128 A_128 A_128 A_128 A_128
     34 
     35 extern "C" A* const lots_of_relro[] = {
     36   A_1024 A_1024 A_1024 A_1024 A_1024 A_1024 A_1024 A_1024
     37 };
     38 
     39 extern "C" int getRandomNumber() {
     40   // access the relro section (twice, in fact, once for the pointer, and once
     41   // for the vtable of A) to check it's actually there.
     42   return lots_of_relro[0]->getRandomNumber();
     43 }
     44