// Copyright 2021 Joshua Bakita // This program does a simple prime number lookup #include "stdio.h" // Should our lookup table be sparse? // If enabled, this binary will only work on Linux before 3.19 due to a bug in // Linux's ELF parser. #define SPARSE_BINARY 1 #if SPARSE_BINARY // A two-page alignment causes LD to use separate ELF segments for each // variable (as there's an empty page between each variable). #define ALIGNMENT 8192 #else #define ALIGNMENT 8 #endif // Yes, this is the most bizzare way to do a lookup table, but bear with me. // These all have to be in their own sections to make LD break them out into // separate segments when SPARSE_BINARY is enabled. int __attribute__((section(".sec1"))) __attribute__((aligned(ALIGNMENT))) prime1 = 2; int __attribute__((section(".sec2"))) __attribute__((aligned(ALIGNMENT))) prime2 = 3; int __attribute__((section(".sec3"))) __attribute__((aligned(ALIGNMENT))) prime3 = 5; int __attribute__((section(".sec4"))) __attribute__((aligned(ALIGNMENT))) prime4 = 7; int __attribute__((section(".sec5"))) __attribute__((aligned(ALIGNMENT))) prime5 = 11; int __attribute__((section(".sec6"))) __attribute__((aligned(ALIGNMENT))) prime6 = 13; int __attribute__((section(".sec7"))) __attribute__((aligned(ALIGNMENT))) prime7 = 17; int __attribute__((section(".sec8"))) __attribute__((aligned(ALIGNMENT))) prime8 = 19; int __attribute__((section(".sec9"))) __attribute__((aligned(ALIGNMENT))) prime9 = 23; int __attribute__((section(".sec10"))) __attribute__((aligned(ALIGNMENT))) prime10 = 29; int __attribute__((section(".sec11"))) __attribute__((aligned(ALIGNMENT))) prime11 = 31; int __attribute__((section(".sec12"))) __attribute__((aligned(ALIGNMENT))) prime12 = 37; int __attribute__((section(".sec13"))) __attribute__((aligned(ALIGNMENT))) prime13 = 41; int __attribute__((section(".sec14"))) __attribute__((aligned(ALIGNMENT))) prime14 = 43; int __attribute__((section(".sec15"))) __attribute__((aligned(ALIGNMENT))) prime15 = 47; int __attribute__((section(".sec16"))) __attribute__((aligned(ALIGNMENT))) prime16 = 53; int __attribute__((section(".sec17"))) __attribute__((aligned(ALIGNMENT))) prime17 = 59; int __attribute__((section(".sec18"))) __attribute__((aligned(ALIGNMENT))) prime18 = 61; int __attribute__((section(".sec19"))) __attribute__((aligned(ALIGNMENT))) prime19 = 67; int __attribute__((section(".sec20"))) __attribute__((aligned(ALIGNMENT))) prime20 = 71; int __attribute__((section(".sec21"))) __attribute__((aligned(ALIGNMENT))) prime21 = 73; int __attribute__((section(".sec22"))) __attribute__((aligned(ALIGNMENT))) prime22 = 79; int __attribute__((section(".sec23"))) __attribute__((aligned(ALIGNMENT))) prime23 = 83; int __attribute__((section(".sec24"))) __attribute__((aligned(ALIGNMENT))) prime24 = 89; int __attribute__((section(".sec25"))) __attribute__((aligned(ALIGNMENT))) prime25 = 97; int __attribute__((section(".sec26"))) __attribute__((aligned(ALIGNMENT))) prime26 = 101; int __attribute__((section(".sec27"))) __attribute__((aligned(ALIGNMENT))) prime27 = 103; int __attribute__((section(".sec28"))) __attribute__((aligned(ALIGNMENT))) prime28 = 107; int __attribute__((section(".sec29"))) __attribute__((aligned(ALIGNMENT))) prime29 = 109; int __attribute__((section(".sec30"))) __attribute__((aligned(ALIGNMENT))) prime30 = 113; int __attribute__((section(".sec31"))) __attribute__((aligned(ALIGNMENT))) prime31 = 127; int __attribute__((section(".sec32"))) __attribute__((aligned(ALIGNMENT))) prime32 = 131; int __attribute__((section(".sec33"))) __attribute__((aligned(ALIGNMENT))) prime33 = 137; int __attribute__((section(".sec34"))) __attribute__((aligned(ALIGNMENT))) prime34 = 139; int __attribute__((section(".sec35"))) __attribute__((aligned(ALIGNMENT))) prime35 = 149; int __attribute__((section(".sec36"))) __attribute__((aligned(ALIGNMENT))) prime36 = 151; int __attribute__((section(".sec37"))) __attribute__((aligned(ALIGNMENT))) prime37 = 157; int __attribute__((section(".sec38"))) __attribute__((aligned(ALIGNMENT))) prime38 = 163; int __attribute__((section(".sec39"))) __attribute__((aligned(ALIGNMENT))) prime39 = 167; int __attribute__((section(".sec40"))) __attribute__((aligned(ALIGNMENT))) prime40 = 173; int __attribute__((section(".sec41"))) __attribute__((aligned(ALIGNMENT))) prime41 = 179; int __attribute__((section(".sec42"))) __attribute__((aligned(ALIGNMENT))) prime42 = 181; int __attribute__((section(".sec43"))) __attribute__((aligned(ALIGNMENT))) prime43 = 191; int __attribute__((section(".sec44"))) __attribute__((aligned(ALIGNMENT))) prime44 = 193; int __attribute__((section(".sec45"))) __attribute__((aligned(ALIGNMENT))) prime45 = 197; int __attribute__((section(".sec46"))) __attribute__((aligned(ALIGNMENT))) prime46 = 199; int __attribute__((section(".sec47"))) __attribute__((aligned(ALIGNMENT))) prime47 = 211; int __attribute__((section(".sec48"))) __attribute__((aligned(ALIGNMENT))) prime48 = 223; int __attribute__((section(".sec49"))) __attribute__((aligned(ALIGNMENT))) prime49 = 227; int __attribute__((section(".sec50"))) __attribute__((aligned(ALIGNMENT))) prime50 = 229; int __attribute__((section(".sec51"))) __attribute__((aligned(ALIGNMENT))) prime51 = 233; int __attribute__((section(".sec52"))) __attribute__((aligned(ALIGNMENT))) prime52 = 239; int __attribute__((section(".sec53"))) __attribute__((aligned(ALIGNMENT))) prime53 = 241; int __attribute__((section(".sec54"))) __attribute__((aligned(ALIGNMENT))) prime54 = 251; int __attribute__((section(".sec55"))) __attribute__((aligned(ALIGNMENT))) prime55 = 257; int __attribute__((section(".sec56"))) __attribute__((aligned(ALIGNMENT))) prime56 = 263; int __attribute__((section(".sec57"))) __attribute__((aligned(ALIGNMENT))) prime57 = 269; int __attribute__((section(".sec58"))) __attribute__((aligned(ALIGNMENT))) prime58 = 271; int __attribute__((section(".sec59"))) __attribute__((aligned(ALIGNMENT))) prime59 = 277; int __attribute__((section(".sec60"))) __attribute__((aligned(ALIGNMENT))) prime60 = 281; int __attribute__((section(".sec61"))) __attribute__((aligned(ALIGNMENT))) prime61 = 283; int __attribute__((section(".sec62"))) __attribute__((aligned(ALIGNMENT))) prime62 = 293; int __attribute__((section(".sec63"))) __attribute__((aligned(ALIGNMENT))) prime63 = 307; int __attribute__((section(".sec64"))) __attribute__((aligned(ALIGNMENT))) prime64 = 311; int __attribute__((section(".sec65"))) __attribute__((aligned(ALIGNMENT))) prime65 = 313; int __attribute__((section(".sec66"))) __attribute__((aligned(ALIGNMENT))) prime66 = 317; int __attribute__((section(".sec67"))) __attribute__((aligned(ALIGNMENT))) prime67 = 331; int __attribute__((section(".sec68"))) __attribute__((aligned(ALIGNMENT))) prime68 = 337; int __attribute__((section(".sec69"))) __attribute__((aligned(ALIGNMENT))) prime69 = 347; int __attribute__((section(".sec70"))) __attribute__((aligned(ALIGNMENT))) prime70 = 349; int __attribute__((section(".sec71"))) __attribute__((aligned(ALIGNMENT))) prime71 = 353; int __attribute__((section(".sec72"))) __attribute__((aligned(ALIGNMENT))) prime72 = 359; int __attribute__((section(".sec73"))) __attribute__((aligned(ALIGNMENT))) prime73 = 367; int __attribute__((section(".sec74"))) __attribute__((aligned(ALIGNMENT))) prime74 = 373; int __attribute__((section(".sec75"))) __attribute__((aligned(ALIGNMENT))) prime75 = 379; int __attribute__((section(".sec76"))) __attribute__((aligned(ALIGNMENT))) prime76 = 383; int __attribute__((section(".sec77"))) __attribute__((aligned(ALIGNMENT))) prime77 = 389; int __attribute__((section(".sec78"))) __attribute__((aligned(ALIGNMENT))) prime78 = 397; int __attribute__((section(".sec79"))) __attribute__((aligned(ALIGNMENT))) prime79 = 401; int __attribute__((section(".sec80"))) __attribute__((aligned(ALIGNMENT))) prime80 = 409; int __attribute__((section(".sec81"))) __attribute__((aligned(ALIGNMENT))) prime81 = 419; int __attribute__((section(".sec82"))) __attribute__((aligned(ALIGNMENT))) prime82 = 421; int __attribute__((section(".sec83"))) __attribute__((aligned(ALIGNMENT))) prime83 = 431; int __attribute__((section(".sec84"))) __attribute__((aligned(ALIGNMENT))) prime84 = 433; int __attribute__((section(".sec85"))) __attribute__((aligned(ALIGNMENT))) prime85 = 439; int __attribute__((section(".sec86"))) __attribute__((aligned(ALIGNMENT))) prime86 = 443; int __attribute__((section(".sec87"))) __attribute__((aligned(ALIGNMENT))) prime87 = 449; int __attribute__((section(".sec88"))) __attribute__((aligned(ALIGNMENT))) prime88 = 457; int __attribute__((section(".sec89"))) __attribute__((aligned(ALIGNMENT))) prime89 = 461; int __attribute__((section(".sec90"))) __attribute__((aligned(ALIGNMENT))) prime90 = 463; int __attribute__((section(".sec91"))) __attribute__((aligned(ALIGNMENT))) prime91 = 467; int __attribute__((section(".sec92"))) __attribute__((aligned(ALIGNMENT))) prime92 = 479; int __attribute__((section(".sec93"))) __attribute__((aligned(ALIGNMENT))) prime93 = 487; int __attribute__((section(".sec94"))) __attribute__((aligned(ALIGNMENT))) prime94 = 491; int __attribute__((section(".sec95"))) __attribute__((aligned(ALIGNMENT))) prime95 = 499; int __attribute__((section(".sec96"))) __attribute__((aligned(ALIGNMENT))) prime96 = 503; int __attribute__((section(".sec97"))) __attribute__((aligned(ALIGNMENT))) prime97 = 509; int __attribute__((section(".sec98"))) __attribute__((aligned(ALIGNMENT))) prime98 = 521; int __attribute__((section(".sec99"))) __attribute__((aligned(ALIGNMENT))) prime99 = 523; int __attribute__((section(".sec100"))) __attribute__((aligned(ALIGNMENT))) prime100 = 541; int main() { int input; printf("Which of the first hundred prime numbers would you like? "); scanf("%d", &input); if (input <= 0 || input > 100) { printf("Invalid query. Exiting...\n"); return 1; } // We assume that the primeX variables are stored in-order printf("%d\n", *(&prime1 + ALIGNMENT/sizeof(int) * (input - 1))); return 0; }