-
Notifications
You must be signed in to change notification settings - Fork 4
/
Macros.mk
1608 lines (1299 loc) · 48.5 KB
/
Macros.mk
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# Naming convention for macros declared here:
#
# Macros named with a leading underscore must be eval-ed after calling them,
# whereas those without a leading underscore do not. Whenever a macro that
# requires eval-ing is defined, both macros with and without the leading
# underscore are defined, where the one without is defined as eval-ing the
# underscore-prefixed version.
#
# For example, macro named _make_rule must be called using:
# $(eval $(call _make_rule,args...))
# whereas a macro named make_rule must be called using:
# $(call make_rule,args)
# Also, when both forms are present, make_rule is defined like so:
# make_rule = $(eval $(call _make_rule,args...))
# In this way, _make_rule is recursively expanded in the call context and
# therefore has access to the parameter variables $1, $2, etc that come
# from calling make_rule.
#
# Because the only difference between the two forms is that the one without
# the leading underscore is eval-ed, when defining such a macro that needs
# to call other macros, call the non-eval-ed version. That prevents double
# evaluation, since otherwise the macro will be evaluated when it is called
# and again when the calling macro is eval-ed.
# Set defaults
ifndef DEFAULT_BITS
DEFAULT_BITS := 64
endif
ifndef DEFAULT_CPPFLAGS
DEFAULT_CPPFLAGS :=
endif
ifndef DEFAULT_CFLAGS
DEFAULT_CFLAGS :=
endif
ifndef DEFAULT_CXXFLAGS
DEFAULT_CXXFLAGS :=
endif
ifndef DEFAULT_OFLAGS
DEFAULT_OFLAGS := -O0
endif
ifndef DEFAULT_ASFLAGS
DEFAULT_ASFLAGS :=
endif
ifndef DEFAULT_LDFLAGS
DEFAULT_LDFLAGS :=
endif
ifndef DEFAULT_CC
DEFAULT_CC := gcc
endif
ifndef DEFAULT_CXX
DEFAULT_CXX := g++
endif
ifndef DEFAULT_AS
DEFAULT_AS := gcc
endif
ifndef DEFAULT_LD
DEFAULT_LD :=
endif
ifndef DEFAULT_AR
DEFAULT_AR := ar
endif
ifndef DEFAULT_RELRO
DEFAULT_RELRO := 0
endif
ifndef DEFAULT_CANARY
DEFAULT_CANARY := none
endif
ifndef DEFAULT_NX
DEFAULT_NX := 0
endif
ifndef DEFAULT_PIE
DEFAULT_PIE :=
endif
ifndef DEFAULT_ASLR
DEFAULT_ASLR :=
endif
ifndef DEFAULT_STRIP
DEFAULT_STRIP := 0
endif
ifndef DEFAULT_DEBUG
DEFAULT_DEBUG := 0
endif
ifndef DEFAULT_UBUNTU_VERSION
DEFAULT_UBUNTU_VERSION := 24.04
endif
ifndef DEFAULT_DOCKER_CPULIMIT
# Default to half of a virtual CPU core
DEFAULT_DOCKER_CPULIMIT := 0.5
endif
ifndef DEFAULT_DOCKER_MEMLIMIT
# Default to 500MB of RAM usage total
# This can be measured with `docker stats <container>`. As a baseline, I
# checked a basic challenge. It uses 3.2MB when sitting idle without any
# connections, then jumps to 5.8MB with an active connection. That's 2.6MB
# per connection (for a basic challenge). If we overestimate this to 10MB
# per connection and want to support 50 concurrent connections, this gives
# us 500MB. Challenges that need more memory can of course override this
# default by setting `DOCKER_MEMLIMIT`.
DEFAULT_DOCKER_MEMLIMIT := 500m
endif
ifndef DEFAULT_DOCKER_TIMELIMIT
DEFAULT_DOCKER_TIMELIMIT :=
endif
ifndef DEFAULT_DOCKER_PASSWORD
DEFAULT_DOCKER_PASSWORD :=
endif
# Any of these values indicate that a variable is "true"
TRUE_VALUES := 1 true True TRUE yes y Yes Y YES on On ON
# Any of these values indicate that a variable is "false"
FALSE_VALUES := 0 false False FALSE no n No N NO off Off OFF
# Returns 1 when $1 is an empty string
is_value_empty = $(if $1,,1)
# Returns 1 when $1 is in $(TRUE_VALUES)
is_value_true = $(if $(filter $1,$(TRUE_VALUES)),1)
# Returns 1 when $1 is in $(FALSE_VALUES)
is_value_false = $(if $(filter $1,$(FALSE_VALUES)),1)
# Returns 1 when $1 names an undefined variable
is_var_undefined = $(if $(filter $(origin $1),undefined),1)
# Returns 1 when $1 names a defined variable
is_var_defined = $(if $(call is_var_undefined,$1),,1)
# Returns 1 when $1 names a variable whose value is in $(TRUE_VALUES)
is_var_true = $(and $(call is_var_defined,$1),$(call is_value_true,$($1)))
# Returns 1 when $1 names a variable whose value is in $(FALSE_VALUES)
is_var_false = $(and $(call is_var_defined,$1),$(call is_value_false,$($1)))
# Returns 1 when $1 names an undefined variable or when $1's value is the empty string or in $(FALSE_VALUES)
is_var_false_or_undefined = $(or $(call is_var_undefined,$1),$(call is_value_empty,$($1)),$(call is_value_false,$($1)))
#####
# generate_target($1: subdirectory, $2: target)
#
# Generate the rules to build the given target contained within the given directory.
#####
define _generate_target
ifdef MKDEBUG
$$(info Generating target rules for $1+$2)
endif
ifdef MKTRACE
$$(info Tracing _generate_target($1,$2)...)
endif #MKTRACE
# Product path
ifeq "$$(origin $2_PRODUCT)" "undefined"
$2_PRODUCT := $$(or $$($1+PRODUCT),$$($1+BUILD)/$2)
endif
# Ensure that target_BITS has a value, default to 32-bits
ifeq "$$(origin $2_BITS)" "undefined"
$2_BITS := $$($1+BITS)
endif
# Check that BITS doesn't conflict with CONFIG_IGNORE_32BIT
ifndef $1+THIS_IS_THE_CORE_PROJECT
ifeq "$$($2_BITS)" "32"
ifdef CONFIG_IGNORE_32BIT
$$(error $1/$2: Requesting 32-bit binaries, but CONFIG_IGNORE_32BIT is set!)
endif #CONFIG_IGNORE_32BIT
endif #BITS==32
endif #THIS_IS_THE_CORE_PROJECT
ifeq "$$(origin $2_NO_EXTRA_FLAGS)" "undefined"
$2_NO_EXTRA_FLAGS := $$($1+NO_EXTRA_FLAGS)
endif
# Ensure that target_CPPFLAGS is defined
ifeq "$$(origin $2_CPPFLAGS)" "undefined"
$2_CPPFLAGS := $$($1+CPPFLAGS)
endif
ifeq "$$(origin $2_NO_EXTRA_CPPFLAGS)" "undefined"
$2_NO_EXTRA_CPPFLAGS := $$(or $$($2_NO_EXTRA_FLAGS),$$($1+NO_EXTRA_CPPFLAGS))
endif
$2_EXTRA_CPPFLAGS :=
# Ensure that target_CFLAGS is defined
ifeq "$$(origin $2_CFLAGS)" "undefined"
$2_CFLAGS := $$($1+CFLAGS)
endif
ifeq "$$(origin $2_NO_EXTRA_CFLAGS)" "undefined"
$2_NO_EXTRA_CFLAGS := $$(or $$($2_NO_EXTRA_FLAGS),$$($1+NO_EXTRA_CFLAGS))
endif
$2_EXTRA_CFLAGS :=
# Ensure that target_CXXFLAGS is defined
ifeq "$$(origin $2_CXXFLAGS)" "undefined"
$2_CXXFLAGS := $$($1+CXXFLAGS)
endif
ifeq "$$(origin $2_NO_EXTRA_CXXFLAGS)" "undefined"
$2_NO_EXTRA_CXXFLAGS := $$(or $$($2_NO_EXTRA_FLAGS),$$($1+NO_EXTRA_CXXFLAGS))
endif
$2_EXTRA_CXXFLAGS :=
# Ensure that target_OFLAGS has a value, default to no optimization
ifeq "$$(origin $2_OFLAGS)" "undefined"
$2_OFLAGS := $$($1+OFLAGS)
endif
ifeq "$$(origin $2_NO_EXTRA_OFLAGS)" "undefined"
$2_NO_EXTRA_OFLAGS := $$(or $$($2_NO_EXTRA_FLAGS),$$($1+NO_EXTRA_OFLAGS))
endif
$2_EXTRA_OFLAGS :=
# Ensure that target_ASFLAGS is defined
ifeq "$$(origin $2_ASFLAGS)" "undefined"
$2_ASFLAGS := $$($1+ASFLAGS)
endif
ifeq "$$(origin $2_NO_EXTRA_ASFLAGS)" "undefined"
$2_NO_EXTRA_ASFLAGS := $$(or $$($2_NO_EXTRA_FLAGS),$$($1+NO_EXTRA_ASFLAGS))
endif
$2_EXTRA_ASFLAGS := -D__ASSEMBLY__
# Ensure that target_LDFLAGS is defined
ifeq "$$(origin $2_LDFLAGS)" "undefined"
$2_LDFLAGS := $$($1+LDFLAGS)
endif
ifeq "$$(origin $2_NO_EXTRA_LDFLAGS)" "undefined"
$2_NO_EXTRA_LDFLAGS := $$(or $$($2_NO_EXTRA_FLAGS),$$($1+NO_EXTRA_LDFLAGS))
endif
$2_EXTRA_LDFLAGS :=
# Ensure that target_SRCS has a value, default to searching for all C and
# C++ sources in the same directory as Build.mk.
ifeq "$$(origin $2_SRCS)" "undefined"
$2_SRCS := $$($1+SRCS)
else
# Prefix each item with the project directory
$2_SRCS := $$(addprefix $1/,$$($2_SRCS))
endif
# Ensure that target_OBJS_DIR has a value, defaulting to .build/proj/target_objs
ifeq "$$(origin $2_OBJS_DIR)" "undefined"
$2_OBJS_DIR := $$($1+BUILD)/$2_objs
endif
# Ensure that target_OBJS has a value, default to modifying the value of each
# src from target_SRCS into target_BUILD/src.o
# Example: generate_target(proj, target) with main.cpp -> .build/proj/target_objs/main.cpp.o
ifeq "$$(origin $2_OBJS)" "undefined"
$2_OBJS := $$(patsubst $1/%,$$($2_OBJS_DIR)/%.o,$$($2_SRCS))
endif
# Ensure that target_DEPS has a value, default to the value of target_OBJS
# but with .o extensions replaced with .d.
ifeq "$$(origin $2_DEPS)" "undefined"
$2_DEPS := $$($2_OBJS:.o=.d)
endif
# Ensure that target_CC has a value
ifeq "$$(origin $2_CC)" "undefined"
$2_CC := $$($1+CC)
endif
# Ensure that target_CXX has a value
ifeq "$$(origin $2_CXX)" "undefined"
$2_CXX := $$($1+CXX)
endif
# Ensure that target_AS has a value
ifeq "$$(origin $2_AS)" "undefined"
$2_AS := $$($1+AS)
endif
# Ensure that target_LD has a value, defaulting to target_CC unless there are
# C++ sources, in which case target_CXX is used instead
ifeq "$$(origin $2_LD)" "undefined"
$2_LD := $$(or $$($1+LD),$$(if $$(filter %.cpp,$$($2_SRCS)),$$($2_CXX),$$($2_CC)))
endif
# Ensure that target_AR has a value
ifeq "$$(origin $2_AR)" "undefined"
$2_AR := $$($1+AR)
endif
ifeq "$$(origin $2_UBUNTU_VERSION)" "undefined"
$2_UBUNTU_VERSION := $$($1+UBUNTU_VERSION)
endif
# Define UBUNTU_VERSION_NUMBER as the numeric version for Ubuntu (even when UBUNTU_VERSION is the named alias)
ifdef UBUNTU_VERSION_TO_ALIAS[$$($2_UBUNTU_VERSION)]
$2_UBUNTU_VERSION_NUMBER := $$($2_UBUNTU_VERSION)
else ifdef UBUNTU_ALIAS_TO_VERSION[$$($2_UBUNTU_VERSION)]
$2_UBUNTU_VERSION_NUMBER := $$(UBUNTU_ALIAS_TO_VERSION[$$($2_UBUNTU_VERSION)])
else
$$(error Unknown Ubuntu version in $1/$2: "$$($2_UBUNTU_VERSION)")
endif
# Split Ubuntu version number into major, minor, and int. 24.04 -> major=24, minor=04, int=2404
$2_UBUNTU_VERSION_PARTS := $$(subst .,$$(SPACE),$$($2_UBUNTU_VERSION_NUMBER))
$2_UBUNTU_VERSION_MAJOR := $$(firstword $$($2_UBUNTU_VERSION_PARTS))
$2_UBUNTU_VERSION_MINOR := $$(word 2,$$($2_UBUNTU_VERSION_PARTS))
$2_UBUNTU_VERSION_INT := $$($2_UBUNTU_VERSION_MAJOR)$$($2_UBUNTU_VERSION_MINOR)
# Run compiler commands in a pwncc container?
$2_PWNCC :=
$2_PWNCC_DEPS :=
$2_PWNCC_DESC :=
ifdef CONFIG_USE_PWNCC
$$(call pwncc_prepare,$1,$$($2_UBUNTU_VERSION),$2_PWNCC,$2_PWNCC_DEPS)
# Format string for a printed message prefix
$2_PWNCC_DESC := [$$($2_UBUNTU_VERSION)]$$(SPACE)
endif #CONFIG_USE_PWNCC
# Ensure that target_BINTYPE has a value, defaulting to "dynamiclib" if target
# name ends in ".so" otherwise "executable".
ifeq "$$(origin $2_BINTYPE)" "undefined"
ifdef $1+BINTYPE
$2_BINTYPE := $$($1+BINTYPE)
else ifeq "$$(suffix $2)" ".so"
$2_BINTYPE := dynamiclib
else ifeq "$$(suffix $2)" ".a"
$2_BINTYPE := staticlib
else #BINTYPE & suffix .so/a
$2_BINTYPE := executable
endif #BINTYPE & suffix .so/a
endif #target_BINTYPE undefined
# Ensure that target_LIBS has a value
ifeq "$$(origin $2_LIBS)" "undefined"
$2_LIBS := $$($1+LIBS)
endif #target_LIBS undefined
# Ensure that target_USE_LIBPWNABLEHARNESS has a value
ifeq "$$(origin $2_USE_LIBPWNABLEHARNESS)" "undefined"
$2_USE_LIBPWNABLEHARNESS := $$($1+USE_LIBPWNABLEHARNESS)
endif
# Ensure that target_NO_UNBUFFERED_STDIO has a value
ifeq "$$(origin $2_NO_UNBUFFERED_STDIO)" "undefined"
$2_NO_UNBUFFERED_STDIO := $$($1+NO_UNBUFFERED_STDIO)
endif
# Ensure that target_NO_RPATH has a value
ifeq "$$(origin $2_NO_RPATH)" "undefined"
$2_NO_RPATH := $$($1+NO_RPATH)
endif
# Ensure that target_LDLIBS has a value
ifeq "$$(origin $2_LDLIBS)" "undefined"
$2_LDLIBS := $$($1+LDLIBS)
endif #target_LDLIBS undefined
# Add dependency on libpwnableharness(32|64).so if requested
ifdef $2_USE_LIBPWNABLEHARNESS
$2_ALLLIBS := $$($2_LIBS) $$(BUILD)/core/$$($2_UBUNTU_VERSION_NUMBER)/libpwnableharness$$($2_BITS).so
# When not building PwnableHarness core, just extract the prebuilt libpwnableharness*.so library out of the Docker image
ifndef $1+THIS_IS_THE_CORE_PROJECT
ifndef DEFINED_GRAB_LIBPWNABLEHARNESS$$($2_BITS)_$$($2_UBUNTU_VERSION_NUMBER)
DEFINED_GRAB_LIBPWNABLEHARNESS$$($2_BITS)_$$($2_UBUNTU_VERSION_NUMBER) := 1
$$(BUILD)/core/$$($2_UBUNTU_VERSION_NUMBER)/libpwnableharness$$($2_BITS).so:
$$(_V)echo "Pulling $$($1+DOCKER_FULL_BASE) (if necessary)"
$$(_v)$$(DOCKER) pull $$($1+DOCKER_PLATFORM) $$($1+DOCKER_FULL_BASE)
$$(_V)echo "Copying libpwnableharness$$($2_BITS).so from $$($1+DOCKER_FULL_BASE)"
$$(_v)mkdir -p $$(@D) && $$(DOCKER) run $$($1+DOCKER_PLATFORM) --rm --entrypoint /bin/cat $$($1+DOCKER_FULL_BASE) /usr/lib/libpwnableharness$$($2_BITS).so > $$@
endif #DEFINED_GRAB_LIBPWNABLEHARNESS
ifndef DEFINED_CLEAN_LIBPWNABLEHARNESS_$$($2_UBUNTU_VERSION_NUMBER)
DEFINED_CLEAN_LIBPWNABLEHARNESS_$$($2_UBUNTU_VERSION_NUMBER) := 1
clean: clean-libpwnableharness-$$($2_UBUNTU_VERSION_NUMBER)
.PHONY: clean-libpwnableharness-$$($2_UBUNTU_VERSION_NUMBER)
clean-libpwnableharness-$$($2_UBUNTU_VERSION_NUMBER):
$$(_v)rm -rf $$(BUILD)/core/$$($2_UBUNTU_VERSION_NUMBER)
endif #DEFINED_CLEAN_LIBPWNABLEHARNESS
endif #THIS_IS_THE_CORE_PROJECT
else #USE_LIBPWNABLEHARNESS
$2_ALLLIBS := $$($2_LIBS)
endif #USE_LIBPWNABLEHARNESS
# Allow loading shared libraries from the executable directory
ifeq "1" "$$(call is_var_false_or_undefined,$2_NO_RPATH)"
ifdef IS_LINUX
$2_EXTRA_LDFLAGS += -Wl,-rpath,`printf "\044"`ORIGIN -Wl,-z,origin
else ifdef IS_MAC
$2_EXTRA_LDFLAGS += -Wl,-rpath,@executable_path
endif #IS_LINUX/IS_MAC
endif #NO_RPATH
# On macOS, dylibs need an "install name" to allow them to be loaded from the
# executable's directory
ifeq "$$($2_BINTYPE)" "dynamiclib"
ifdef IS_MAC
$2_EXTRA_LDFLAGS += -install_name @rpath/$2
endif #IS_MAC
endif #dynamiclib
# Convert a list of dynamic library names into linker arguments
ifdef IS_LINUX
$2_LIBPATHS := $$(sort $$(patsubst %/,%,$$(dir $$($2_ALLLIBS))))
$2_EXTRA_LDFLAGS += $$(addprefix -L,$$($2_LIBPATHS))
$2_LDLIBS += $$(addprefix -l:,$$(notdir $$($2_ALLLIBS)))
else ifdef IS_MAC
$2_LDLIBS += $$($2_ALLLIBS)
endif
## Hardening flags
# Ensure that target_RELRO has a value
ifeq "$$(origin $2_RELRO)" "undefined"
$2_RELRO := $$($1+RELRO)
endif
# Ensure that target_CANARY has a value
ifeq "$$(origin $2_CANARY)" "undefined"
$2_CANARY := $$($1+CANARY)
endif
# Ensure that target_NX has a value
ifeq "$$(origin $2_NX)" "undefined"
$2_NX := $$($1+NX)
endif
# Ensure that target_PIE has a value
ifeq "$$(origin $2_PIE)" "undefined"
$2_PIE := $$($1+PIE)
endif
# Ensure that target_ASLR has a value
ifeq "$$(origin $2_ASLR)" "undefined"
$2_ASLR := $$($1+ASLR)
endif
# Ensure that target_STRIP has a value
ifeq "$$(origin $2_STRIP)" "undefined"
$2_STRIP := $$($1+STRIP)
endif
# Ensure that target_DEBUG has a value
ifeq "$$(origin $2_DEBUG)" "undefined"
$2_DEBUG := $$($1+DEBUG)
endif
## Apply hardening flags
# RELRO (Read-only relocations), only works on Linux
ifdef IS_LINUX
ifeq "1" "$$(call is_var_false_or_undefined,$2_RELRO)"
$2_EXTRA_LDFLAGS += -Wl,-z,norelro
else ifeq "1" "$$(call is_var_true,$2_RELRO)"
$2_EXTRA_LDFLAGS += -Wl,-z,relro,-z,now
else ifeq "$$($2_RELRO)" "partial"
# Nothing
else ifeq "$$($2_RELRO)" "default"
# Nothing
else #RELRO
$$(error Unknown value for RELRO in $1/$2: "$$($2_RELRO)". Possible values: 0 1 partial default)
endif #RELRO
endif #IS_LINUX
# Map user-provided value of CANARY to a valid value
ifeq "1" "$$(call is_var_false_or_undefined,$2_CANARY)"
# False-ish values
$2_CANARY := none
else ifeq "1" "$$(call is_var_true,$2_CANARY)"
# True-ish values imply strong (except gcc in Ubuntu <= 14.04 doesn't support that)
# Just use "all" for now: https://github.com/C0deH4cker/PwnableHarness/issues/54
$2_CANARY := all
else ifeq "" "$$(filter $$($2_CANARY),all strong explicit normal default none)"
$$(error Unknown value for CANARY in $1/$2: "$$($2_CANARY)". Possible values: all strong explicit normal default none)
endif #CANARY
# Apply CANARY setting to flags
ifeq "$$($2_CANARY)" "all"
$2_CANARY_FLAG := -fstack-protector-all
else ifeq "$$($2_CANARY)" "strong"
$2_CANARY_FLAG := -fstack-protector-strong
else ifeq "$$($2_CANARY)" "explicit"
$2_CANARY_FLAG := -fstack-protector-explicit
else ifeq "$$($2_CANARY)" "normal"
$2_CANARY_FLAG := -fstack-protector
else ifeq "$$($2_CANARY)" "default"
# Nothing
$2_CANARY_FLAG :=
else ifeq "$$($2_CANARY)" "none"
$2_CANARY_FLAG := -fno-stack-protector
endif #CANARY
$2_CFLAGS += $$($2_CANARY_FLAG)
$2_CXXFLAGS += $$($2_CANARY_FLAG)
# NX (No Execute) aka DEP (Data Execution Prevention) aka W^X (Write XOR eXecute)
ifeq "1" "$$(call is_var_false_or_undefined,$2_NX)"
ifdef IS_LINUX
$2_EXTRA_LDFLAGS += -z execstack
else ifdef IS_MAC
$2_EXTRA_LDFLAGS += -Wl,-allow_stack_execute
endif #OS
else ifeq "1" "$$(call is_var_true,$2_NX)"
ifdef IS_LINUX
$2_EXTRA_LDFLAGS += -z noexecstack
else ifdef IS_MAC
# No option, default is stack isn't executable
endif #OS
else ifeq "$$($2_NX)" "default"
# Nothing
else #NX
$$(error Unknown value for NX in $1/$2: "$$($2_NX)". Possible values: 0 1 default)
endif #NX
# PIE is an alias for ASLR
ifdef $2_PIE
ifdef $2_ASLR
$$(error Both ASLR and PIE are defined! Pick one.)
endif #ASLR
$2_ASLR := $$($2_PIE)
$2_ASLR_VAR := PIE
else #PIE
$2_ASLR_VAR := ASLR
endif #PIE
# ASLR (Address Space Layout Randomization)
ifeq "1" "$$(call is_var_false_or_undefined,$2_ASLR)"
ifeq "$$($2_BINTYPE)" "executable"
ifdef IS_LINUX
$2_EXTRA_LDFLAGS += -no-pie
else ifdef IS_MAC
$2_EXTRA_LDFLAGS += -Wl,-no_pie
endif #IS_LINUX/IS_MAC
endif #executable
else ifeq "$$($2_ASLR)" "default"
# Nothing
else ifeq "1" "$$(call is_var_true,$2_ASLR)"
ifeq "$$($2_BINTYPE)" "executable"
$2_EXTRA_CFLAGS += -fPIE
$2_EXTRA_CXXFLAGS += -fPIE
ifdef IS_LINUX
$2_EXTRA_LDFLAGS += -pie
else ifdef IS_MAC
$2_EXTRA_LDFLAGS += -Wl,-pie
endif #IS_LINUX/IS_MAC
else #executable
$2_EXTRA_CFLAGS += -fPIC
$2_EXTRA_CXXFLAGS += -fPIC
endif #executable
else #ASLR
$$(error Unknown value for $$($2_ASLR_VAR) in $1/$2: "$$($2_ASLR)". Possible values: 0 1 default)
endif #ASLR
# Strip symbols
ifeq "1" "$$(call is_var_true,$2_STRIP)"
ifdef IS_LINUX
$2_EXTRA_LDFLAGS += -Wl,-s
else ifdef IS_MAC
$2_EXTRA_LDFLAGS += -Wl,-S,-x
endif #IS_LINUX/IS_MAC
endif #STRIP
# Debug symbols
ifeq "1" "$$(call is_var_false_or_undefined,$2_DEBUG)"
$2_EXTRA_CPPFLAGS += -DNDEBUG=1 -UDEBUG
else ifeq "$$($2_DEBUG)" "default"
# Nothing
else #DEBUG
$2_EXTRA_CPPFLAGS += -DDEBUG=1 -UNDEBUG
ifeq "1" "$$(call is_var_true,$2_DEBUG)"
$2_EXTRA_OFLAGS += -ggdb
else #!is_true(DEBUG)
$2_EXTRA_OFLAGS += $$($2_DEBUG)
endif #!is_true(DEBUG)
endif #DEBUG
# Add project directory to include path
$2_EXTRA_CPPFLAGS += -I$1
# Combine user-provided flags with PwnableHarness-generated flags
$2_ALL_CPPFLAGS := $$(if $$($2_NO_EXTRA_CPPFLAGS),,$$($2_EXTRA_CPPFLAGS) )$$($2_CPPFLAGS)
$2_ALL_ASFLAGS := $$(if $$($2_NO_EXTRA_ASFLAGS),,$$($2_EXTRA_ASFLAGS) )$$($2_ASFLAGS)
$2_ALL_CFLAGS := $$(if $$($2_NO_EXTRA_CFLAGS),,$$($2_EXTRA_CFLAGS) )$$($2_CFLAGS)
$2_ALL_CXXFLAGS := $$(if $$($2_NO_EXTRA_CXXFLAGS),,$$($2_EXTRA_CXXFLAGS) )$$($2_CXXFLAGS)
$2_ALL_OFLAGS := $$(if $$($2_NO_EXTRA_OFLAGS),,$$($2_EXTRA_OFLAGS) )$$($2_OFLAGS)
$2_ALL_LDFLAGS := $$(if $$($2_NO_EXTRA_LDFLAGS),,$$($2_EXTRA_LDFLAGS) )$$($2_LDFLAGS)
ifeq "$$($2_BINTYPE)" "executable"
# Build and link in the stdio_unbuffer.c source file unless opted out
ifndef $2_NO_UNBUFFERED_STDIO
$2_OBJS += $$($1+BUILD)/$2_objs/stdio_unbuffer.o
ifdef MKTRACE
$$(info Adding rule for $1+$2's stdio_unbuffer.o)
endif #MKTRACE
ifndef UNBUFFER_DIR
ifdef CONTAINER_BUILD
UNBUFFER_DIR := $$(BUILD)/core
# Copy from pwnmake's ROOT_DIR to the workspace's .build directory. This is done
# so that the pwncc image is able to access it (as it can't access files in the
# pwnmake image).
$$(UNBUFFER_DIR)/stdio_unbuffer.c: $$(ROOT_DIR)/stdio_unbuffer.c | $$(UNBUFFER_DIR)/.dir
$$(_v)cp $$< $$@
clean: clean-unbuffer
.PHONY: clean-unbuffer
clean-unbuffer:
$$(_v)rm -f $$(UNBUFFER_DIR)/stdio_unbuffer.c
else #CONTAINER_BUILD
UNBUFFER_DIR := $$(ROOT_DIR)
endif #CONTAINER_BUILD
endif #UNBUFFER_DIR
# Compiler rule for stdio_unbuffer.o
$$($1+BUILD)/$2_objs/stdio_unbuffer.o: $$(UNBUFFER_DIR)/stdio_unbuffer.c $$($2_PWNCC_DEPS)
$$(_V)echo "$$($2_PWNCC_DESC)Compiling $$(<F) for $$(patsubst ./%,%,$1/$2)"
$$(_v)$$($2_PWNCC)$$($2_CC) -m$$($2_BITS) $$($2_ALL_CPPFLAGS) $$($2_ALL_CFLAGS) $$($2_ALL_OFLAGS) -MD -MP -MF $$(@:.o=.d) -c -o $$@ $$<
endif #NO_UNBUFFERED_STDIO
endif #BINTYPE == executable
ifdef MKTRACE
$$(info Adding build deps for $1+$2)
endif #MKTRACE
# Ensure directories are created for all object files
$2_OBJ_DIR_RULES := $$(addsuffix /.dir,$$(sort $$(patsubst %/,%,$$(dir $$($2_OBJS)))))
$$($2_OBJS): | $$($2_OBJ_DIR_RULES)
# Rebuild all build products when the Build.mk is modified
$$($2_OBJS): $$($1+BUILD_MK) $$(ROOT_DIR)/Macros.mk
$$($2_PRODUCT): $$($1+BUILD_MK) $$(ROOT_DIR)/Macros.mk
ifdef MKTRACE
$$(info Adding compiler rules for $1+$2)
endif #MKTRACE
# Compiler rule for C sources
$$(filter %.c.o,$$($2_OBJS)): $$($1+BUILD)/$2_objs/%.c.o: $1/%.c $$($2_PWNCC_DEPS)
$$(_V)echo "$$($2_PWNCC_DESC)Compiling $$< for $$(patsubst ./%,%,$1/$2)"
$$(_v)$$($2_PWNCC)$$($2_CC) -m$$($2_BITS) $$($2_ALL_CPPFLAGS) $$($2_ALL_CFLAGS) $$($2_ALL_OFLAGS) -MD -MP -MF $$(@:.o=.d) -c -o $$@ $$<
# Compiler rule for C++ sources
$$(filter %.cpp.o,$$($2_OBJS)): $$($1+BUILD)/$2_objs/%.cpp.o: $1/%.cpp $$($2_PWNCC_DEPS)
$$(_V)echo "$$($2_PWNCC_DESC)Compiling $$< for $$(patsubst ./%,%,$1/$2)"
$$(_v)$$($2_PWNCC)$$($2_CXX) -m$$($2_BITS) $$($2_ALL_CPPFLAGS) $$($2_ALL_CXXFLAGS) $$($2_ALL_OFLAGS) -MD -MP -MF $$(@:.o=.d) -c -o $$@ $$<
# Assembler rule
$$(filter %.S.o,$$($2_OBJS)): $$($1+BUILD)/$2_objs/%.S.o: $1/%.S $$($2_PWNCC_DEPS)
$$(_V)echo "$$($2_PWNCC_DESC)Assembling $$< for $$(patsubst ./%,%,$1/$2)"
$$(_v)$$($2_PWNCC)$$($2_AS) -m$$($2_BITS) $$($2_ALL_CPPFLAGS) $$($2_ALL_ASFLAGS) -MD -MP -MF $$(@:.o=.d) -c -o $$@ $$<
clean-one[$1]: clean-objs[$1+$2]
$$(call add_phony_target,clean-objs[$1+$2])
clean-objs[$1+$2]:
$$(_v)rm -rf $$($2_OBJS_DIR)
ifdef MKTRACE
$$(info Including dependency files for $1/$2)
endif #MKTRACE
# Compilation dependency rules
-include $$($2_DEPS)
$2_PRODUCT_DIR_RULE := $$(patsubst %/,%,$$(dir $$($2_PRODUCT)))/.dir
ifdef MKTRACE
$$(info Adding linker rules for $1/$2)
endif #MKTRACE
ifeq "$$($2_BINTYPE)" "executable"
# Linker rule to produce the final target (specialization for executables)
$$($2_PRODUCT): $$($2_OBJS) $$($2_ALLLIBS) $$($2_PWNCC_DEPS) | $$($2_PRODUCT_DIR_RULE)
$$(_V)echo "$$($2_PWNCC_DESC)Linking executable $$@"
$$(_v)$$($2_PWNCC)$$($2_LD) -m$$($2_BITS) $$($2_ALL_OFLAGS) $$($2_ALL_LDFLAGS) \
-o $$@ $$($2_OBJS) $$($2_LDLIBS)
else ifeq "$$($2_BINTYPE)" "dynamiclib"
# Linker rule to produce the final target (specialization for shared libraries)
$$($2_PRODUCT): $$($2_OBJS) $$($2_ALLLIBS) $$($2_PWNCC_DEPS) | $$($2_PRODUCT_DIR_RULE)
$$(_V)echo "$$($2_PWNCC_DESC)Linking shared library $$@"
$$(_v)$$($2_PWNCC)$$($2_LD) -m$$($2_BITS) -shared $$($2_ALL_OFLAGS) $$($2_ALL_LDFLAGS) \
-o $$@ $$($2_OBJS) $$($2_LDLIBS)
else ifeq "$$($2_BINTYPE)" "staticlib"
# Archive rule to produce the final target (specialication for static libraries)
$$($2_PRODUCT): $$($2_OBJS) $$($2_PWNCC_DEPS) | $$($2_PRODUCT_DIR_RULE)
$$(_V)echo "$$($2_PWNCC_DESC)Archiving static library $$@"
$$(_v)$$($2_PWNCC)$$($2_AR) rcs $$@ $$($2_OBJS)
else #dynamiclib & executable & staticlib
# Assume that the user will provide their own linker rule here
ifdef MKDEBUG
$$(info Not generating a linker rule for $1/$2 because its BINTYPE is "$$($2_BINTYPE)")
endif #MKDEBUG
endif #dynamiclib & executable & staticlib
ifdef MKTRACE
$$(info Done generating target rules for $1/$2)
endif #MKTRACE
endef #_generate_target
generate_target = $(eval $(call _generate_target,$1,$2))
#####
#####
# docker_compose($1: project directory)
#
# Rules for deploying a docker-compose project
#####
define _docker_compose
$$(call add_phony_target,docker-build[$1])
docker-build[$1]: docker-build-one[$1]
$$(call add_phony_target,docker-build-one[$1])
docker-build-one[$1]: docker-rebuild-one[$1]
$$(call add_phony_target,docker-rebuild[$1])
docker-rebuild[$1]: docker-rebuild-one[$1]
$$(call add_phony_target,docker-rebuild-one[$1])
docker-rebuild-one[$1]:
$$(_V)echo "Building images with docker-compose in $1"
$$(_v)cd $1 && docker-compose build
$$(call add_phony_target,docker-start[$1])
docker-start[$1]: docker-start-one[$1]
$$(call add_phony_target,docker-start-one[$1])
docker-start-one[$1]:
$$(_V)echo "Starting containers with docker-compose in $1"
$$(_v)cd $1 && docker-compose up -d
$$(call add_phony_target,docker-restart[$1])
docker-restart[$1]: docker-restart-one[$1]
$$(call add_phony_target,docker-restart-one[$1])
docker-restart-one[$1]:
$$(_V)echo "Restarting containers with docker-compose in $1"
$$(_v)cd $1 && docker-compose restart
$$(call add_phony_target,docker-stop[$1])
docker-stop[$1]: docker-stop-one[$1]
$$(call add_phony_target,docker-stop-one[$1])
docker-stop-one[$1]:
$$(_V)echo "Stopping containers with docker-compose in $1"
$$(_v)cd $1 && docker-compose down
$$(call add_phony_target,docker-clean[$1])
docker-clean[$1]: docker-clean-one[$1]
$$(call add_phony_target,docker-clean-one[$1])
docker-clean-one[$1]:
$$(_V)echo "Removing containers with docker-compose in $1"
$$(_v)cd $1 && docker-compose rm --stop
endef #_docker_compose
docker_compose = $(eval $(call _docker_compose,$1))
#####
#####
# add_publish_rule($1: project directory, $2: path containing files to publish, $3: list of files under $2 to publish)
#
# Define a rule for copying a file to a project's publish directory
#####
define _add_publish_rule
ifdef MKDEBUG
$$(info add_publish_rule($1,$2,$3))
endif #MKDEBUG
$1+$2+PUB := $$(PUB_DIR)/$$(patsubst /%,%,$1)
$1+$2+DST := $$(addprefix $$($1+$2+PUB)/,$$(notdir $3))
publish-one[$1]: $$($1+$2+DST)
# Publishing rule
$$($1+$2+DST): $$($1+$2+PUB)/%: $2/%
$$(_V)echo "Publishing $$(patsubst ./%,%,$1/$$*)"
$$(_v)mkdir -p $$(@D) && cat $$< > $$@
endef
add_publish_rule = $(eval $(call _add_publish_rule,$1,$2,$3))
#####
#####
# link_project_target($1: project directory, $2: base target name (build, clean, docker-start, etc))
#
# Define the various project-specific rules, including:
# * <target>[$1]: <target>-one[$1] (building this project tree should build the current directory)
# * <target>[<parent>]: <target>[$1] (building the parent project tree should also build this project tree)
#####
define _link_project_target
ifdef MKTRACE
$$(info Tracing link_project_target($1,$2)...)
endif #MKTRACE
$2[$1]-DEPS :=
# This parent link isn't defining the rule but rather just adding a dependency.
# Therefore, we don't call the add_phony_target function here.
ifdef $1/..
$2[$$($1/..)]-DEPS += $2[$1]
$2[$$($1/..)]: $2[$1]
endif #DIR/..
$$(call add_phony_target,$2[$1])
$$(call add_phony_target,$2-one[$1])
$2[$1]: $2-one[$1]
endef #_link_project_target
link_project_target = $(eval $(call _link_project_target,$1,$2))
#####
#####
# include_subdir($1: subdirectory)
#
# Check for a Build.mk file in the given directory. If one exists, include it and
# automatically generate all target dependencies and rules to build the products.
#####
define _include_subdir
# Prevent accidentally including a project file multiple times
ifndef $1+INCLUDE_GUARD
$1+INCLUDE_GUARD := 1
# Allow overriding the Build.mk path for a directory
ifndef $1+BUILD_MK
$1+BUILD_MK := $$(wildcard $1/Build.mk)
endif
ifdef $1+BUILD_MK
# Append project directory to the list of discovered projects
PROJECT_LIST += $1
# Exactly one of these must be defined by Build.mk
TARGET :=
TARGETS :=
# For advanced users that want to define custom build rules for a directory
PRODUCT :=
PRODUCTS :=
CLEAN :=
# Optional list of files to publish
PUBLISH :=
PUBLISH_BUILD :=
PUBLISH_TOP :=
PUBLISH_LIBC :=
PUBLISH_LD :=
# Deployment
DEPLOY_COMMAND :=
DEPLOY_DEPS :=
# Optional CTF flag management
FLAG_FILE := $$(or $$(wildcard $1/real_flag.txt),$$(wildcard $1/flag.txt))
FLAG_DST := flag.txt
# These can optionally be defined by Build.mk for Docker management
DOCKERFILE :=
DOCKER_IMAGE :=
DOCKER_IMAGE_TAG :=
DOCKER_IMAGE_CUSTOM :=
DOCKER_CONTAINER :=
DOCKER_CHALLENGE_NAME :=
DOCKER_CHALLENGE_PATH :=
DOCKER_CHALLENGE_ARGS :=
DOCKER_BUILD_ARGS :=
DOCKER_BUILD_DEPS :=
DOCKER_START_DEPS :=
DOCKER_PORTS :=
DOCKER_PORT_ARGS :=
DOCKER_RUN_ARGS :=
DOCKER_PWNABLESERVER_ARGS :=
DOCKER_RUNNABLE :=
DOCKER_BUILD_ONLY :=
DOCKER_CPULIMIT := $$(DEFAULT_DOCKER_CPULIMIT)
DOCKER_MEMLIMIT := $$(DEFAULT_DOCKER_MEMLIMIT)
DOCKER_TIMELIMIT := $$(DEFAULT_DOCKER_TIMELIMIT)
DOCKER_WRITEABLE :=
DOCKER_PASSWORD := $$(DEFAULT_DOCKER_PASSWORD)
# These can optionally be defined to set directory-specific variables
BITS := $$(DEFAULT_BITS)
CPPFLAGS := $$(DEFAULT_CPPFLAGS)
CFLAGS := $$(DEFAULT_CFLAGS)
CXXFLAGS := $$(DEFAULT_CXXFLAGS)
OFLAGS := $$(DEFAULT_OFLAGS)
ASFLAGS := $$(DEFAULT_ASFLAGS)
LDFLAGS := $$(DEFAULT_LDFLAGS)
NO_EXTRA_CPPFLAGS :=
NO_EXTRA_CFLAGS :=
NO_EXTRA_CXXFLAGS :=
NO_EXTRA_OFLAGS :=
NO_EXTRA_ASFLAGS :=
NO_EXTRA_LDFLAGS :=
NO_EXTRA_FLAGS :=
SRCS := $$(patsubst $1/%,%,$$(foreach ext,c cpp S,$$(wildcard $1/*.$$(ext))))
CC := $$(DEFAULT_CC)
CXX := $$(DEFAULT_CXX)
AS := $$(DEFAULT_AS)
LD := $$(DEFAULT_LD)
AR := $$(DEFAULT_AR)
BINTYPE :=
LIBS :=
LDLIBS :=
USE_LIBPWNABLEHARNESS :=
NO_UNBUFFERED_STDIO :=
NO_RPATH :=
# Ubuntu/glibc versions
UBUNTU_VERSION :=
GLIBC_VERSION :=
# Hardening flags
RELRO := $$(DEFAULT_RELRO)
CANARY := $$(DEFAULT_CANARY)
NX := $$(DEFAULT_NX)
PIE := $$(DEFAULT_PIE)
ASLR := $$(DEFAULT_ASLR)
STRIP := $$(DEFAULT_STRIP)
DEBUG := $$(DEFAULT_DEBUG)
# Set DIR+BUILD to the build directory for this project folder
ifeq "$1" "."
# For container builds, this is the workspace directory
$1+BUILD := $$(BUILD)
else
# For container builds: subdirectories of the workspace directory
# For normal builds: subdirectories of the PwnableHarness repo
$1+BUILD := $$(BUILD)/$1
endif
# Define DIR and BUILD_DIR for use by Build.mk files
DIR := $1
BUILD_DIR := $$($1+BUILD)
# First, include the subdirectory's makefile
ifdef MKDEBUG
$$(info Including $$($1+BUILD_MK))
endif
include $$($1+BUILD_MK)
# Look for new definition of TARGET/TARGETS
ifdef TARGET
# It's an error to define both TARGET and TARGETS
ifdef TARGETS
$$(error $$($1+BUILD_MK) defined both TARGET ($$(TARGET)) and TARGETS ($$(TARGETS))!)
endif
$1+TARGETS := $$(TARGET)
else ifdef TARGETS
$1+TARGETS := $$(TARGETS)